test_htmlserializer.js (2377B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 add_task(async function testAttrsOutsideBodyWithRawBodyOnly() { 6 // Create a simple HTML document in which the header contains a tag with set 7 // attributes 8 const htmlString = `<html><head><link rel="stylesheet" href="foo"/></head><body>some content</body></html>`; 9 10 const parser = new DOMParser(); 11 const doc = parser.parseFromString(htmlString, "text/html"); 12 13 // Sanity check 14 const linkElems = doc.head.getElementsByTagName("link"); 15 Assert.equal( 16 linkElems.length, 17 1, 18 "document header should contain one link element" 19 ); 20 Assert.equal( 21 linkElems[0].rel, 22 "stylesheet", 23 "link element should have rel attribute set" 24 ); 25 26 // Verify that the combination of raw output and body-only does not allow 27 // attributes from header elements to creep into the output string 28 const encoder = Cu.createDocumentEncoder("text/html"); 29 encoder.init( 30 doc, 31 "text/html", 32 Ci.nsIDocumentEncoder.OutputRaw | Ci.nsIDocumentEncoder.OutputBodyOnly 33 ); 34 35 const result = encoder.encodeToString(); 36 Assert.equal( 37 result, 38 "<body>some content</body>", 39 "output should not contain attributes from head elements" 40 ); 41 }); 42 43 add_task(async function testAttrsInsideBodyWithRawBodyOnly() { 44 // Create a simple HTML document in which the body contains a tag with set 45 // attributes 46 const htmlString = `<html><head><link rel="stylesheet" href="foo"/></head><body><span id="foo">some content</span></body></html>`; 47 48 const parser = new DOMParser(); 49 const doc = parser.parseFromString(htmlString, "text/html"); 50 51 // Sanity check 52 const spanElem = doc.getElementById("foo"); 53 Assert.ok(spanElem, "should be able to get span element by ID"); 54 55 // Verify that the combination of raw output and body-only does not strip 56 // tag attributes inside the body 57 const encoder = Cu.createDocumentEncoder("text/html"); 58 encoder.init( 59 doc, 60 "text/html", 61 Ci.nsIDocumentEncoder.OutputRaw | Ci.nsIDocumentEncoder.OutputBodyOnly 62 ); 63 64 const result = encoder.encodeToString(); 65 Assert.equal( 66 result, 67 `<body><span id="foo">some content</span></body>`, 68 "output should not contain attributes from head elements" 69 ); 70 });