Commit a5a50355 authored by Yu Han's avatar Yu Han Committed by Commit Bot

Fixed selectionEnd for TextArea with text end in newline.

Previous to this CL, hitting CTRL-A inside a TextArea
containing text that ends with a newline would cause the
selectionEnd property to be incorrect. selectionEnd returns
a number that's 1 pass the end of text.

This is because TextControl automatically inserts an BRElement
at the end when the last child is a carriage return. And the
inserted node is taken into consideration when calculating the
selection range.

The fix is to ignore this last child if it's a BRElement.

Tested with carriage return inserted into the beginning, middle and end
of text to make sure there's no regression.

Bug: 882614
Change-Id: Idc7a1f098556fc74b89f3041e3f5f35e127eed78
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838549Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Commit-Queue: Yu Han <yuzhehan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#704486}
parent c46cfe72
......@@ -408,7 +408,8 @@ unsigned TextControlElement::IndexForPosition(HTMLElement* inner_editor,
index += std::min(length, passed_position.OffsetInContainerNode());
else
index += length;
} else if (node->HasTagName(kBrTag)) {
// Disregard the last auto added placeholder BrTag.
} else if (node->HasTagName(kBrTag) && node != inner_editor->lastChild()) {
++index;
}
}
......
<!doctype html>
<meta charset=utf-8>
<title>This test selectAll in textarea.</title>
<link rel="author" title="yu.han" href="mailto:yuzhehan@chromium.org">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange">
<script src=../../../resources/testharness.js></script>
<script src=../../../resources/testharnessreport.js></script>
<div id=log></div>
<form id="form"><textarea id="form-textarea"></textarea></form>
<script>
test(function() {
var textarea = document.getElementById('form-textarea');
textarea.value = 'a\n';
textarea.focus();
document.execCommand('selectAll');
assert_equals(textarea.selectionStart, 0, 'textarea.selectionStart');
assert_equals(textarea.selectionEnd, 2, 'textarea.selectionEnd');
}, 'textarea selection index is correct when text ends in newline');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment