content/base/test/test_htmlcopyencoder.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:af9331a39bb9
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 -->
5 <head>
6 <title>Test on the html copy encoder</title>
7 <script type="text/javascript" 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=422403">Mozilla Bug </a>
12 <p id="display"></p>
13 <div id="content" style="display: none">
14 </div>
15 <pre id="test">
16 <script class="testbody" type="text/javascript">
17
18 function testHtmlCopyEncoder () {
19 const de = SpecialPowers.Ci.nsIDocumentEncoder;
20 var encoder = SpecialPowers.Cc["@mozilla.org/layout/htmlCopyEncoder;1"]
21 .createInstance(SpecialPowers.Ci.nsIDocumentEncoder);
22 var out, expected;
23
24 var node = document.getElementById('draggable');
25
26
27 // in the following tests, we must use the OutputLFLineBreak flag, to avoid
28 // to have the default line break of the platform in the result, so the test
29 // can pass on all platform
30
31
32 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
33 encoder.setContainerNode(node);
34 out = encoder.encodeToString();
35 expected = 'This is a <em>draggable</em> bit of text.';
36 is(out, expected, "test container node ");
37
38 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
39 encoder.setNode(node);
40 out = encoder.encodeToString();
41 expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>";
42 is(out, expected, "test node");
43
44 var select = window.getSelection();
45 select.selectAllChildren(node);
46
47 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
48 encoder.setSelection(select);
49 out = encoder.encodeToString();
50 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>";
51 is(out, expected, "test selection");
52
53 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputAbsoluteLinks | de.OutputEncodeHTMLEntities | de.OutputSelectionOnly | de.OutputRaw);
54 encoder.setSelection(select);
55 var outContext = {value:''}, outInfo = {value:''};
56 out = encoder.encodeToStringWithContext(outContext, outInfo);
57 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>";
58 is(out, expected, "test encodeToStringWithContext with selection ");
59
60 node.nextSibling.data="\nfoo bar\n";
61 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
62 encoder.setSelection(select);
63 out = encoder.encodeToString();
64 expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>";
65 is(out, expected, "test selection with additional data");
66
67 node = document.getElementById('aList');
68
69 var select = window.getSelection();
70 select.selectAllChildren(node);
71
72 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
73 encoder.setSelection(select);
74 out = encoder.encodeToString();
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 aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>';
76 is(out, expected, "test list selection");
77
78 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
79 encoder.setContainerNode(node);
80 out = encoder.encodeToString();
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';
82 is(out, expected, "test list container node");
83
84 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
85 encoder.setNode(node);
86 out = encoder.encodeToString();
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>";
88 is(out, expected, "test list node");
89
90 var liList = node.getElementsByTagName("li");
91 var range = document.createRange();
92
93 // selection start at the first child of the ol, and end after the element ol
94 range.setStart(node, 1);
95 range.setEnd(node.parentNode, 2);
96 select.removeAllRanges();
97 select.addRange(range);
98 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
99 encoder.setSelection(select);
100 out = encoder.encodeToString();
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>';
102 is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol");
103
104 // selection start at the third child of the ol, and end after the element ol
105 range.setStart(node, 3);
106 range.setEnd(node.parentNode, 2);
107 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
108 encoder.setSelection(select);
109 out = encoder.encodeToString();
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>';
111 is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol");
112
113
114 // selection start at the third child of the ol, and end after the element ol + ol start at the value 5
115 range.setStart(node, 3);
116 range.setEnd(node.parentNode, 2);
117 node.setAttribute("start","5");
118 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
119 encoder.setSelection(select);
120 out = encoder.encodeToString();
121 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>';
122 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");
123
124 // selection contains only some child of the ol
125 node.removeAttribute("start");
126 range.setStart(node, 3);
127 range.setEnd(node, 5);
128 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
129 encoder.setSelection(select);
130 out = encoder.encodeToString();
131 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
132 is(out, expected, "test list selection with range: selection contains only some child of the ol");
133
134 // selection contains only some child of the ol + ol start at the value 5
135 node.setAttribute("start","5");
136 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
137 encoder.setSelection(select);
138 out = encoder.encodeToString();
139 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
140 is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");
141
142 // selection contains only some child of the ol + a value is set on the first li
143 node.removeAttribute("start");
144 liList[0].setAttribute("value","8");
145 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
146 encoder.setSelection(select);
147 out = encoder.encodeToString();
148 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
149 is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");
150
151 select.selectAllChildren(node);
152 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
153 encoder.setSelection(select);
154 out = encoder.encodeToString();
155 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>';
156 is(out, expected, "test list selection with a value on a LI");
157
158 //test Bug 436703
159 node = document.getElementById('aContentEditable');
160 select.selectAllChildren(node);
161 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
162 encoder.setSelection(select);
163 out = encoder.encodeToString();
164 expected = '<p>one</p><p>two</p>';
165 is(out, expected, "select all children in an contentEditable div should not select the div itself");
166
167 SimpleTest.finish();
168 }
169
170
171 SimpleTest.waitForExplicitFinish();
172
173 addLoadEvent(testHtmlCopyEncoder);
174
175 </script>
176 </pre>
177 <div style="display: none">
178
179 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div>
180
181 </div>
182 <div style="display: none">
183
184 <ol id="aList">
185 <li>Lorem ipsum dolor</li>
186 <li>sit amet, <strong>consectetuer</strong> </li>
187 <li>adipiscing elit</li>
188 <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>
189 <li>aptent taciti</li>
190 </ol>
191 foo bar
192 </div>
193
194 <div id="aContentEditable" contentEditable="true"><p>one</p><p>two</p></div>
195 </body>
196 </html>

mercurial