1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_htmlcopyencoder.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,196 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +--> 1.8 +<head> 1.9 + <title>Test on the html copy encoder</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 + 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> 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 + expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; 1.45 + is(out, expected, "test node"); 1.46 + 1.47 + var select = window.getSelection(); 1.48 + select.selectAllChildren(node); 1.49 + 1.50 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.51 + encoder.setSelection(select); 1.52 + out = encoder.encodeToString(); 1.53 + expected = "<div style=\"display: none\">\n\n<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>\n\n</div>"; 1.54 + is(out, expected, "test selection"); 1.55 + 1.56 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputAbsoluteLinks | de.OutputEncodeHTMLEntities | de.OutputSelectionOnly | de.OutputRaw); 1.57 + encoder.setSelection(select); 1.58 + var outContext = {value:''}, outInfo = {value:''}; 1.59 + out = encoder.encodeToStringWithContext(outContext, outInfo); 1.60 + expected = "<div style=\"display: none\">\n\n<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>\n\n</div>"; 1.61 + is(out, expected, "test encodeToStringWithContext with selection "); 1.62 + 1.63 + node.nextSibling.data="\nfoo bar\n"; 1.64 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.65 + encoder.setSelection(select); 1.66 + out = encoder.encodeToString(); 1.67 + expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; 1.68 + is(out, expected, "test selection with additional data"); 1.69 + 1.70 + node = document.getElementById('aList'); 1.71 + 1.72 + var select = window.getSelection(); 1.73 + select.selectAllChildren(node); 1.74 + 1.75 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.76 + encoder.setSelection(select); 1.77 + out = encoder.encodeToString(); 1.78 + 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.79 + is(out, expected, "test list selection"); 1.80 + 1.81 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.82 + encoder.setContainerNode(node); 1.83 + out = encoder.encodeToString(); 1.84 + 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.85 + is(out, expected, "test list container node"); 1.86 + 1.87 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.88 + encoder.setNode(node); 1.89 + out = encoder.encodeToString(); 1.90 + 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.91 + is(out, expected, "test list node"); 1.92 + 1.93 + var liList = node.getElementsByTagName("li"); 1.94 + var range = document.createRange(); 1.95 + 1.96 + // selection start at the first child of the ol, and end after the element ol 1.97 + range.setStart(node, 1); 1.98 + range.setEnd(node.parentNode, 2); 1.99 + select.removeAllRanges(); 1.100 + select.addRange(range); 1.101 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.102 + encoder.setSelection(select); 1.103 + out = encoder.encodeToString(); 1.104 + 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.105 + is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); 1.106 + 1.107 + // selection start at the third child of the ol, and end after the element ol 1.108 + range.setStart(node, 3); 1.109 + range.setEnd(node.parentNode, 2); 1.110 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.111 + encoder.setSelection(select); 1.112 + out = encoder.encodeToString(); 1.113 + 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.114 + is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); 1.115 + 1.116 + 1.117 + // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 1.118 + range.setStart(node, 3); 1.119 + range.setEnd(node.parentNode, 2); 1.120 + node.setAttribute("start","5"); 1.121 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.122 + encoder.setSelection(select); 1.123 + out = encoder.encodeToString(); 1.124 + expected = '<ol start=\"5\" id=\"aList\"><li value=\"6\">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.125 + 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.126 + 1.127 + // selection contains only some child of the ol 1.128 + node.removeAttribute("start"); 1.129 + range.setStart(node, 3); 1.130 + range.setEnd(node, 5); 1.131 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.132 + encoder.setSelection(select); 1.133 + out = encoder.encodeToString(); 1.134 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.135 + is(out, expected, "test list selection with range: selection contains only some child of the ol"); 1.136 + 1.137 + // selection contains only some child of the ol + ol start at the value 5 1.138 + node.setAttribute("start","5"); 1.139 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.140 + encoder.setSelection(select); 1.141 + out = encoder.encodeToString(); 1.142 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.143 + is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); 1.144 + 1.145 + // selection contains only some child of the ol + a value is set on the first li 1.146 + node.removeAttribute("start"); 1.147 + liList[0].setAttribute("value","8"); 1.148 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.149 + encoder.setSelection(select); 1.150 + out = encoder.encodeToString(); 1.151 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.152 + is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); 1.153 + 1.154 + select.selectAllChildren(node); 1.155 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.156 + encoder.setSelection(select); 1.157 + out = encoder.encodeToString(); 1.158 + 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 aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>'; 1.159 + is(out, expected, "test list selection with a value on a LI"); 1.160 + 1.161 + //test Bug 436703 1.162 + node = document.getElementById('aContentEditable'); 1.163 + select.selectAllChildren(node); 1.164 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.165 + encoder.setSelection(select); 1.166 + out = encoder.encodeToString(); 1.167 + expected = '<p>one</p><p>two</p>'; 1.168 + is(out, expected, "select all children in an contentEditable div should not select the div itself"); 1.169 + 1.170 + SimpleTest.finish(); 1.171 +} 1.172 + 1.173 + 1.174 +SimpleTest.waitForExplicitFinish(); 1.175 + 1.176 +addLoadEvent(testHtmlCopyEncoder); 1.177 + 1.178 +</script> 1.179 +</pre> 1.180 +<div style="display: none"> 1.181 + 1.182 +<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> 1.183 + 1.184 +</div> 1.185 +<div style="display: none"> 1.186 + 1.187 +<ol id="aList"> 1.188 + <li>Lorem ipsum dolor</li> 1.189 + <li>sit amet, <strong>consectetuer</strong> </li> 1.190 + <li>adipiscing elit</li> 1.191 + <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> 1.192 + <li>aptent taciti</li> 1.193 +</ol> 1.194 +foo bar 1.195 +</div> 1.196 + 1.197 +<div id="aContentEditable" contentEditable="true"><p>one</p><p>two</p></div> 1.198 +</body> 1.199 +</html>