summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_htmlserializer.js
blob: 17995ddddeac224a6c5188a1073d57af60c97872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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"
  );
});