Skip to content

Commit

Permalink
Parse noscript children as nodes when runScripts is not "dangerously"
Browse files Browse the repository at this point in the history
Fixes #1611.
  • Loading branch information
ACHP authored and domenic committed Jan 25, 2020
1 parent b8c7bb5 commit 7581dba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/api.js
Expand Up @@ -204,7 +204,10 @@ function transformOptions(options, encoding, mimeType) {
referrer: "",
contentType: "text/html",
parsingMode: "html",
parseOptions: { sourceCodeLocationInfo: false },
parseOptions: {
sourceCodeLocationInfo: false,
scriptingEnabled: false
},
runScripts: undefined,
encoding,
pretendToBeVisual: false,
Expand Down Expand Up @@ -260,8 +263,9 @@ function transformOptions(options, encoding, mimeType) {

if (options.runScripts !== undefined) {
transformed.windowOptions.runScripts = String(options.runScripts);
if (transformed.windowOptions.runScripts !== "dangerously" &&
transformed.windowOptions.runScripts !== "outside-only") {
if (transformed.windowOptions.runScripts === "dangerously") {
transformed.windowOptions.parseOptions.scriptingEnabled = true;
} else if (transformed.windowOptions.runScripts !== "outside-only") {
throw new RangeError(`runScripts must be undefined, "dangerously", or "outside-only"`);
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/api/options-run-scripts.js
Expand Up @@ -95,6 +95,35 @@ describe("API: runScripts constructor option", () => {
});
});

describe("<noscript> children", () => {
it("should be considered text when runScripts is set to \"dangerously\"", () => {
const { document } = new JSDOM(
`<body><noscript><div></div></noscript></body>`,
{ runScripts: "dangerously" }
).window;

assert.strictEqual(document.querySelector("noscript").children.length, 0);
assert.strictEqual(document.querySelector("noscript").textContent, "<div></div>");
});
it("should be considered nodes when runScripts is set to \"outside-only\"", () => {
const dom = new JSDOM(
`<body><noscript><div></div></noscript></body>`,
{ runScripts: "outside-only" }
);
const { document } = dom.window;

assert.strictEqual(document.querySelector("noscript").children.length, 1);
assert.instanceOf(document.querySelector("noscript").children[0], dom.window.HTMLDivElement);
});
it("should be considered nodes when runScripts is left undefined", () => {
const dom = new JSDOM(`<body><noscript><div></div></noscript></body>`).window;
const { document } = dom.window;

assert.strictEqual(document.querySelector("noscript").children.length, 1);
assert.instanceOf(document.querySelector("noscript").children[0], dom.window.HTMLDivElement);
});
});

const jsSpecGlobalsDescribe = hasNode10 ? describe.skip : describe;
jsSpecGlobalsDescribe("JS spec globals", () => {
it("should include aliased globals by default", () => {
Expand Down
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>querySelector on template fragments with noscript elements</title>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<template id="template1"><noscript><div></div></noscript></template>
<template id="template2"><noscript><div id="noScriptContent"></div></noscript></template>

<script>
"use strict";
test(() => {
const fragment = document.querySelector("#template1").content;
const noscriptNode = fragment.querySelector("noscript");

assert_equals(noscriptNode.childNodes[0].nodeType, Node.TEXT_NODE, "Is a text node");
assert_equals(noscriptNode.innerHTML, "<div></div>");
}, "noscript child should be text node.");

test(() => {
const fragment = document.querySelector("#template2").content;
const noscriptChildNode = fragment.querySelector("#noScriptContent");
assert_equals(noscriptChildNode, null);
}, "noscript child should be unreachable");
</script>

0 comments on commit 7581dba

Please sign in to comment.