Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / editing / selection / document-mutation.html
blob7345f72787a0dfc1e741ba0ca20d5ea523a12304
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <p>This test ensures WebKit adjusts the selection under document mutation.</p>
5 <p> Examples are from:<br> http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html#Level-2-Range-Mutation</p>
6 <div id="test" contenteditable></div>
7 <pre>
8 <script>
10 var test = document.getElementById('test');
11 test.focus();
13 if (window.testRunner)
14 testRunner.dumpAsText();
16 var baseIsFirst = true;
17 var selection = window.getSelection();
19 function checkResult(expectedStartOffset, expectedEndOffset) {
20 var actualStartOffset = selection.getRangeAt(0).startOffset;
21 var actualEndOffset = selection.getRangeAt(0).endOffset;
22 if (actualStartOffset == expectedStartOffset && actualEndOffset == expectedEndOffset)
23 document.write('PASS: selection is (' + expectedStartOffset + ', ' + expectedEndOffset + ') "' + selection + '"\n');
24 else
25 document.write('FAIL: selection is (' + actualStartOffset + ', ' + actualEndOffset + ') "' + selection +
26 '" and expected selection is ' + '(' + expectedStartOffset + ', ' + expectedEndOffset + ')\n');
29 function setSelectionRange(startContainer, startOffset, endContainer, endOffset) {
30 if (baseIsFirst)
31 selection.setBaseAndExtent(startContainer, startOffset, endContainer, endOffset);
32 else
33 selection.setBaseAndExtent(endContainer, endOffset, startContainer, startOffset);
36 function runInsertionTest(actor, expectedStartOffset, expectedEndOffset) {
37 test.innerHTML = '<p>Abcd efgh XY blah ijkl</p>';
39 // Select "Y blah i"
40 setSelectionRange(test.firstChild.firstChild, 11, test.firstChild.firstChild, 19);
41 actor(test.firstChild.firstChild);
42 checkResult(expectedStartOffset, expectedEndOffset);
45 function runDeletionTest(actor, expectedStartOffset, expectedEndOffset) {
46 test.innerHTML = '<p>Abcd efgh The Range ijkl</p>';
48 // Select "he Range i"
49 setSelectionRange(test.firstChild.firstChild, 11, test.firstChild.firstChild, 21);
50 actor(test.firstChild.firstChild);
51 checkResult(expectedStartOffset, expectedEndOffset);
54 function deleteNodeContainingSelection(expectedStartOffset, expectedEndOffset) {
55 test.innerHTML = '<p>Abcd <em>efgh The Range ij</em>kl</p>';
57 // Select "he Range i"
58 setSelectionRange(test.firstChild.firstChild.nextSibling.firstChild, 6, test.firstChild.firstChild.nextSibling.firstChild, 16);
59 test.firstChild.removeChild(test.firstChild.firstChild.nextSibling);
60 checkResult(expectedStartOffset, expectedEndOffset);
63 function runTests() {
64 document.write('Insertion tests:\n');
65 runInsertionTest(function(node) { node.insertData(10, 'inserted text'); }, 24, 32);
66 runInsertionTest(function(node) { node.insertData(11, 'inserted text'); }, 11, 32);
67 runInsertionTest(function(node) { node.insertData(12, 'inserted text'); }, 11, 32);
68 runInsertionTest(function(node) { node.insertData(17, 'inserted text'); }, 11, 32);
69 runInsertionTest(function(node) { node.insertData(19, 'inserted text'); }, 11, 19);
71 document.write('\nDeletion tests:\n');
72 runDeletionTest(function(node) { node.deleteData(5, 8); }, 5, 13);
73 runDeletionTest(function(node) { node.deleteData(5, 17); }, 5, 5);
74 runDeletionTest(function(node) { node.deleteData(5, 6); }, 5, 15);
75 deleteNodeContainingSelection(1, 1);
78 document.write('Base is first\n\n');
79 runTests();
81 baseIsFirst = false;
83 document.write('\n\nExtent is first\n\n');
84 runTests();
86 test.style.display = 'none';
87 document.write('\nDONE');
88 </script>
89 </pre>
90 </body>
91 </html>