Skip to content

Commit

Permalink
fix: update worker-script innerHTML implementation (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekkolodziej authored Mar 24, 2024
1 parent 0258d26 commit d72574b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/web-worker/worker-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const structureChangingMethodNames = /*#__PURE__*/ commaSplit(

/** setters that could change dimensions of elements */
export const dimensionChangingSetterNames = /*#__PURE__*/ commaSplit(
'className,width,height,hidden,innerHTML,innerText,textContent'
'className,width,height,hidden,innerHTML,innerText,textContent,text'
);

/** method calls that could change dimensions of elements */
Expand Down
12 changes: 9 additions & 3 deletions src/lib/web-worker/worker-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const patchHTMLScriptElement = (WorkerHTMLScriptElement: any, env: WebWor
},
},

text: innerHTMLDescriptor,

textContent: innerHTMLDescriptor,

type: {
Expand All @@ -59,11 +61,15 @@ export const patchHTMLScriptElement = (WorkerHTMLScriptElement: any, env: WebWor
const innerHTMLDescriptor: PropertyDescriptor & ThisType<WorkerNode> = {
get() {
const type = getter(this, ['type']);

if (isScriptJsType(type)) {
return getInstanceStateValue<string>(this, StateProp.innerHTML) || '';
} else {
return getter(this, ['innerHTML']);
const scriptContent = getInstanceStateValue<string>(this, StateProp.innerHTML);
if (scriptContent) {
return scriptContent;
}
}

return getter(this, ['innerHTML']) || '';
},
set(scriptContent: string) {
setInstanceStateValue(this, StateProp.innerHTML, scriptContent);
Expand Down
25 changes: 25 additions & 0 deletions tests/integrations/load-scripts-on-main-thread/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
loadScriptsOnMainThread: [
`${location.protocol}//${location.host}/tests/integrations/load-scripts-on-main-thread/test-script.js`,
`inline-test-script`,
`inline-test-script-text`,
/regex-test-script\.js/,
],
};
Expand Down Expand Up @@ -215,6 +216,30 @@ <h1>Load scripts on main thread 🎉</h1>
})();
</script>
</li>
<li>
<strong>Inline script with text</strong>
<code id="testInlineScriptText"></code>
<script type="text/javascript">
const globalVariableScriptText = 13;
</script>
<script type="text/partytown">
(function () {
const script = document.createElement('script');

script.type = "text/javascript";
script.id = "inline-test-script-text";

script.text = `
(function () {
const testEl = document.getElementById('testInlineScriptText');
testEl.className = 'testInlineScriptText';
testEl.innerHTML = globalVariableScriptText;
})();
`;
document.body.appendChild(script);
})();
</script>
</li>
<li>
<strong>Inline Script ran in the background:</strong>
<code id="inlineTestScriptRanInTheBackground"></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ test('integration window accessor', async ({ page }) => {
await page.waitForSelector('.testInlineScript');
const testInlineScript = page.locator('#testInlineScript');
await expect(testInlineScript).toHaveText('12');

await page.waitForSelector('.testInlineScriptText');
const testInlineScriptText = page.locator('#testInlineScriptText');
await expect(testInlineScriptText).toHaveText('13');
});

test('integration window accessor with snippet', async ({ page }) => {
Expand Down Expand Up @@ -52,4 +56,8 @@ test('integration window accessor with snippet', async ({ page }) => {
await page.waitForSelector('.testInlineScript');
const testInlineScript = page.locator('#testInlineScript');
await expect(testInlineScript).toHaveText('12');

await page.waitForSelector('.testInlineScriptText');
const testInlineScriptText = page.locator('#testInlineScriptText');
await expect(testInlineScriptText).toHaveText('13');
});
25 changes: 25 additions & 0 deletions tests/integrations/load-scripts-on-main-thread/snippet.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
loadScriptsOnMainThread: [
`${location.protocol}//${location.host}/tests/integrations/load-scripts-on-main-thread/test-script.js`,
`inline-test-script`,
`inline-test-script-text`,
/regex-test-script\.js/,
],
});
Expand Down Expand Up @@ -126,6 +127,30 @@ <h1>Load scripts on main thread 🎉</h1>
})()
</script>
</li>
<li>
<strong>Inline script with text</strong>
<code id="testInlineScriptText"></code>
<script type="text/javascript">
const globalVariableScriptText = 13;
</script>
<script type="text/partytown">
(function () {
const script = document.createElement('script');

script.type = "text/javascript";
script.id = "inline-test-script-text";

script.text = `
(function () {
const testEl = document.getElementById('testInlineScriptText');
testEl.className = 'testInlineScriptText';
testEl.innerHTML = globalVariableScriptText;
})();
`;
document.body.appendChild(script);
})();
</script>
</li>
<li>
<strong>Script ran in the background:</strong>
<code id="testScriptRanInTheBackground"></code>
Expand Down
18 changes: 18 additions & 0 deletions tests/platform/script/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ <h1 class="title">Script</h1>
</script>
</li>

<li>
<strong>set innerHTML, nested</strong>
<code some-attr="111" id="testInnerHTMLNested"></code>
<script type="text/partytown">
(function () {
const testEl = document.getElementById('testInnerHTMLNested');
const parent = document.createElement('div');
parent.innerHTML = '<script>console.log(42);<\/script>';

testEl.innerHTML = [
parent.firstChild.tagName,
parent.firstChild.innerHTML
].join(', ');
testEl.className = 'testInnerHTMLNested';
})();
</script>
</li>

<li>
<strong>async/defer</strong>
<code id="testAsync"></code>
Expand Down
4 changes: 4 additions & 0 deletions tests/platform/script/script.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test('script', async ({ page }) => {
const testInnerHTMLError = page.locator('#testInnerHTMLError');
await expect(testInnerHTMLError).toHaveText('gahh');

await page.waitForSelector('.testInnerHTMLNested');
const testInnerHTMLNested = page.locator('#testInnerHTMLNested');
await expect(testInnerHTMLNested).toHaveText('SCRIPT, console.log(42);');

await page.waitForSelector('.testAsync');
const testAsync = page.locator('#testAsync');
const testAsyncText = await testAsync.innerText();
Expand Down

0 comments on commit d72574b

Please sign in to comment.