Bug 1923688 [wpt PR 48546] - Use selection focus for caret position while navigating using arrow keys, a=testonly

Automatic update from web-platform-tests
Use selection focus for caret position while navigating using arrow keys

While moving cursor up and down, the cursor should come exactly above
and below the previous position respectively. Currently the cursor
position is not correct in these scenarios since we pass
|selection_.Start()| to calculate the x position while editing, instead
we should pass |selection_.Focus()| since focus will be having the last
position of the caret. This change is Firefox compatible.

Updated some existing web_tests that were using selection modifier and
after this CL, the caret position is now changed so these web_tests
needed to adhere to the updated caret positioning.

Updated tests:
web_tests/editing/pasteboard/paste-list-002.html
web_tests/editing/pasteboard/paste-list-003.html
web_tests/editing/pasteboard/paste-list-004.html
web_tests/editing/selection/move-3875618-fix.html
web_tests/editing/inserting/insert-3786362-fix-expected.txt

Bug: 40658856
Change-Id: Id6f00889621003072d7f88bcc00d10f8318ad8f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5804495
Reviewed-by: Siye Liu <siliu@microsoft.com>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Commit-Queue: Utkarsh Pathak <utpathak@microsoft.com>
Reviewed-by: Sanket Joshi <sajos@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1368113}

--

wpt-commits: 1e0eb917a4db14710f3eefd9b7ffc5f7066fd08b
wpt-pr: 48546
This commit is contained in:
Utkarsh Pathak
2024-10-15 04:51:23 +00:00
committed by moz-wptsync-bot
parent e995674a26
commit 2fc74f51f3

View File

@@ -0,0 +1,450 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
.vertical-lr {
writing-mode: vertical-lr;
border: 1px solid black;
padding: 10px;
width: 100px;
height: 200px;
}
.vertical-rl {
writing-mode: vertical-rl;
border: 1px solid black;
padding: 10px;
width: 100px;
height: 200px;
}
</style>
</head>
<body>
<div contenteditable id="horizontal">text1<br />text2</div>
<div contenteditable class="vertical-lr" id="vertical_lr">
text1<br />
text2
</div>
<div contenteditable class="vertical-rl" id="vertical_rl">
text1<br />
text2
</div>
<script>
const horizontal = document.getElementById("horizontal");
const verticalLR = document.getElementById("vertical_lr");
const verticalRL = document.getElementById("vertical_rl");
const tests = [
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[2],
endNode: horizontal.childNodes[0],
anchorOffset: horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
),
focusOffset:
horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
) + horizontal.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in moving up horizontal div when selection was left to right with line granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[2],
endNode: horizontal.childNodes[0],
anchorOffset:
horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
) + horizontal.childNodes[2].textContent.trim().length,
focusOffset: horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in moving up horizontal div when selection was right to left with line granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[0],
endNode: horizontal.childNodes[2],
anchorOffset: horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
),
focusOffset:
horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
) + horizontal.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in moving down horizontal div when selection was left to right with line granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[0],
endNode: horizontal.childNodes[2],
anchorOffset:
horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
) + horizontal.childNodes[0].textContent.trim().length,
focusOffset: horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in moving down horizontal div when selection was right to left with line granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[2],
endNode: horizontal.childNodes[0],
anchorOffset: horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
),
focusOffset:
horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
) + horizontal.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "paragraph",
description:
"Caret position should be correct in moving up horizontal div when selection was left to right with paragraph granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[2],
endNode: horizontal.childNodes[0],
anchorOffset:
horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
) + horizontal.childNodes[2].textContent.trim().length,
focusOffset: horizontal.childNodes[2].textContent.indexOf(
horizontal.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "paragraph",
description:
"Caret position should be correct in moving up horizontal div when selection was right to left with paragraph granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[0],
endNode: horizontal.childNodes[2],
anchorOffset: horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
),
focusOffset:
horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
) + horizontal.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "paragraph",
description:
"Caret position should be correct in moving down horizontal div when selection was left to right with paragraph granularity",
},
{
elementUnderTest: horizontal,
startNode: horizontal.childNodes[0],
endNode: horizontal.childNodes[2],
anchorOffset:
horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
) + horizontal.childNodes[0].textContent.trim().length,
focusOffset: horizontal.childNodes[0].textContent.indexOf(
horizontal.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "paragraph",
description:
"Caret position should be correct in moving down horizontal div when selection was right to left with paragraph granularity",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
focusOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move right with line granularity for vertical-lr div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
focusOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move right with line granularity for vertical-lr div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
focusOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move left with line granularity for vertical-lr div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
focusOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move left with line granularity for vertical-lr div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
focusOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "paragraph",
description:
"Caret position should be correct in move right with paragraph granularity for vertical-lr div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
focusOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "paragraph",
description:
"Caret position should be correct in move right with paragraph granularity for vertical-lr div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
focusOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "paragraph",
description:
"Caret position should be correct in move left with paragraph granularity for vertical-lr div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
focusOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "paragraph",
description:
"Caret position should be correct in move left with paragraph granularity for vertical-lr div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
focusOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move left with line granularity for vertical-rl div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
focusOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move left with line granularity for vertical-rl div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
focusOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move right with line granularity for vertical-rl div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
focusOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move right with line granularity for vertical-rl div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
focusOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move left with paragraph granularity for vertical-rl div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[0],
endNode: verticalLR.childNodes[2],
anchorOffset:
verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
) + verticalLR.childNodes[0].textContent.trim().length,
focusOffset: verticalLR.childNodes[0].textContent.indexOf(
verticalLR.childNodes[0].textContent.trim()
),
direction: "forward",
granularity: "line",
description:
"Caret position should be correct in move left with paragraph granularity for vertical-rl div when selection was bottom to top",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
focusOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move right with paragraph granularity for vertical-rl div when selection was top to bottom",
},
{
elementUnderTest: verticalLR,
startNode: verticalLR.childNodes[2],
endNode: verticalLR.childNodes[0],
anchorOffset:
verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
) + verticalLR.childNodes[2].textContent.trim().length,
focusOffset: verticalLR.childNodes[2].textContent.indexOf(
verticalLR.childNodes[2].textContent.trim()
),
direction: "backward",
granularity: "line",
description:
"Caret position should be correct in move right with paragraph granularity for vertical-rl div when selection was bottom to top",
},
];
for (const testData of tests) {
test(function () {
// Get the selection object and set startNode to it.
const selection = window.getSelection();
selection.removeAllRanges();
selection.setBaseAndExtent(
testData.startNode,
testData.anchorOffset,
testData.startNode,
testData.focusOffset
);
// Modify the selection as per the direction and granularity of the test
testData.elementUnderTest.focus();
selection.modify(
"move",
testData.direction,
testData.granularity
);
// After modification, the selection should should have collapsed and should be on the endNode of the test with offset same as the initial focus offset
assert_true(selection.isCollapsed);
assert_equals(selection.focusOffset, testData.focusOffset);
assert_equals(selection.focusNode, testData.endNode);
}, testData.description);
}
</script>
</body>
</html>