|
1 <!DOCTYPE HTML> |
|
2 <html xmlns="http://www.w3.org/1999/xhtml"> |
|
3 <!-- |
|
4 --> |
|
5 <head> |
|
6 <title>Test the html copy encoder with XHTML</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 //<![CDATA[ |
|
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> <br>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 // the attributes are in the reverse order because the XHTML parser parse in the |
|
42 // right order but the html serializer serializes in the reverse order |
|
43 // (because the html parser stores the attribute in the reverse order, |
|
44 // see bug 213347 for reason). |
|
45 expected = "<div ondragstart=\"doDragStartSelection(event)\" id=\"draggable\">This is a <em>draggable</em> <br>bit of text.</div>"; |
|
46 is(out, expected, "test node"); |
|
47 |
|
48 var select = window.getSelection(); |
|
49 select.selectAllChildren(node); |
|
50 |
|
51 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
52 encoder.setSelection(select); |
|
53 out = encoder.encodeToString(); |
|
54 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>"; |
|
55 todo_is(out, expected, "test selection"); |
|
56 |
|
57 node.nextSibling.data="\nfoo bar\n"; |
|
58 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
59 encoder.setSelection(select); |
|
60 out = encoder.encodeToString(); |
|
61 expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em>\n <br>bit of text.</div>"; |
|
62 todo_is(out, expected, "test selection with additional data"); |
|
63 |
|
64 node = document.getElementById('aList'); |
|
65 |
|
66 var select = window.getSelection(); |
|
67 select.selectAllChildren(node); |
|
68 |
|
69 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
70 encoder.setSelection(select); |
|
71 out = encoder.encodeToString(); |
|
72 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>'; |
|
73 todo_is(out, expected, "test list selection"); |
|
74 |
|
75 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
76 encoder.setContainerNode(node); |
|
77 out = encoder.encodeToString(); |
|
78 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'; |
|
79 is(out, expected, "test list container node"); |
|
80 |
|
81 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
82 encoder.setNode(node); |
|
83 out = encoder.encodeToString(); |
|
84 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>"; |
|
85 is(out, expected, "test list node"); |
|
86 |
|
87 var liList = node.getElementsByTagName("li"); |
|
88 var range = document.createRange(); |
|
89 |
|
90 // selection start at the first child of the ol, and end after the element ol |
|
91 range.setStart(node, 1); |
|
92 range.setEnd(node.parentNode, 2); |
|
93 select.removeAllRanges(); |
|
94 select.addRange(range); |
|
95 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
96 encoder.setSelection(select); |
|
97 out = encoder.encodeToString(); |
|
98 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>'; |
|
99 todo_is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); |
|
100 |
|
101 // selection start at the third child of the ol, and end after the element ol |
|
102 range.setStart(node, 3); |
|
103 range.setEnd(node.parentNode, 2); |
|
104 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
105 encoder.setSelection(select); |
|
106 out = encoder.encodeToString(); |
|
107 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>'; |
|
108 todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); |
|
109 |
|
110 |
|
111 // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 |
|
112 range.setStart(node, 3); |
|
113 range.setEnd(node.parentNode, 2); |
|
114 node.setAttribute("start","5"); |
|
115 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
116 encoder.setSelection(select); |
|
117 out = encoder.encodeToString(); |
|
118 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>'; |
|
119 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"); |
|
120 |
|
121 |
|
122 // selection contains only some child of the ol |
|
123 node.removeAttribute("start"); |
|
124 range.setStart(node, 3); |
|
125 range.setEnd(node, 5); |
|
126 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
127 encoder.setSelection(select); |
|
128 out = encoder.encodeToString(); |
|
129 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
130 todo_is(out, expected, "test list selection with range: selection contains only some child of the ol"); |
|
131 |
|
132 // selection contains only some child of the ol + ol start at the value 5 |
|
133 node.setAttribute("start","5"); |
|
134 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
135 encoder.setSelection(select); |
|
136 out = encoder.encodeToString(); |
|
137 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
138 todo_is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
|
139 |
|
140 |
|
141 // selection contains only some child of the ol + a value is set on the first li |
|
142 node.removeAttribute("start"); |
|
143 liList[0].setAttribute("value","8"); |
|
144 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
145 encoder.setSelection(select); |
|
146 out = encoder.encodeToString(); |
|
147 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
148 todo_is(out, expected, "test list selection: contains only some child of the ol + a value is set on the first li"); |
|
149 |
|
150 select.selectAllChildren(node); |
|
151 encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
152 encoder.setSelection(select); |
|
153 out = encoder.encodeToString(); |
|
154 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>'; |
|
155 todo_is(out, expected, "test list selection with a value on a LI"); |
|
156 |
|
157 SimpleTest.finish(); |
|
158 } |
|
159 |
|
160 |
|
161 SimpleTest.waitForExplicitFinish(); |
|
162 |
|
163 addLoadEvent(testHtmlCopyEncoder); |
|
164 //]]> |
|
165 </script> |
|
166 </pre> |
|
167 <div style="display: none"> |
|
168 |
|
169 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> <br/>bit of text.</div> |
|
170 |
|
171 </div> |
|
172 <div style="display: none"> |
|
173 |
|
174 <ol id="aList"> |
|
175 <li>Lorem ipsum dolor</li> |
|
176 <li>sit amet, <strong>consectetuer</strong> </li> |
|
177 <li>adipiscing elit</li> |
|
178 <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> |
|
179 <li>aptent taciti</li> |
|
180 </ol> |
|
181 foo bar |
|
182 </div> |
|
183 </body> |
|
184 </html> |