Index: third_party/WebKit/LayoutTests/editing/selection/document-mutation.html |
diff --git a/third_party/WebKit/LayoutTests/editing/selection/document-mutation.html b/third_party/WebKit/LayoutTests/editing/selection/document-mutation.html |
index 7345f72787a0dfc1e741ba0ca20d5ea523a12304..6587ad267c19ff91967d1117d1637592b7908dc5 100644 |
--- a/third_party/WebKit/LayoutTests/editing/selection/document-mutation.html |
+++ b/third_party/WebKit/LayoutTests/editing/selection/document-mutation.html |
@@ -1,91 +1,85 @@ |
-<!DOCTYPE html> |
-<html> |
-<body> |
-<p>This test ensures WebKit adjusts the selection under document mutation.</p> |
-<p> Examples are from:<br> http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html#Level-2-Range-Mutation</p> |
-<div id="test" contenteditable></div> |
-<pre> |
+<!doctype html> |
+<script src="../../resources/testharness.js"></script> |
+<script src="../../resources/testharnessreport.js"></script> |
+<script src="../assert_selection.js"></script> |
<script> |
- |
-var test = document.getElementById('test'); |
-test.focus(); |
- |
-if (window.testRunner) |
- testRunner.dumpAsText(); |
- |
-var baseIsFirst = true; |
-var selection = window.getSelection(); |
- |
-function checkResult(expectedStartOffset, expectedEndOffset) { |
- var actualStartOffset = selection.getRangeAt(0).startOffset; |
- var actualEndOffset = selection.getRangeAt(0).endOffset; |
- if (actualStartOffset == expectedStartOffset && actualEndOffset == expectedEndOffset) |
- document.write('PASS: selection is (' + expectedStartOffset + ', ' + expectedEndOffset + ') "' + selection + '"\n'); |
- else |
- document.write('FAIL: selection is (' + actualStartOffset + ', ' + actualEndOffset + ') "' + selection + |
- '" and expected selection is ' + '(' + expectedStartOffset + ', ' + expectedEndOffset + ')\n'); |
+function swapMarks(string) { |
+ if (string.indexOf('^') === -1) |
+ return string; |
+ return string.replace('^', '$').replace('|', '^').replace('$', '|'); |
} |
-function setSelectionRange(startContainer, startOffset, endContainer, endOffset) { |
- if (baseIsFirst) |
- selection.setBaseAndExtent(startContainer, startOffset, endContainer, endOffset); |
- else |
- selection.setBaseAndExtent(endContainer, endOffset, startContainer, startOffset); |
+function test_selection(sample, closure, expected, description) { |
+ test(() => assert_selection(sample, closure, expected), |
+ description + '; anchor is first'); |
+ test(() => assert_selection(swapMarks(sample), closure, swapMarks(expected)), |
+ description + '; focus is first'); |
} |
-function runInsertionTest(actor, expectedStartOffset, expectedEndOffset) { |
- test.innerHTML = '<p>Abcd efgh XY blah ijkl</p>'; |
- |
- // Select "Y blah i" |
- setSelectionRange(test.firstChild.firstChild, 11, test.firstChild.firstChild, 19); |
- actor(test.firstChild.firstChild); |
- checkResult(expectedStartOffset, expectedEndOffset); |
+function doInsert(selection, offset) { |
+ const element = selection.document.querySelector('p'); |
+ element.firstChild.insertData(offset, '_'.repeat(13)); |
} |
-function runDeletionTest(actor, expectedStartOffset, expectedEndOffset) { |
- test.innerHTML = '<p>Abcd efgh The Range ijkl</p>'; |
- |
- // Select "he Range i" |
- setSelectionRange(test.firstChild.firstChild, 11, test.firstChild.firstChild, 21); |
- actor(test.firstChild.firstChild); |
- checkResult(expectedStartOffset, expectedEndOffset); |
+function doDelete(selection, offset, length) { |
+ const element = selection.document.querySelector('p'); |
+ element.firstChild.deleteData(offset, length); |
} |
-function deleteNodeContainingSelection(expectedStartOffset, expectedEndOffset) { |
- test.innerHTML = '<p>Abcd <em>efgh The Range ij</em>kl</p>'; |
- |
- // Select "he Range i" |
- setSelectionRange(test.firstChild.firstChild.nextSibling.firstChild, 6, test.firstChild.firstChild.nextSibling.firstChild, 16); |
- test.firstChild.removeChild(test.firstChild.firstChild.nextSibling); |
- checkResult(expectedStartOffset, expectedEndOffset); |
-} |
- |
-function runTests() { |
- document.write('Insertion tests:\n'); |
- runInsertionTest(function(node) { node.insertData(10, 'inserted text'); }, 24, 32); |
- runInsertionTest(function(node) { node.insertData(11, 'inserted text'); }, 11, 32); |
- runInsertionTest(function(node) { node.insertData(12, 'inserted text'); }, 11, 32); |
- runInsertionTest(function(node) { node.insertData(17, 'inserted text'); }, 11, 32); |
- runInsertionTest(function(node) { node.insertData(19, 'inserted text'); }, 11, 19); |
- |
- document.write('\nDeletion tests:\n'); |
- runDeletionTest(function(node) { node.deleteData(5, 8); }, 5, 13); |
- runDeletionTest(function(node) { node.deleteData(5, 17); }, 5, 5); |
- runDeletionTest(function(node) { node.deleteData(5, 6); }, 5, 15); |
- deleteNodeContainingSelection(1, 1); |
-} |
- |
-document.write('Base is first\n\n'); |
-runTests(); |
- |
-baseIsFirst = false; |
- |
-document.write('\n\nExtent is first\n\n'); |
-runTests(); |
- |
-test.style.display = 'none'; |
-document.write('\nDONE'); |
+// Insertion tests |
+test_selection( |
+ '<p>Abcd efgh X^Y blah i|jkl</p>', |
+ selection => doInsert(selection, 10), |
+ '<p>Abcd efgh _____________X^Y blah i|jkl</p>', |
+ 'insertData at 10'); |
+ |
+test_selection( |
+ '<p>Abcd efgh X^Y blah i|jkl</p>', |
+ selection => doInsert(selection, 11), |
+ '<p>Abcd efgh X^_____________Y blah i|jkl</p>', |
+ 'insertData at 11'); |
+ |
+test_selection( |
+ '<p>Abcd efgh X^Y blah i|jkl</p>', |
+ selection => doInsert(selection, 12), |
+ '<p>Abcd efgh X^Y_____________ blah i|jkl</p>', |
+ 'insertData at 12'); |
+ |
+test_selection( |
+ '<p>Abcd efgh X^Y blah i|jkl</p>', |
+ selection => doInsert(selection, 17), |
+ '<p>Abcd efgh X^Y blah_____________ i|jkl</p>', |
+ 'insertData at 17'); |
+ |
+test_selection( |
+ '<p>Abcd efgh X^Y blah i|jkl</p>', |
+ selection => doInsert(selection, 19), |
+ '<p>Abcd efgh X^Y blah i|_____________jkl</p>', |
+ 'insertData at 19'); |
+ |
+// Deletion tests |
+test_selection( |
+ '<p>Abcd efgh T^he Range i|jkl</p>', |
+ selection => doDelete(selection, 5, 8), |
+ '<p>Abcd ^ Range i|jkl</p>', |
+ 'deleteData(5, 8)'); |
+ |
+test_selection( |
+ '<p>Abcd efgh T^he Range i|jkl</p>', |
+ selection => doDelete(selection, 5, 6), |
+ '<p>Abcd ^he Range i|jkl</p>', |
+ 'deleteData(5, 6)'); |
+ |
+test_selection( |
+ '<p>Abcd efgh T^he Range i|jkl</p>', |
+ selection => doDelete(selection, 5, 17), |
+ '<p>Abcd |kl</p>', |
+ 'deleteData(5, 17)'); |
+ |
+// Delete node containing selection |
+test_selection( |
+ '<p>Abcd <em>efgh T^he Range i|j</em>kl</p>', |
+ selection => selection.document.querySelector('em').remove(), |
+ '<p>Abcd |kl</p>', |
+ 'delete node containing selection'); |
</script> |
-</pre> |
-</body> |
-</html> |