Files
cthulhu/internal/review/document_caret_line.go
deedra edd6c560b8 Read Thunderbird message text and preserve link announcements
Thunderbird HTML messages expose readable text on containers around embedded
objects. The browse cursor previously considered only text leaves, and native
document capture omitted heading/link character counts. This skipped message
content, headings and link text. A caret event following Tab could also replace
a complete link announcement with its first character.

Capture owned hypertext child identities and native placeholder offsets on the
GLib thread, releasing native interfaces before returning. Require every child
to occupy one embedded-object character so providers exposing duplicate text
through static descendants retain their ordinary traversal.

Traverse native text ranges before and after embedded children without changing
their offset domains. Preserve character, word, line and document-boundary
navigation; normalize native caret and Braille routing positions at adjacent
embedded links. Keep the Braille cursor on the correct fragment when a parent
contributes text more than once to an assembled line.

Ignore same-input link caret echoes after focus while retaining subsequent
arrow and selection feedback. Add regression tests, extend the private Firefox
fixture, and record behavior and remaining acceptance in the parity ledger.

Validation:
- Full Go test suite with count=1; focused app/atspi/review race checks.
- go vet ./...; reader and brlapi helper builds.
- Private Firefox baseline, mixed-parent offsets and tab navigation.
- Private Chromium fragments, named links, document navigation and selection.
- Parity ledger validation and whitespace checks.

The existing Firefox tab test intermittently timed out on both an untouched
HEAD copy and the changed tree; the final run passed. The unchanged
TestRunReachesSoundStopAfterEarlierServiceStopError also failed once during
startup cancellation and passed in subsequent full-suite and race runs.

Both installed binaries were verified and the reader reloaded in its original
desktop session. The maintainer confirmed readable message text and later
working links after an earlier intermittent omission. That omission remains
untraced; repeat live acceptance and physical Braille verification remain open.
2026-10-20 10:41:38 -07:00

166 lines
5.2 KiB
Go

// Cthulhu screen reader.
// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package review
import (
"context"
"errors"
"strings"
"git.stormux.org/storm/cthulhu/internal/atspi"
)
// move_caret_line handles line and document boundaries under the cursor lock
// held by MoveCaret. Text-unit movement has its separate traversal helper.
func move_caret_object_line(ctx context.Context, reader DocumentCaretReader, doc atspi.DocumentSnapshot, place *documentPlace, indices []caretRange, current int, command DocumentCaretCommand) (DocumentCaretMove, error) {
unit := atspi.DocumentCaretLine
if command == CaretReadLine {
unit = atspi.DocumentCaretLineRemainder
}
position, same := caretRangePosition(indices, current, place.offset)
backwards := command == CaretPreviousLine || command == CaretDocumentEnd
if command == CaretDocumentStart {
position = 0
} else if command == CaretDocumentEnd {
position = len(indices) - 1
}
offset := place.offset
if !same || command == CaretDocumentStart {
offset = 0
}
if command == CaretDocumentEnd {
offset = indices[position].end - 1
}
if backwards && command != CaretDocumentEnd && !same {
position--
if position < 0 {
return DocumentCaretMove{Position: documentPosition(doc, *place)}, nil
}
offset = indices[position].end - 1
}
initialPosition := position
for attempts := 0; attempts < len(indices)+1; attempts++ {
if position < 0 || position >= len(indices) {
return DocumentCaretMove{Position: documentPosition(doc, *place)}, nil
}
node := doc.Nodes[indices[position].index]
if node.Snapshot.CharacterCount == 0 || wholeCaretNode(node.Snapshot) {
if (command == CaretNextLine || command == CaretPreviousLine) && node.Reference == place.object {
if backwards {
position--
if position >= 0 {
offset = indices[position].end - 1
}
} else {
position++
offset = 0
}
continue
}
place.object, place.offset, place.origin = node.Reference, 0, CaretNavigation
return DocumentCaretMove{Position: documentPosition(doc, *place), Moved: true}, nil
}
if offset < indices[position].start {
offset = indices[position].start
}
if command == CaretReadLine && offset >= indices[position].end {
position++
offset = 0
continue
}
if offset >= indices[position].end {
offset = indices[position].end - 1
}
piece, readErr := readCaretRange(ctx, reader, doc, indices[position], offset, unit)
if readErr != nil && !errors.Is(readErr, atspi.ErrNoDocumentCaret) {
return DocumentCaretMove{}, readErr
}
if readErr == nil && piece.End > piece.Start && strings.TrimSpace(piece.Text) != "\ufffc" {
nextOffset := offset
sameCurrent := position == initialPosition && same ||
place.origin == StructuralNavigation && position == initialPosition && indices[position].index >= current
switch command {
case CaretNextLine:
if sameCurrent {
nextOffset = piece.End
if nextOffset < piece.CharacterCount {
piece, readErr = readCaretRange(ctx, reader, doc, indices[position], nextOffset, unit)
if readErr != nil {
return DocumentCaretMove{}, readErr
}
} else {
position++
if position >= len(indices) {
return DocumentCaretMove{Position: documentPosition(doc, *place)}, nil
}
offset = 0
continue
}
}
nextOffset = piece.Start
case CaretPreviousLine:
if sameCurrent {
if piece.Start > indices[position].start {
piece, readErr = readCaretRange(ctx, reader, doc, indices[position], piece.Start-1, unit)
if readErr != nil {
return DocumentCaretMove{}, readErr
}
} else {
position--
if position < 0 {
return DocumentCaretMove{Position: documentPosition(doc, *place)}, nil
}
offset = indices[position].end - 1
continue
}
}
nextOffset = piece.Start
case CaretLineStart:
nextOffset = piece.Start
case CaretLineEnd, CaretDocumentEnd:
nextOffset = max(piece.Start, piece.End-1)
case CaretDocumentStart:
nextOffset = piece.Start
}
if piece.End <= piece.Start || strings.TrimSpace(piece.Text) == "\ufffc" {
// An embedded-object placeholder is a container, not spoken text.
if backwards {
position--
if position >= 0 {
offset = indices[position].end - 1
}
} else {
position++
offset = 0
}
continue
}
spoken := piece.Text
if command == CaretLineStart || command == CaretLineEnd {
character, charErr := readCaretRange(ctx, reader, doc, indices[position], nextOffset, atspi.DocumentCaretCharacter)
if charErr != nil {
return DocumentCaretMove{}, charErr
}
spoken = character.Text
}
place.object = node.Reference
place.offset = nextOffset
place.origin = CaretNavigation
return DocumentCaretMove{Position: documentPosition(doc, *place), Text: spoken, RawText: piece.RawText, Misspelled: piece.Misspelled, Moved: true, Start: piece.Start, End: piece.End}, nil
}
if backwards {
position--
if position >= 0 {
offset = indices[position].end - 1
}
} else {
position++
offset = 0
}
}
return DocumentCaretMove{Position: documentPosition(doc, *place)}, nil
}