test_bug541937.html (5469B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 --> 5 <head> 6 <title>Test for XHTML serializer, bug 541937</title> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 9 </head> 10 <body> 11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=541937">Mozilla Bug </a> 12 <p id="display"></p> 13 <div id="content" style="display: none"> 14 <iframe id="testframe" src="file_bug541937.html"> 15 </iframe> 16 <iframe id="testframe2" src="file_bug541937.xhtml"> 17 </iframe> 18 </div> 19 20 <pre id="test"> 21 <script class="testbody" type="text/javascript"> 22 23 function testSerializer () { 24 const de = SpecialPowers.Ci.nsIDocumentEncoder; 25 var encoder = SpecialPowers.Cu.createDocumentEncoder("text/html"); 26 27 var parser = new DOMParser(); 28 var serializer = new XMLSerializer(); 29 30 // with content 31 var str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes --> \n<content xmlns=""/></link>\n</doc>'; 32 var expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes --> \n<content xmlns=""/></link>\n</doc>'; 33 34 var doc = parser.parseFromString(str,"application/xml"); 35 var result = serializer.serializeToString(doc); 36 result = result.replace(/\r\n/mg, "\n"); 37 is(result, expected, "serialization of a link element inside an xml document with children"); 38 39 // with only whitespaces 40 str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> \n </link>\n</doc>'; 41 expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> \n </link>\n</doc>'; 42 43 doc = parser.parseFromString(str,"application/xml"); 44 result = serializer.serializeToString(doc); 45 result = result.replace(/\r\n/mg, "\n"); 46 is(result, expected, "serialization of a link element with only whitespaces as content, inside an xml document"); 47 48 // with only one space as content 49 str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>'; 50 expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>'; 51 52 doc = parser.parseFromString(str,"application/xml"); 53 result = serializer.serializeToString(doc); 54 result = result.replace(/\r\n/mg, "\n"); 55 is(result, expected, "serialization of a link element with only one space as content, inside an xml document"); 56 57 // let's remove the content 58 str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> <!-- child nodes --> \ndeleted content<content xmlns=""/> </link>\n</doc>'; 59 expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>'; 60 61 doc = parser.parseFromString(str,"application/xml"); 62 doc.documentElement.firstElementChild.textContent = ''; 63 result = serializer.serializeToString(doc); 64 result = result.replace(/\r\n/mg, "\n"); 65 is(result, expected, "serialization of a link element on which we removed dynamically the content, inside an xml document"); 66 67 // with no content but an ended tag 68 str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"></link>\n</doc>'; 69 expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>'; 70 71 doc = parser.parseFromString(str,"application/xml"); 72 result = serializer.serializeToString(doc); 73 result = result.replace(/\r\n/mg, "\n"); 74 is(result, expected, "serialization of a link element with no content but with an ended tag, inside an xml document"); 75 76 // with no content 77 str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"/>\n</doc>'; 78 expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>'; 79 80 doc = parser.parseFromString(str,"application/xml"); 81 result = serializer.serializeToString(doc); 82 result = result.replace(/\r\n/mg, "\n"); 83 is(result, expected, "serialization of a link element with no content, inside an xml document"); 84 85 86 doc = $("testframe").contentDocument; 87 encoder.init(doc, "text/html", de.OutputLFLineBreak); 88 encoder.setCharset("UTF-8"); 89 result = encoder.encodeToString(); 90 expected = '<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n <title>Test</title>\n'; 91 expected += ' <link rel=\"Top\" href=\"\"> '; 92 expected += ' </head><body>foo \n\n\n <p>Hello world</p>\n</body></html>'; 93 is(result, expected, "serialization of a link element with content, inside an html document"); 94 95 doc = $("testframe2").contentDocument; 96 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak); 97 encoder.setCharset("UTF-8"); 98 result = encoder.encodeToString(); 99 expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'; 100 expected += '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n <title>Test</title>\n'; 101 expected += ' <link rel="Top" href=""> foo </link>\n'; 102 expected += '\n</head>\n<body>\n <p>Hello world</p>\n</body>\n</html>'; 103 is(result, expected, "serialization of a link element with content, inside an xhtml document"); 104 105 SimpleTest.finish(); 106 } 107 108 109 SimpleTest.waitForExplicitFinish(); 110 111 addLoadEvent(testSerializer); 112 113 </script> 114 </pre> 115 </body> 116 </html>