Skip to content

Commit

Permalink
Preserve comments and other block text element's structures
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Jun 4, 2024
1 parent 4d0db85 commit ea6525c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/sku/entry/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export default function createCSPHandler({
console.error(`Invalid script passed to 'registerScript'\n${script}`);
}

parse(script, { blockTextElements: { script: true } })
.querySelectorAll('script')
.forEach(processScriptNode);
parse(script).querySelectorAll('script').forEach(processScriptNode);
};

/**
Expand Down Expand Up @@ -113,7 +111,9 @@ export default function createCSPHandler({
* @returns {string}
*/
const handleHtml = (html) => {
const root = parse(html);
const root = parse(html, {
comment: true,
});

if (!root) {
throw new Error(
Expand Down
68 changes: 64 additions & 4 deletions packages/sku/entry/csp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,70 @@ describe('createCSPHandler', () => {
);
cspHandler.registerScript('<script>console.log("Hello, World!")</script>');

const html = /* html */ `<html><head></head><body>Welcome to my App!</body></html>`;
const html =
/* html */
`<html>
<!--
Hello
World!
-->
<head>
<script>
console.log("Hello, World!");
console.log("Hello, World!");
</script>
<noscript>
You have
Javascript disabled
</noscript>
<style>
div {
color: papayawhip;
}
</style>
</head>
<body>
<div>
Welcome to my App!
</div>
<pre>
Multi
Line
Text
</pre>
</html>`;

expect(cspHandler.handleHtml(html)).toMatchInlineSnapshot(
`"<html><head><meta http-equiv="Content-Security-Policy" content="script-src 'self' 'sha256-uCWFWr3Z6MtMWnxQ5Qyr6+zW+EGpZvdwyqLxlDbe/rE=' 'sha256-lLONtxmGZgnK0e52sTdx8mwK2vMdmln9LPZAbPGHAR0=';"></head><body>Welcome to my App!</body></html>"`,
);
expect(cspHandler.handleHtml(html)).toMatchInlineSnapshot(`
"<html>
<!--
Hello
World!
-->
<head><meta http-equiv="Content-Security-Policy" content="script-src 'self' 'sha256-uCWFWr3Z6MtMWnxQ5Qyr6+zW+EGpZvdwyqLxlDbe/rE=' 'sha256-lLONtxmGZgnK0e52sTdx8mwK2vMdmln9LPZAbPGHAR0=' 'sha256-IYf6lwpasx2aXpcaX33x4ihyqfUb7LQFFjpVwJyfoYk=';">
<script>
console.log("Hello, World!");
console.log("Hello, World!");
</script>
<noscript>
You have
Javascript disabled
</noscript>
<style>
div {
color: papayawhip;
}
</style>
</head>
<div>
Welcome to my App!
</div>
<pre>
Multi
Line
Text
</pre>
</html>"
`);
});
});

0 comments on commit ea6525c

Please sign in to comment.