Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
michael@0 | 3 | <!-- |
michael@0 | 4 | --> |
michael@0 | 5 | <head> |
michael@0 | 6 | <title>Test the html copy encoder with XHTML</title> |
michael@0 | 7 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 8 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 9 | </head> |
michael@0 | 10 | <body> |
michael@0 | 11 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=422403">Mozilla Bug </a> |
michael@0 | 12 | <p id="display"></p> |
michael@0 | 13 | <div id="content" style="display: none"> |
michael@0 | 14 | </div> |
michael@0 | 15 | <pre id="test"> |
michael@0 | 16 | <script class="testbody" type="text/javascript"> |
michael@0 | 17 | //<![CDATA[ |
michael@0 | 18 | function testHtmlCopyEncoder () { |
michael@0 | 19 | const de = SpecialPowers.Ci.nsIDocumentEncoder; |
michael@0 | 20 | var encoder = SpecialPowers.Cc["@mozilla.org/layout/htmlCopyEncoder;1"] |
michael@0 | 21 | .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); |
michael@0 | 22 | var out, expected; |
michael@0 | 23 | |
michael@0 | 24 | var node = document.getElementById('draggable'); |
michael@0 | 25 | |
michael@0 | 26 | |
michael@0 | 27 | // in the following tests, we must use the OutputLFLineBreak flag, to avoid |
michael@0 | 28 | // to have the default line break of the platform in the result, so the test |
michael@0 | 29 | // can pass on all platform |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 33 | encoder.setContainerNode(node); |
michael@0 | 34 | out = encoder.encodeToString(); |
michael@0 | 35 | expected = 'This is a <em>draggable</em> <br>bit of text.'; |
michael@0 | 36 | is(out, expected, "test container node "); |
michael@0 | 37 | |
michael@0 | 38 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 39 | encoder.setNode(node); |
michael@0 | 40 | out = encoder.encodeToString(); |
michael@0 | 41 | // the attributes are in the reverse order because the XHTML parser parse in the |
michael@0 | 42 | // right order but the html serializer serializes in the reverse order |
michael@0 | 43 | // (because the html parser stores the attribute in the reverse order, |
michael@0 | 44 | // see bug 213347 for reason). |
michael@0 | 45 | expected = "<div ondragstart=\"doDragStartSelection(event)\" id=\"draggable\">This is a <em>draggable</em> <br>bit of text.</div>"; |
michael@0 | 46 | is(out, expected, "test node"); |
michael@0 | 47 | |
michael@0 | 48 | var select = window.getSelection(); |
michael@0 | 49 | select.selectAllChildren(node); |
michael@0 | 50 | |
michael@0 | 51 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 52 | encoder.setSelection(select); |
michael@0 | 53 | out = encoder.encodeToString(); |
michael@0 | 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>"; |
michael@0 | 55 | todo_is(out, expected, "test selection"); |
michael@0 | 56 | |
michael@0 | 57 | node.nextSibling.data="\nfoo bar\n"; |
michael@0 | 58 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 59 | encoder.setSelection(select); |
michael@0 | 60 | out = encoder.encodeToString(); |
michael@0 | 61 | expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em>\n <br>bit of text.</div>"; |
michael@0 | 62 | todo_is(out, expected, "test selection with additional data"); |
michael@0 | 63 | |
michael@0 | 64 | node = document.getElementById('aList'); |
michael@0 | 65 | |
michael@0 | 66 | var select = window.getSelection(); |
michael@0 | 67 | select.selectAllChildren(node); |
michael@0 | 68 | |
michael@0 | 69 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 70 | encoder.setSelection(select); |
michael@0 | 71 | out = encoder.encodeToString(); |
michael@0 | 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>'; |
michael@0 | 73 | todo_is(out, expected, "test list selection"); |
michael@0 | 74 | |
michael@0 | 75 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 76 | encoder.setContainerNode(node); |
michael@0 | 77 | out = encoder.encodeToString(); |
michael@0 | 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'; |
michael@0 | 79 | is(out, expected, "test list container node"); |
michael@0 | 80 | |
michael@0 | 81 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 82 | encoder.setNode(node); |
michael@0 | 83 | out = encoder.encodeToString(); |
michael@0 | 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>"; |
michael@0 | 85 | is(out, expected, "test list node"); |
michael@0 | 86 | |
michael@0 | 87 | var liList = node.getElementsByTagName("li"); |
michael@0 | 88 | var range = document.createRange(); |
michael@0 | 89 | |
michael@0 | 90 | // selection start at the first child of the ol, and end after the element ol |
michael@0 | 91 | range.setStart(node, 1); |
michael@0 | 92 | range.setEnd(node.parentNode, 2); |
michael@0 | 93 | select.removeAllRanges(); |
michael@0 | 94 | select.addRange(range); |
michael@0 | 95 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 96 | encoder.setSelection(select); |
michael@0 | 97 | out = encoder.encodeToString(); |
michael@0 | 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>'; |
michael@0 | 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"); |
michael@0 | 100 | |
michael@0 | 101 | // selection start at the third child of the ol, and end after the element ol |
michael@0 | 102 | range.setStart(node, 3); |
michael@0 | 103 | range.setEnd(node.parentNode, 2); |
michael@0 | 104 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 105 | encoder.setSelection(select); |
michael@0 | 106 | out = encoder.encodeToString(); |
michael@0 | 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>'; |
michael@0 | 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"); |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 |
michael@0 | 112 | range.setStart(node, 3); |
michael@0 | 113 | range.setEnd(node.parentNode, 2); |
michael@0 | 114 | node.setAttribute("start","5"); |
michael@0 | 115 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 116 | encoder.setSelection(select); |
michael@0 | 117 | out = encoder.encodeToString(); |
michael@0 | 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>'; |
michael@0 | 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"); |
michael@0 | 120 | |
michael@0 | 121 | |
michael@0 | 122 | // selection contains only some child of the ol |
michael@0 | 123 | node.removeAttribute("start"); |
michael@0 | 124 | range.setStart(node, 3); |
michael@0 | 125 | range.setEnd(node, 5); |
michael@0 | 126 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 127 | encoder.setSelection(select); |
michael@0 | 128 | out = encoder.encodeToString(); |
michael@0 | 129 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 130 | todo_is(out, expected, "test list selection with range: selection contains only some child of the ol"); |
michael@0 | 131 | |
michael@0 | 132 | // selection contains only some child of the ol + ol start at the value 5 |
michael@0 | 133 | node.setAttribute("start","5"); |
michael@0 | 134 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 135 | encoder.setSelection(select); |
michael@0 | 136 | out = encoder.encodeToString(); |
michael@0 | 137 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 138 | todo_is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
michael@0 | 139 | |
michael@0 | 140 | |
michael@0 | 141 | // selection contains only some child of the ol + a value is set on the first li |
michael@0 | 142 | node.removeAttribute("start"); |
michael@0 | 143 | liList[0].setAttribute("value","8"); |
michael@0 | 144 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 145 | encoder.setSelection(select); |
michael@0 | 146 | out = encoder.encodeToString(); |
michael@0 | 147 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 148 | todo_is(out, expected, "test list selection: contains only some child of the ol + a value is set on the first li"); |
michael@0 | 149 | |
michael@0 | 150 | select.selectAllChildren(node); |
michael@0 | 151 | encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 152 | encoder.setSelection(select); |
michael@0 | 153 | out = encoder.encodeToString(); |
michael@0 | 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>'; |
michael@0 | 155 | todo_is(out, expected, "test list selection with a value on a LI"); |
michael@0 | 156 | |
michael@0 | 157 | SimpleTest.finish(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | |
michael@0 | 161 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 162 | |
michael@0 | 163 | addLoadEvent(testHtmlCopyEncoder); |
michael@0 | 164 | //]]> |
michael@0 | 165 | </script> |
michael@0 | 166 | </pre> |
michael@0 | 167 | <div style="display: none"> |
michael@0 | 168 | |
michael@0 | 169 | <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> <br/>bit of text.</div> |
michael@0 | 170 | |
michael@0 | 171 | </div> |
michael@0 | 172 | <div style="display: none"> |
michael@0 | 173 | |
michael@0 | 174 | <ol id="aList"> |
michael@0 | 175 | <li>Lorem ipsum dolor</li> |
michael@0 | 176 | <li>sit amet, <strong>consectetuer</strong> </li> |
michael@0 | 177 | <li>adipiscing elit</li> |
michael@0 | 178 | <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> |
michael@0 | 179 | <li>aptent taciti</li> |
michael@0 | 180 | </ol> |
michael@0 | 181 | foo bar |
michael@0 | 182 | </div> |
michael@0 | 183 | </body> |
michael@0 | 184 | </html> |