mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
add tests for Selection.modify
This commit is contained in:
@@ -546,14 +546,14 @@
|
||||
{
|
||||
const sel = window.getSelection();
|
||||
sel.removeAllRanges();
|
||||
|
||||
let eventCount = 0;
|
||||
let lastEvent = null;
|
||||
|
||||
document.addEventListener('selectionchange', (e) => {
|
||||
const listener = (e) => {
|
||||
eventCount++;
|
||||
lastEvent = e;
|
||||
});
|
||||
};
|
||||
document.addEventListener('selectionchange', listener);
|
||||
|
||||
const p1 = document.getElementById("p1");
|
||||
const textNode = p1.firstChild;
|
||||
@@ -563,27 +563,25 @@
|
||||
sel.extend(textNode, 10);
|
||||
sel.collapseToStart();
|
||||
sel.collapseToEnd();
|
||||
|
||||
sel.removeAllRanges();
|
||||
const range = document.createRange();
|
||||
range.setStart(textNode, 4);
|
||||
range.setEnd(textNode, 15);
|
||||
sel.addRange(range);
|
||||
|
||||
sel.removeRange(range);
|
||||
|
||||
const newRange = document.createRange();
|
||||
newRange.selectNodeContents(p1);
|
||||
sel.addRange(newRange);
|
||||
sel.removeAllRanges();
|
||||
|
||||
sel.selectAllChildren(nested);
|
||||
sel.setBaseAndExtent(textNode, 4, textNode, 15);
|
||||
|
||||
sel.collapse(textNode, 5);
|
||||
sel.extend(textNode, 10);
|
||||
sel.deleteFromDocument();
|
||||
|
||||
document.removeEventListener('selectionchange', listener);
|
||||
textNode.textContent = "The quick brown fox";
|
||||
|
||||
testing.eventually(() => {
|
||||
testing.expectEqual(14, eventCount);
|
||||
testing.expectEqual('selectionchange', lastEvent.type);
|
||||
@@ -593,3 +591,87 @@
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=modifyCharacterForward>
|
||||
{
|
||||
const sel = window.getSelection();
|
||||
const p1 = document.getElementById("p1");
|
||||
const textNode = p1.firstChild; // "The quick brown fox"
|
||||
|
||||
// Collapse to position 4 (after "The ")
|
||||
sel.collapse(textNode, 4);
|
||||
testing.expectEqual(4, sel.anchorOffset);
|
||||
|
||||
// Move forward one character
|
||||
sel.modify("move", "forward", "character");
|
||||
testing.expectEqual(5, sel.anchorOffset);
|
||||
testing.expectEqual(true, sel.isCollapsed);
|
||||
testing.expectEqual("none", sel.direction);
|
||||
|
||||
// Move forward again
|
||||
sel.modify("move", "forward", "character");
|
||||
testing.expectEqual(6, sel.anchorOffset);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=modifyWordForward>
|
||||
{
|
||||
const sel = window.getSelection();
|
||||
const p1 = document.getElementById("p1");
|
||||
const textNode = p1.firstChild; // "The quick brown fox"
|
||||
|
||||
// Collapse to start
|
||||
sel.collapse(textNode, 0);
|
||||
|
||||
// Move forward one word - should land at end of "The"
|
||||
sel.modify("move", "forward", "word");
|
||||
testing.expectEqual(3, sel.anchorOffset);
|
||||
testing.expectEqual(true, sel.isCollapsed);
|
||||
|
||||
// Move forward again - should skip space and land at end of "quick"
|
||||
sel.modify("move", "forward", "word");
|
||||
testing.expectEqual(9, sel.anchorOffset);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=modifyCharacterBackward>
|
||||
{
|
||||
const sel = window.getSelection();
|
||||
const p1 = document.getElementById("p1");
|
||||
const textNode = p1.firstChild; // "The quick brown fox"
|
||||
|
||||
// Collapse to position 6
|
||||
sel.collapse(textNode, 6);
|
||||
testing.expectEqual(6, sel.anchorOffset);
|
||||
|
||||
// Move backward one character
|
||||
sel.modify("move", "backward", "character");
|
||||
testing.expectEqual(5, sel.anchorOffset);
|
||||
testing.expectEqual(true, sel.isCollapsed);
|
||||
testing.expectEqual("none", sel.direction);
|
||||
|
||||
// Move backward again
|
||||
sel.modify("move", "backward", "character");
|
||||
testing.expectEqual(4, sel.anchorOffset);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=modifyWordBackward>
|
||||
{
|
||||
const sel = window.getSelection();
|
||||
const p1 = document.getElementById("p1");
|
||||
const textNode = p1.firstChild; // "The quick brown fox"
|
||||
|
||||
// Collapse to end of "quick" (offset 9)
|
||||
sel.collapse(textNode, 9);
|
||||
|
||||
// Move backward one word - should land at start of "quick"
|
||||
sel.modify("move", "backward", "word");
|
||||
testing.expectEqual(4, sel.anchorOffset);
|
||||
testing.expectEqual(true, sel.isCollapsed);
|
||||
|
||||
// Move backward again - should land at start of "The"
|
||||
sel.modify("move", "backward", "word");
|
||||
testing.expectEqual(0, sel.anchorOffset);
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user