1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_htmlcopyencoder.xhtml Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.6 +<!-- 1.7 +--> 1.8 +<head> 1.9 + <title>Test the html copy encoder with XHTML</title> 1.10 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.11 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.12 +</head> 1.13 +<body> 1.14 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=422403">Mozilla Bug </a> 1.15 +<p id="display"></p> 1.16 +<div id="content" style="display: none"> 1.17 +</div> 1.18 +<pre id="test"> 1.19 +<script class="testbody" type="text/javascript"> 1.20 +//<![CDATA[ 1.21 +function testHtmlCopyEncoder () { 1.22 + const de = SpecialPowers.Ci.nsIDocumentEncoder; 1.23 + var encoder = SpecialPowers.Cc["@mozilla.org/layout/htmlCopyEncoder;1"] 1.24 + .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); 1.25 + var out, expected; 1.26 + 1.27 + var node = document.getElementById('draggable'); 1.28 + 1.29 + 1.30 + // in the following tests, we must use the OutputLFLineBreak flag, to avoid 1.31 + // to have the default line break of the platform in the result, so the test 1.32 + // can pass on all platform 1.33 + 1.34 + 1.35 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.36 + encoder.setContainerNode(node); 1.37 + out = encoder.encodeToString(); 1.38 + expected = 'This is a <em>draggable</em> <br>bit of text.'; 1.39 + is(out, expected, "test container node "); 1.40 + 1.41 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.42 + encoder.setNode(node); 1.43 + out = encoder.encodeToString(); 1.44 + // the attributes are in the reverse order because the XHTML parser parse in the 1.45 + // right order but the html serializer serializes in the reverse order 1.46 + // (because the html parser stores the attribute in the reverse order, 1.47 + // see bug 213347 for reason). 1.48 + expected = "<div ondragstart=\"doDragStartSelection(event)\" id=\"draggable\">This is a <em>draggable</em> <br>bit of text.</div>"; 1.49 + is(out, expected, "test node"); 1.50 + 1.51 + var select = window.getSelection(); 1.52 + select.selectAllChildren(node); 1.53 + 1.54 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.55 + encoder.setSelection(select); 1.56 + out = encoder.encodeToString(); 1.57 + expected = "<div style=\"display: none;\">\n\n<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> <br>bit of text.</div>\n\n</div>"; 1.58 + todo_is(out, expected, "test selection"); 1.59 + 1.60 + node.nextSibling.data="\nfoo bar\n"; 1.61 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.62 + encoder.setSelection(select); 1.63 + out = encoder.encodeToString(); 1.64 + expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em>\n <br>bit of text.</div>"; 1.65 + todo_is(out, expected, "test selection with additional data"); 1.66 + 1.67 + node = document.getElementById('aList'); 1.68 + 1.69 + var select = window.getSelection(); 1.70 + select.selectAllChildren(node); 1.71 + 1.72 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.73 + encoder.setSelection(select); 1.74 + out = encoder.encodeToString(); 1.75 + expected = '<ol id=\"aList\">\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.76 + todo_is(out, expected, "test list selection"); 1.77 + 1.78 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.79 + encoder.setContainerNode(node); 1.80 + out = encoder.encodeToString(); 1.81 + expected = '\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n'; 1.82 + is(out, expected, "test list container node"); 1.83 + 1.84 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.85 + encoder.setNode(node); 1.86 + out = encoder.encodeToString(); 1.87 + expected = "<ol id=\"aList\">\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>"; 1.88 + is(out, expected, "test list node"); 1.89 + 1.90 + var liList = node.getElementsByTagName("li"); 1.91 + var range = document.createRange(); 1.92 + 1.93 + // selection start at the first child of the ol, and end after the element ol 1.94 + range.setStart(node, 1); 1.95 + range.setEnd(node.parentNode, 2); 1.96 + select.removeAllRanges(); 1.97 + select.addRange(range); 1.98 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.99 + encoder.setSelection(select); 1.100 + out = encoder.encodeToString(); 1.101 + expected = '<ol id="aList">\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.102 + todo_is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); 1.103 + 1.104 + // selection start at the third child of the ol, and end after the element ol 1.105 + range.setStart(node, 3); 1.106 + range.setEnd(node.parentNode, 2); 1.107 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.108 + encoder.setSelection(select); 1.109 + out = encoder.encodeToString(); 1.110 + expected = '<ol id="aList"><li value=\"2\">sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.111 + todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); 1.112 + 1.113 + 1.114 + // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 1.115 + range.setStart(node, 3); 1.116 + range.setEnd(node.parentNode, 2); 1.117 + node.setAttribute("start","5"); 1.118 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.119 + encoder.setSelection(select); 1.120 + out = encoder.encodeToString(); 1.121 + expected = '<ol start=\"5\" id=\"aList\"><li value=\"6\">sit amet, <strong>consectetuer</strong>\n </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.122 + todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol + ol start at the value 5"); 1.123 + 1.124 + 1.125 + // selection contains only some child of the ol 1.126 + node.removeAttribute("start"); 1.127 + range.setStart(node, 3); 1.128 + range.setEnd(node, 5); 1.129 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.130 + encoder.setSelection(select); 1.131 + out = encoder.encodeToString(); 1.132 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.133 + todo_is(out, expected, "test list selection with range: selection contains only some child of the ol"); 1.134 + 1.135 + // selection contains only some child of the ol + ol start at the value 5 1.136 + node.setAttribute("start","5"); 1.137 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.138 + encoder.setSelection(select); 1.139 + out = encoder.encodeToString(); 1.140 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.141 + todo_is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); 1.142 + 1.143 + 1.144 + // selection contains only some child of the ol + a value is set on the first li 1.145 + node.removeAttribute("start"); 1.146 + liList[0].setAttribute("value","8"); 1.147 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.148 + encoder.setSelection(select); 1.149 + out = encoder.encodeToString(); 1.150 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.151 + todo_is(out, expected, "test list selection: contains only some child of the ol + a value is set on the first li"); 1.152 + 1.153 + select.selectAllChildren(node); 1.154 + encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); 1.155 + encoder.setSelection(select); 1.156 + out = encoder.encodeToString(); 1.157 + expected = '<ol id=\"aList\">\n <li value=\"8\">Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.158 + todo_is(out, expected, "test list selection with a value on a LI"); 1.159 + 1.160 + SimpleTest.finish(); 1.161 +} 1.162 + 1.163 + 1.164 +SimpleTest.waitForExplicitFinish(); 1.165 + 1.166 +addLoadEvent(testHtmlCopyEncoder); 1.167 +//]]> 1.168 +</script> 1.169 +</pre> 1.170 +<div style="display: none"> 1.171 + 1.172 +<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> <br/>bit of text.</div> 1.173 + 1.174 +</div> 1.175 +<div style="display: none"> 1.176 + 1.177 +<ol id="aList"> 1.178 + <li>Lorem ipsum dolor</li> 1.179 + <li>sit amet, <strong>consectetuer</strong> </li> 1.180 + <li>adipiscing elit</li> 1.181 + <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> 1.182 + <li>aptent taciti</li> 1.183 +</ol> 1.184 +foo bar 1.185 +</div> 1.186 +</body> 1.187 +</html>