summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_htmlserializer.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/base/test/unit/test_htmlserializer.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/base/test/unit/test_htmlserializer.js')
-rw-r--r--dom/base/test/unit/test_htmlserializer.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/dom/base/test/unit/test_htmlserializer.js b/dom/base/test/unit/test_htmlserializer.js
new file mode 100644
index 0000000000..17995dddde
--- /dev/null
+++ b/dom/base/test/unit/test_htmlserializer.js
@@ -0,0 +1,70 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+add_task(async function testAttrsOutsideBodyWithRawBodyOnly() {
+ // Create a simple HTML document in which the header contains a tag with set
+ // attributes
+ const htmlString = `<html><head><link rel="stylesheet" href="foo"/></head><body>some content</body></html>`;
+
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(htmlString, "text/html");
+
+ // Sanity check
+ const linkElems = doc.head.getElementsByTagName("link");
+ Assert.equal(
+ linkElems.length,
+ 1,
+ "document header should contain one link element"
+ );
+ Assert.equal(
+ linkElems[0].rel,
+ "stylesheet",
+ "link element should have rel attribute set"
+ );
+
+ // Verify that the combination of raw output and body-only does not allow
+ // attributes from header elements to creep into the output string
+ const encoder = Cu.createDocumentEncoder("text/html");
+ encoder.init(
+ doc,
+ "text/html",
+ Ci.nsIDocumentEncoder.OutputRaw | Ci.nsIDocumentEncoder.OutputBodyOnly
+ );
+
+ const result = encoder.encodeToString();
+ Assert.equal(
+ result,
+ "<body>some content</body>",
+ "output should not contain attributes from head elements"
+ );
+});
+
+add_task(async function testAttrsInsideBodyWithRawBodyOnly() {
+ // Create a simple HTML document in which the body contains a tag with set
+ // attributes
+ const htmlString = `<html><head><link rel="stylesheet" href="foo"/></head><body><span id="foo">some content</span></body></html>`;
+
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(htmlString, "text/html");
+
+ // Sanity check
+ const spanElem = doc.getElementById("foo");
+ Assert.ok(spanElem, "should be able to get span element by ID");
+
+ // Verify that the combination of raw output and body-only does not strip
+ // tag attributes inside the body
+ const encoder = Cu.createDocumentEncoder("text/html");
+ encoder.init(
+ doc,
+ "text/html",
+ Ci.nsIDocumentEncoder.OutputRaw | Ci.nsIDocumentEncoder.OutputBodyOnly
+ );
+
+ const result = encoder.encodeToString();
+ Assert.equal(
+ result,
+ `<body><span id="foo">some content</span></body>`,
+ "output should not contain attributes from head elements"
+ );
+});