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>
10 var test
= document
.getElementById('test');
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');
25 document
.write('FAIL: selection is (' + actualStartOffset
+ ', ' + actualEndOffset
+ ') "' + selection
+
26 '" and expected selection is ' + '(' + expectedStartOffset
+ ', ' + expectedEndOffset
+ ')\n');
29 function setSelectionRange(startContainer
, startOffset
, endContainer
, endOffset
) {
31 selection
.setBaseAndExtent(startContainer
, startOffset
, endContainer
, endOffset
);
33 selection
.setBaseAndExtent(endContainer
, endOffset
, startContainer
, startOffset
);
36 function runInsertionTest(actor
, expectedStartOffset
, expectedEndOffset
) {
37 test
.innerHTML
= '<p>Abcd efgh XY blah ijkl</p>';
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
);
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');
83 document
.write('\n\nExtent is first\n\n');
86 test
.style
.display
= 'none';
87 document
.write('\nDONE');