blob: 45b642eadef758e95af2d964ba8420a9ebfa8b6b [file] [log] [blame]
<div id="container">
<p id="description"></p>
<div id="sample"></div>
</div>
<script src="../../resources/js-test.js"></script>
<script>
description('Check selection updated after normalize.');
function $(id) { return document.getElementById(id); }
var selection = window.getSelection();
var newText;
function testIt(source, expectedText)
{
var sample = $('sample');
sample.innerHTML = '';
var expectedEnd;
var expectedStart;
var sampleText = '';
var offset = 0;
var textNode = sample.appendChild(document.createTextNode(''));
var startNode;
var startOffset;
var endNode;
var endOffset;
for (var i = 0; i < source.length; ++i) {
var ch = source[i];
switch (ch) {
case '[':
expectedStart = offset;
startNode = textNode;
startOffset = textNode.length;
break;
case ']':
expectedEnd = offset;
endNode = textNode;
endOffset = textNode.length;
break;
case ' ':
textNode = sample.appendChild(document.createTextNode(''));
break;
default:
++offset;
sampleText += ch;
textNode.textContent += ch;
break;
}
}
var expectedText = sampleText.substring(expectedStart, expectedEnd);
var selection = getSelection();
selection.collapse(startNode, startOffset);
selection.extend(endNode, endOffset);
sample.normalize();
newText = sample.firstChild;
debug(source);
shouldBeFalse('selection.isCollapsed');
shouldBe('selection.anchorNode', 'newText');
shouldBe('selection.anchorOffset', expectedStart.toString());
shouldBe('selection.focusNode', 'newText');
shouldBe('selection.focusOffset', expectedEnd.toString());
shouldBeEqualToString('selection.toString()', expectedText);
debug('');
}
testIt('[foo] bar baz');
testIt('foo [bar] baz');
testIt('foo bar [baz]');
testIt('f[oo ba]r baz');
testIt('f[oo bar ba]z');
testIt('foo b[ar ba]z');
testIt('f[oo ba]z');
testIt('[foo ] baz');
testIt('[foo baz]');
testIt('foo [ baz]');
testIt('foo [bar ]');
if (window.testRunner)
$('container').outerHTML = '';
</script>