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> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=498240 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug 498240</title> |
michael@0 | 8 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 10 | <style> |
michael@0 | 11 | .container { border: 1px solid blue; display:block; } |
michael@0 | 12 | b { color:blue; } |
michael@0 | 13 | i { color:magenta; } |
michael@0 | 14 | </style> |
michael@0 | 15 | </head> |
michael@0 | 16 | <body> |
michael@0 | 17 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=498240">Mozilla Bug 498240</a> |
michael@0 | 18 | <p id="display"></p> |
michael@0 | 19 | <div id="content" style="display: none"> |
michael@0 | 20 | |
michael@0 | 21 | </div> |
michael@0 | 22 | <pre id="test"> |
michael@0 | 23 | <script type="application/javascript"> |
michael@0 | 24 | |
michael@0 | 25 | /** Test for Bug 498240 **/ |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | function create(s) { |
michael@0 | 29 | var p = document.createElement('span'); |
michael@0 | 30 | p.innerHTML = s; |
michael@0 | 31 | p.setAttribute("class","container"); |
michael@0 | 32 | document.body.appendChild(p); |
michael@0 | 33 | return p; |
michael@0 | 34 | } |
michael@0 | 35 | function select(start, startOffset, end, endOffset) { |
michael@0 | 36 | var sel = getSelection(); |
michael@0 | 37 | sel.removeAllRanges(); |
michael@0 | 38 | var range = document.createRange(); |
michael@0 | 39 | range.setStart(start, startOffset); |
michael@0 | 40 | range.setEnd(end, endOffset); |
michael@0 | 41 | sel.addRange(range); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function insertClone(node) { |
michael@0 | 45 | var sel = getSelection(); |
michael@0 | 46 | var range = sel.getRangeAt(0); |
michael@0 | 47 | range.insertNode(node.cloneNode(true)); |
michael@0 | 48 | } |
michael@0 | 49 | function insertCloneAtEnd(node) { |
michael@0 | 50 | var sel = getSelection(); |
michael@0 | 51 | var range = sel.getRangeAt(0); |
michael@0 | 52 | range.endContainer.insertBefore(node.cloneNode(true),range.endContainer.childNodes[range.endOffset]); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function check(start, startOffset, end, endOffset, s) { |
michael@0 | 56 | var sel = getSelection(); |
michael@0 | 57 | var range = sel.getRangeAt(0); |
michael@0 | 58 | is(range.startContainer, start, "wrong start node for range: '"+range.toString()+"'"); |
michael@0 | 59 | is(range.startOffset, startOffset, "wrong start offset for range: '"+range.toString()+"'"); |
michael@0 | 60 | is(range.endContainer, end, "wrong end node for range: '"+range.toString()+"'"); |
michael@0 | 61 | is(range.endOffset, endOffset, "wrong end offset for range: '"+range.toString()+"'"); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function testInsertNode(node) { |
michael@0 | 65 | var p; |
michael@0 | 66 | |
michael@0 | 67 | p = create('a<b>bc</b>'); |
michael@0 | 68 | select(p.childNodes[0],0,p.childNodes[1],0); |
michael@0 | 69 | insertClone(node); |
michael@0 | 70 | check(p.childNodes[0],0,p.childNodes[3],0); |
michael@0 | 71 | |
michael@0 | 72 | p = create('d<b>ef</b>'); |
michael@0 | 73 | select(p.childNodes[0],0,p.childNodes[1],1); |
michael@0 | 74 | insertClone(node); |
michael@0 | 75 | check(p.childNodes[0],0,p.childNodes[3],1); |
michael@0 | 76 | |
michael@0 | 77 | p = create('g<b>h</b>'); |
michael@0 | 78 | select(p.childNodes[0],0,p.childNodes[0],0); |
michael@0 | 79 | insertClone(node); |
michael@0 | 80 | check(p.childNodes[0],0,p,2); |
michael@0 | 81 | |
michael@0 | 82 | p = create('i<b>j</b>'); |
michael@0 | 83 | select(p.childNodes[0],1,p.childNodes[0],1); |
michael@0 | 84 | insertClone(node); |
michael@0 | 85 | check(p.childNodes[0],1,p,2); |
michael@0 | 86 | |
michael@0 | 87 | p = create('k<b>l</b>'); |
michael@0 | 88 | select(p.childNodes[0],0,p.childNodes[1].childNodes[0],0); |
michael@0 | 89 | insertClone(node); |
michael@0 | 90 | check(p.childNodes[0],0,p.childNodes[3].childNodes[0],0); |
michael@0 | 91 | |
michael@0 | 92 | p = create('m<b>no</b>'); |
michael@0 | 93 | select(p.childNodes[0],1,p.childNodes[1].childNodes[0],0); |
michael@0 | 94 | insertClone(node); |
michael@0 | 95 | check(p.childNodes[0],1,p.childNodes[3].childNodes[0],0); |
michael@0 | 96 | |
michael@0 | 97 | p = create('p<b>qr</b>'); |
michael@0 | 98 | select(p.childNodes[0],1,p.childNodes[1].childNodes[0],1); |
michael@0 | 99 | insertClone(node); |
michael@0 | 100 | check(p.childNodes[0],1,p.childNodes[3].childNodes[0],1); |
michael@0 | 101 | |
michael@0 | 102 | p = create('s<b>tu</b>'); |
michael@0 | 103 | select(p.childNodes[0],1,p.childNodes[1],0); |
michael@0 | 104 | insertClone(node); |
michael@0 | 105 | check(p.childNodes[0],1,p.childNodes[3],0); |
michael@0 | 106 | |
michael@0 | 107 | p = create('<i>A</i><b>BC</b>'); |
michael@0 | 108 | select(p.childNodes[0],0,p.childNodes[1],0); |
michael@0 | 109 | insertClone(node); |
michael@0 | 110 | check(p.childNodes[0],0,p.childNodes[1],0); |
michael@0 | 111 | |
michael@0 | 112 | p = create('<i>D</i><b>EF</b>'); |
michael@0 | 113 | select(p.childNodes[0],1,p.childNodes[1],1); |
michael@0 | 114 | insertClone(node); |
michael@0 | 115 | check(p.childNodes[0],1,p.childNodes[1],1); |
michael@0 | 116 | |
michael@0 | 117 | p = create('<i></i><b>GH</b>'); |
michael@0 | 118 | select(p.childNodes[0],0,p.childNodes[1],0); |
michael@0 | 119 | insertClone(node); |
michael@0 | 120 | check(p.childNodes[0],0,p.childNodes[1],0); |
michael@0 | 121 | |
michael@0 | 122 | p = create('<i>I</i><b>J</b>'); |
michael@0 | 123 | select(p,0,p.childNodes[1],0); |
michael@0 | 124 | insertClone(node); |
michael@0 | 125 | check(p,0,p.childNodes[2],0); |
michael@0 | 126 | |
michael@0 | 127 | p = create('<i>K</i><b>L</b>'); |
michael@0 | 128 | select(p,0,p,2); |
michael@0 | 129 | insertClone(node); |
michael@0 | 130 | check(p,0,p,3); |
michael@0 | 131 | |
michael@0 | 132 | p = create('<i>M</i><b>N</b>'); |
michael@0 | 133 | select(p,1,p,2); |
michael@0 | 134 | insertClone(node); |
michael@0 | 135 | check(p,1,p,3); |
michael@0 | 136 | |
michael@0 | 137 | p = create('<i>O</i><b>P</b>'); |
michael@0 | 138 | select(p,1,p,1); |
michael@0 | 139 | insertClone(node); |
michael@0 | 140 | check(p,1,p,2); |
michael@0 | 141 | |
michael@0 | 142 | p = create('<i>Q</i><b>R</b>'); |
michael@0 | 143 | select(p,2,p,2); |
michael@0 | 144 | insertClone(node); |
michael@0 | 145 | check(p,2,p,3); |
michael@0 | 146 | |
michael@0 | 147 | p = create('<i>S</i><b>T</b>'); |
michael@0 | 148 | select(p,1,p,1); |
michael@0 | 149 | insertCloneAtEnd(node); |
michael@0 | 150 | check(p,1,p,1); |
michael@0 | 151 | |
michael@0 | 152 | p = create('<i>U</i><b>V</b>'); |
michael@0 | 153 | select(p,2,p,2); |
michael@0 | 154 | insertCloneAtEnd(node); |
michael@0 | 155 | check(p,2,p,2); |
michael@0 | 156 | |
michael@0 | 157 | p = create('<i>X</i><b>Y</b>'); |
michael@0 | 158 | select(p,0,p,1); |
michael@0 | 159 | insertCloneAtEnd(node); |
michael@0 | 160 | check(p,0,p,1); |
michael@0 | 161 | |
michael@0 | 162 | p = create('<i>X</i><b><s>Y</s></b>'); |
michael@0 | 163 | select(p,0,p.childNodes[1],1); |
michael@0 | 164 | insertCloneAtEnd(node); |
michael@0 | 165 | check(p,0,p.childNodes[1],1); |
michael@0 | 166 | |
michael@0 | 167 | p = create('<i>Z</i><b></b>'); |
michael@0 | 168 | select(p,0,p.childNodes[1],0); |
michael@0 | 169 | insertCloneAtEnd(node); |
michael@0 | 170 | check(p,0,p.childNodes[1],0); |
michael@0 | 171 | |
michael@0 | 172 | p = create('<i>ZA</i><b><s>ZB</s><u>ZC</u></b>'); |
michael@0 | 173 | select(p,0,p.childNodes[1],1); |
michael@0 | 174 | insertCloneAtEnd(node); |
michael@0 | 175 | check(p,0,p.childNodes[1],1); |
michael@0 | 176 | } |
michael@0 | 177 | function testInvalidNodeType(node) { |
michael@0 | 178 | try { |
michael@0 | 179 | testInsertNode(node); |
michael@0 | 180 | ok(false,"Expected an InvalidNodeTypeError"); |
michael@0 | 181 | } catch(e) { |
michael@0 | 182 | is(e.name, "InvalidNodeTypeError", "Wrong exception, expected InvalidNodeTypeError"); |
michael@0 | 183 | ok(e instanceof DOMException, "Wrong type of exception: " + e); |
michael@0 | 184 | is(e.code, DOMException.INVALID_NODE_TYPE_ERR, "Wrong exception code, expected INVALID_NODE_TYPE_ERR"); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | function runTest() { |
michael@0 | 189 | testInsertNode(document.createTextNode('123')); |
michael@0 | 190 | |
michael@0 | 191 | var i = document.createElement('SPAN') |
michael@0 | 192 | i.innerHTML='456' |
michael@0 | 193 | testInsertNode(i); |
michael@0 | 194 | |
michael@0 | 195 | i = document.createDocumentFragment(); |
michael@0 | 196 | i.appendChild(document.createTextNode('789')); |
michael@0 | 197 | testInsertNode(i); |
michael@0 | 198 | |
michael@0 | 199 | /// DOM2 Traversal and Range Specification 2.13 "insertNode": |
michael@0 | 200 | /// RangeException INVALID_NODE_TYPE_ERR: Raised if newNode is an Attr, Entity, Notation, or Document node. |
michael@0 | 201 | // BUG: testInvalidNodeType(document.createAttribute('a')); |
michael@0 | 202 | todo(false, "Test insertion of Entity node into range"); |
michael@0 | 203 | // TODO: testInvalidNodeType(document.createEntity()); |
michael@0 | 204 | todo(false, "Test insertion of Notation node into range"); |
michael@0 | 205 | // TODO: testInvalidNodeType(document.createNotation()); |
michael@0 | 206 | // BUG: testInvalidNodeType(document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null)); |
michael@0 | 207 | |
michael@0 | 208 | // Intentionally fails because of bug 418755. |
michael@0 | 209 | todo(false, "test that Range::insertNode() throws WrongDocumentError when it should"); |
michael@0 | 210 | i = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null).createElement('html'); |
michael@0 | 211 | try { |
michael@0 | 212 | testInsertNode(i); |
michael@0 | 213 | todo(false,"Expected a WrongDocumentError"); |
michael@0 | 214 | } catch(e) { |
michael@0 | 215 | is(e.name, "WrongDocumentError", "Wrong exception, expected WrongDocumentError"); |
michael@0 | 216 | ok(e instanceof DOMException, "Wrong type of exception: " + e); |
michael@0 | 217 | is(e.code, DOMException.WRONG_DOCUMENT_ERR, "Wrong exception code, expected WRONG_DOCUMENT_ERR"); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | // Inserting an ancestor of the start container should throw HierarchyRequestError |
michael@0 | 221 | todo(false, "test that Range::insertNode() throws HierarchyRequestError when it should"); |
michael@0 | 222 | var p = create('<b>IJK</b>'); |
michael@0 | 223 | select(p.childNodes[0],0,p.childNodes[0],1); |
michael@0 | 224 | var sel = getSelection(); |
michael@0 | 225 | var range = sel.getRangeAt(0); |
michael@0 | 226 | try { |
michael@0 | 227 | range.insertNode(p); |
michael@0 | 228 | ok(false,"Expected a HierarchyRequestError"); |
michael@0 | 229 | } catch(e) { |
michael@0 | 230 | is(e.name, "HierarchyRequestError", "Wrong exception, expected HierarchyRequestError"); |
michael@0 | 231 | ok(e instanceof DOMException, "Wrong type of exception: " + e); |
michael@0 | 232 | is(e.code, DOMException.HIERARCHY_REQUEST_ERR, "Wrong exception code, expected HIERARCHY_REQUEST_ERR"); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // TODO: we should also have a test for: |
michael@0 | 236 | /// "HierarchyRequestError: Raised if the container of the start of the Range is of a type |
michael@0 | 237 | /// that does not allow children of the type of newNode" |
michael@0 | 238 | |
michael@0 | 239 | todo(false, "InvalidStateError test goes here..."); |
michael@0 | 240 | |
michael@0 | 241 | var sel = getSelection(); |
michael@0 | 242 | sel.removeAllRanges(); |
michael@0 | 243 | |
michael@0 | 244 | SimpleTest.finish(); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 248 | addLoadEvent(runTest); |
michael@0 | 249 | |
michael@0 | 250 | |
michael@0 | 251 | </script> |
michael@0 | 252 | </pre> |
michael@0 | 253 | </body> |
michael@0 | 254 | </html> |