Skip to content

Commit 17aa594

Browse files
Fix document.caretPositionFromPoint().offset for textarea elements (#55033)
It assumed a <textarea> contained only a single Text node. It's not true since the "TextareaLineEndingsAsBr" feature. We need to call IndexForPosition(). Bug: 446475645 Change-Id: I215df0a3434de69750a15b834ef048e26535151e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6977829 Auto-Submit: Kent Tamura <[email protected]> Reviewed-by: Koji Ishii <[email protected]> Commit-Queue: Koji Ishii <[email protected]> Cr-Commit-Position: refs/heads/main@{#1519798}
1 parent 41d4d41 commit 17aa594

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

shadow-dom/Document-caretPositionFromPoint.tentative.html

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
<link rel="help" href="https://www.w3.org/TR/cssom-view-1/#dom-document-caretpositionfrompoint">
77
<script src="/resources/testharness.js"></script>
88
<script src="/resources/testharnessreport.js"></script>
9+
<link rel="stylesheet" href="/fonts/ahem.css">
10+
<style>
11+
textarea {
12+
font: 20px/1 Ahem;
13+
border: none;
14+
padding: 0;
15+
}
16+
</style>
17+
918
<div id="container"></div>
1019
<script>
1120

@@ -44,12 +53,13 @@
4453
assert_equals(caretPosition.offset, 0);
4554
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to an input element which is the offsetNode.");
4655

47-
test(() => {
48-
container.setHTMLUnsafe(`<textarea rows="2" cols="4">12345678901234567890</textarea>`);
56+
promise_test(async () => {
57+
container.setHTMLUnsafe(`<textarea rows="3" cols="4">12345678\n901234567890</textarea>`);
58+
await document.fonts.ready;
4959
const rect = container.firstChild.getBoundingClientRect();
5060
// Get x and y coordinate at "1234|5678..."
5161
const x = rect.left + 1;
52-
const y = rect.top + rect.height * 0.75;
62+
const y = rect.top + rect.height * 0.5;
5363
const caretPosition = document.caretPositionFromPoint(x, y);
5464
assert_true(caretPosition instanceof CaretPosition);
5565
assert_true(caretPosition.offsetNode instanceof Node);
@@ -58,6 +68,25 @@
5868
assert_equals(caretPosition.offset, 4);
5969
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to a textarea element which is the offsetNode.");
6070

71+
promise_test(async () => {
72+
container.setHTMLUnsafe(`<textarea rows="3" cols="4">12345678\n901234567890</textarea>`);
73+
await document.fonts.ready;
74+
const element = container.firstChild;
75+
const rect = element.getBoundingClientRect();
76+
const fontSize = parseInt(getComputedStyle(element).fontSize.match(/\d+/)[0]);
77+
78+
// Check a position after a forced break.
79+
const caretPosition = document.caretPositionFromPoint(
80+
rect.left + fontSize + 2, rect.bottom - fontSize / 2);
81+
// caretPosition should point between '9' and '0'.
82+
assert_equals(caretPosition.offsetNode, element);
83+
assert_equals(caretPosition.offset, 10, 'offset');
84+
const caretRect = caretPosition.getClientRect();
85+
assert_equals(caretRect.left, rect.left + fontSize, 'caretRect.left');
86+
assert_greater_than(caretRect.bottom, rect.top + rect.height * 0.9,
87+
'caretRect.bottom');
88+
}, "document.caretPositionFromPoint() for a point after a forced break should return a CaretPosition at the specified location pointing to a textarea element which is the offsetNode.");
89+
6190
test(() => {
6291
container.setHTMLUnsafe(`a<div id="host"></div>b`);
6392
const shadowRoot = host.attachShadow({mode: 'closed'});

0 commit comments

Comments
 (0)