tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 0615fb365e73e9435be3bc88cefad6d990d298a2
parent c61290c87180b3f8db0c06e06b340720a9c5d307
Author: James Teh <jteh@mozilla.com>
Date:   Fri, 19 Dec 2025 07:38:08 +0000

Bug 2006816 part 2: Fix CaretLineNumber returning a number 1 larger than it should when the caret is not on the first character of a line. r=eeejay

Previously, it walked back by line start from the caret until it reached the start of the container.
If the caret was after the first character on a line, the first line start it encountered was the start of the current line.
To fix this, walk forward from the start of the container to the caret instead.
This also needs to account for the case that the caret is at the end of a line or container.

Differential Revision: https://phabricator.services.mozilla.com/D276962

Diffstat:
Maccessible/basetypes/HyperTextAccessibleBase.cpp | 19++++++++++++++-----
Maccessible/tests/browser/text/browser_text_caret.js | 6+++---
2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/accessible/basetypes/HyperTextAccessibleBase.cpp b/accessible/basetypes/HyperTextAccessibleBase.cpp @@ -640,12 +640,21 @@ int32_t HyperTextAccessibleBase::CaretLineNumber() { return -1; } - TextLeafPoint firstPointInThis = TextLeafPoint(Acc(), 0); - int32_t lineNumber = 1; - for (TextLeafPoint line = point; line && firstPointInThis < line; + // Walk forward by line from the start of the container. + TextLeafPoint line = TextLeafPoint(Acc(), 0); + int32_t lineNumber = 0; + for (; line && line < point; line = line.FindBoundary(nsIAccessibleText::BOUNDARY_LINE_START, - eDirPrevious)) { - lineNumber++; + eDirNext)) { + ++lineNumber; + } + // The caret might be right at the start of a line, in which case we should + // increment the line number. We shouldn't do that if the caret is at the end + // of a line or container, though. + if (line == point && !point.mIsEndOfLineInsertionPoint && + point.mOffset < + static_cast<int32_t>(nsAccUtils::TextLength(point.mAcc))) { + ++lineNumber; } return lineNumber; diff --git a/accessible/tests/browser/text/browser_text_caret.js b/accessible/tests/browser/text/browser_text_caret.js @@ -716,7 +716,7 @@ ij let moved = waitForEvent(EVENT_TEXT_CARET_MOVED, docAcc); docAcc.caretOffset = 1; await moved; - testAttrs(docAcc, { "line-number": "1" }, true, true); + testAttrs(docAcc, { "line-number": "1" }, true); info("Moving caret to c"); const blockquote = findAccessibleChildByID(docAcc, "blockquote", [ nsIAccessibleText, @@ -760,14 +760,14 @@ ij moved = waitForEvent(EVENT_TEXT_CARET_MOVED, docAcc); docAcc.caretOffset = 5; await moved; - testAttrs(docAcc, { "line-number": "5" }, true, true); + testAttrs(docAcc, { "line-number": "5" }, true); info("moving caret to end"); moved = waitForEvent(EVENT_TEXT_CARET_MOVED, docAcc); // We end up with space at the end of the document, so use characterCount to // ensure we really move to the end. docAcc.caretOffset = docAcc.characterCount; await moved; - testAttrs(docAcc, { "line-number": "5" }, true, true); + testAttrs(docAcc, { "line-number": "5" }, true); }, { // Bug 2007033: This is currently only supported for LocalAccessible.