-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add css injection via style tag and add testcase
- Loading branch information
1 parent
f1a6ff8
commit ed308a9
Showing
3 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { runScriptlet, clearGlobalProps } from '../helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'inject-css-in-shadow-dom'; | ||
|
||
const HOST_ID = 'host'; | ||
const TARGET_ID = 'target'; | ||
const TARGET_CSS_PROP = 'color'; | ||
const TARGET_CSS_VALUE = 'rgb(255, 0, 0)'; | ||
const CSS_TEXT = `#target { ${TARGET_CSS_PROP}: ${TARGET_CSS_VALUE} !important}`; | ||
// const CSS_TEXT = '#target { color: red !important}'; | ||
|
||
const appendTarget = (parent) => { | ||
const target = document.createElement('h1'); | ||
target.id = TARGET_ID; | ||
target.innerText = 'Target element'; | ||
return parent.appendChild(target); | ||
}; | ||
|
||
const appendHost = () => { | ||
const host = document.createElement('div'); | ||
host.id = HOST_ID; | ||
return document.body.appendChild(host); | ||
}; | ||
|
||
const removeHost = () => document.getElementById(HOST_ID)?.remove(); | ||
|
||
const beforeEach = () => { | ||
window.__debug = () => { | ||
window.hit = 'FIRED'; | ||
}; | ||
}; | ||
|
||
const afterEach = () => { | ||
clearGlobalProps('hit', '__debug'); | ||
removeHost(); | ||
}; | ||
|
||
module(name, { beforeEach, afterEach }); | ||
|
||
// some browsers do not support ShadowRoot | ||
// for example, Firefox 52 which is used for browserstack tests | ||
// https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot | ||
const isSupported = typeof Element.prototype.attachShadow !== 'undefined'; | ||
|
||
if (!isSupported) { | ||
test('unsupported', (assert) => { | ||
assert.ok(true, 'Browser does not support it'); | ||
}); | ||
} else { | ||
test('styles applied to shadow root subtree with adoptedStyleSheets', (assert) => { | ||
runScriptlet(name, [CSS_TEXT]); | ||
|
||
const host = appendHost(); | ||
const shadowRoot = host.attachShadow({ mode: 'closed' }); | ||
appendTarget(shadowRoot); | ||
|
||
const target = shadowRoot.getElementById(TARGET_ID); | ||
assert.strictEqual(getComputedStyle(target)[TARGET_CSS_PROP], TARGET_CSS_VALUE, 'style was applied on target'); | ||
assert.strictEqual(window.hit, 'FIRED', 'hit function was executed'); | ||
}); | ||
} |