4 <p>This test ensures WebKit adjusts or clears the selection when either the start or the end container was modified.
</p>
5 <div id=
"test" contenteditable
></div>
9 var test
= document
.getElementById('test');
12 if (window
.testRunner
)
13 testRunner
.dumpAsText();
15 var baseIsFirst
= true;
17 function runSingleTest(actor
, isStart
, expectedOffset
) {
18 test
.innerHTML
= '<span>hello</span> world';
21 var selection
= window
.getSelection();
23 selection
.setBaseAndExtent(test
.firstChild
.firstChild
, 2, test
.lastChild
, 3);
25 selection
.setBaseAndExtent(test
.lastChild
, 3, test
.firstChild
.firstChild
, 2);
26 var originalOffset
= isStart
? selection
.getRangeAt(0).startOffset
: selection
.getRangeAt(0).endOffset
;
27 var action
= isStart
? actor(test
.firstChild
.firstChild
, 'startContainer') : actor(test
.lastChild
, 'endContainer');
30 if (typeof(expectedOffset
) == 'undefined') {
31 if (selection
.rangeCount
)
32 message
= 'did not clear selection';
34 message
= 'cleared selection';
37 } else if (!selection
.rangeCount
)
38 message
= 'cleared selection unexpectedly';
39 else if ((selection
.getRangeAt(0).startContainer
== selection
.baseNode
40 && selection
.getRangeAt(0).startOffset
== selection
.baseOffset
) ^ baseIsFirst
)
41 message
= 'exchanged base and extent';
43 var actualOffset
= isStart
? selection
.getRangeAt(0).startOffset
: selection
.getRangeAt(0).endOffset
;
44 message
= actualOffset
== originalOffset
? 'did not move selection' : 'moved the offset from ' +
45 originalOffset
+ ' to ' + actualOffset
;
47 if (actualOffset
!= expectedOffset
)
48 message
+= ' but expected ' + expectedOffset
;
53 document
.write((passed
? 'PASS: ' : 'FAIL: ') + action
+ ' ' + message
+ '\n');
56 function runTestPairs(actor
, expectedStartOffset
, expectedEndOffset
, skipEnd
) {
57 runSingleTest(actor
, true, expectedStartOffset
);
59 runSingleTest(actor
, false, expectedEndOffset
);
63 runTestPairs(function() { test
.removeChild(test
.firstChild
); return 'Removing the parent of startContainer'; }, 0, undefined, true);
64 runTestPairs(function(node
, nodeName
) { node
.nodeValue
= 'a'; return 'Replacing nodeValue of ' + nodeName
; }, 0, 0);
65 runTestPairs(function(node
, nodeName
) { node
.appendData(' WebKit'); return 'Appending " WebKit" to ' + nodeName
; }, 2, 3);
67 runTestPairs(function(node
, nodeName
) { node
.insertData(2, 'WebKit ');
68 return 'Inserting " WebKit" to ' + nodeName
+ ' before the end point'; }, 2, 10);
69 runTestPairs(function(node
, nodeName
) { node
.insertData(3, 'WebKit ');
70 return 'Inserting " WebKit" to ' + nodeName
+ ' after the end point'; }, 2, 3);
72 runTestPairs(function(node
, nodeName
) { node
.deleteData(1, 4);
73 return 'Removing text in ' + nodeName
+ ' containing the end point'; }, 1, 1);
74 runTestPairs(function(node
, nodeName
) { node
.deleteData(0, 3);
75 return 'Removing text in ' + nodeName
+ ' containing the start point'; }, 0, 0);
76 runTestPairs(function(node
, nodeName
) { node
.deleteData(0, 2);
77 return 'Removing 2 characters in ' + nodeName
+ ' before the end point'; }, 0, 1);
78 runTestPairs(function(node
, nodeName
) { node
.deleteData(0, 3);
79 return 'Removing 3 characters at the beginning of ' + nodeName
; }, 0, 0);
80 runTestPairs(function(node
, nodeName
) { node
.deleteData(3, 2);
81 return 'Removing text in ' + nodeName
+ ' after the end point'; }, 2, 3);
83 runTestPairs(function(node
, nodeName
) { node
.replaceData(1, 4, '1234');
84 return 'Replacing text in ' + nodeName
+ ' containing the end point'; }, 1, 1);
85 runTestPairs(function(node
, nodeName
) { node
.replaceData(0, 2, '12');
86 return 'Replacing 2 characters in ' + nodeName
+ ' by 2 characters before the end point'; }, 0, 3);
87 runTestPairs(function(node
, nodeName
) { node
.replaceData(0, 2, '1');
88 return 'Replacing 2 characters in ' + nodeName
+ ' by 1 characters before the end point'; }, 0, 2);
89 runTestPairs(function(node
, nodeName
) { node
.replaceData(3, 2, '12');
90 return 'Replacing 2 characters in ' + nodeName
+ ' by 2 characters after the end point'; }, 2, 3);
93 document
.write('Base is first:\n');
98 document
.write('\nExtent is first:\n');
101 test
.style
.display
= 'none';
103 document
.write('\nDONE');