Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/hello-world/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
}, false);
</script> -->
</body>
</html>
</html>
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h3>Basic</h3>
<li><a href='iframe/'>Iframed storage</a></li>
<li><a href='sandboxed/'>Sandboxed Worker (via iframe)</a></li>
<li><a href='scroll-into-view/'>Scroll into view</a></li>
<li><a href='shadow-root/'>Shadow Root</a></li>
</ul>

<h3>Frameworks</h3>
Expand Down
34 changes: 34 additions & 0 deletions demo/shadow-dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Shadow Root</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/demo.css" rel="stylesheet">
<script src="/dist/main.mjs" type="module"></script>
<script src="/dist/main.js" nomodule defer></script>
<!-- This comment block is intended to make it easier to test both the script module and nomodule path -->
<!-- Comment either block to enable module/nomodule or disable it. -->
<!-- <script src="/dist/main.js" defer></script> -->
</head>
<body>
<div>Host Element</div>
<div src="shadow-dom.js" id="upgrade-me" data-shadow-dom>
<div id="root">
<div>Guest Element</div>
<button>Insert style tag</button>
</div>
</div>
<script type="module">
import {upgradeElement} from '/dist/main.mjs';
upgradeElement(document.getElementById('upgrade-me'), '/dist/worker/worker.mjs');
</script>
<!-- This comment block is intended to make it easier to test both the script module and nomodule path -->
<!-- Comment either block to enable module/nomodule or disable it. -->
<!-- <script async=false defer>
document.addEventListener('DOMContentLoaded', function() {
MainThread.upgradeElement(document.getElementById('upgrade-me'), './dist/worker/worker.js');
}, false);
</script> -->
</body>
</html>
7 changes: 7 additions & 0 deletions demo/shadow-dom/shadow-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const btn = document.getElementsByTagName('button')[0];

btn.addEventListener('click', async () => {
const style = document.createElement('style');
style.textContent = `* { color: red !important }`;
document.body.appendChild(style);
});
7 changes: 7 additions & 0 deletions src/main-thread/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export function install(
baseElement: HTMLElement,
config: InboundWorkerDOMConfiguration,
): Promise<ExportedWorker | null> {
const mode = baseElement.dataset['shadowDom'];
if (mode != null) {
const shadowRoot = baseElement.attachShadow({ mode: (mode || 'open') as ShadowRootMode });
const clonedElement = baseElement.cloneNode(true) as HTMLElement;
shadowRoot.appendChild(clonedElement);
baseElement = clonedElement;
}
const stringContext = new StringContext();
const objectContext = new ObjectContext();
const nodeContext = new NodeContext(stringContext, baseElement);
Expand Down