<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
function swapMarks(string) {
if (string.indexOf('^') === -1)
return string;
return string.replace('^', '$').replace('|', '^').replace('$', '|');
}
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 doInsert(selection, offset) {
const element = selection.document.querySelector('p');
element.firstChild.insertData(offset, '_'.repeat(13));
}
function doDelete(selection, offset, length) {
const element = selection.document.querySelector('p');
element.firstChild.deleteData(offset, length);
}
// 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>