Skip to content

HTML viewer: a highlight whose content was removed is restored exactly or reported as unanchored - #4152

Merged
backnotprop merged 7 commits into
mainfrom
fix/html-viewer-collapsed-range-4138
Nov 28, 2026
Merged

backnotprop merged 7 commits into
mainfrom
fix/html-viewer-collapsed-range-4138

Conversation

@backnotprop

@backnotprop backnotprop commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Carries #4138 by @HaloXie (their commit is kept as authored) and replaces what review added on top of it with exact rules. It is opened here because the fork belongs to an organization, where GitHub does not allow maintainer edits.

What was wrong. Some pages remove content from the DOM and add it back (virtualized lists, framework re-renders). When the highlighted text node is removed, or its text is rewritten, the browser collapses the highlight's live Range. The viewer still counted the collapsed Range as alive, so the highlight painted nothing, nobody was told, and the viewer never looked again.

The fix from #4138. A collapsed Range counts as lost (rangeAlive).

What happens to a lost highlight (added here). A highlight that was in place and then collapsed never moves outside where it was. It comes back on the same nodes or, for an annotation with an anchor, inside its anchor element, and otherwise stays unanchored.

  • While a text highlight without an anchor is in place, each overlay pass notes its start and end text nodes, their offsets, and the text those two nodes hold. A highlight with an anchor keeps only a mark that it was in place, with no node references.
  • Without an anchor: when it collapses, it comes back only if those same two nodes are in the page again and each still holds exactly that text. The Range is then set back to the same offsets. If the two nodes come back apart or in another order, the restore is refused: a Range across two nodes must still cover the quoted words (whitespace ignored). A two-node highlight whose quote was cut at the selection limit of 10,000 characters is never restored this way, because the cut words cannot confirm where it ends; it stays unanchored. This covers pages that detach and re-attach the same nodes: keyed reordering, a moved subtree, show/hide by removal. A selection that began on a leading space comes back exactly as it was.
  • With an anchor: when it collapses, its words are searched for only inside the anchor element. If they are not there, or the anchor element is gone, it stays unanchored. It never falls back to the whole document.
  • Otherwise it stays unanchored and is reported unanchored. The viewer never searches the whole document for it: in the "Revenue grew." / "Revenue fell." case, that search moved the highlight onto the wrong row.
  • While lost, each pass costs two isConnected reads and two string compares; when both nodes of a two-node highlight are back but the restore is refused, each pass also builds that Range and reads its text, until the bound. After 5 minutes lost, the two node references are dropped so detached DOM is not kept alive. The highlight then stays unanchored.
  • A highlight that starts or ends on an element boundary rather than in a text node has no text to check against. Once lost, it stays unanchored.
  • No change for: highlights that never collapse, element/image/region targets, and the reload path (restoreAnnotation). All of these work as on main.

Two exceptions where main's search (first occurrence) still runs, as main and a reload do:

  • A Range that collapses before the first overlay pass ever noted its place. This is only possible in the frame right after a restore, which itself just took the first occurrence. It is also how a target that was never in place (a live page still rendering) finds its text.
  • Text inside a shadow tree whose host is removed. The browser does not collapse that Range, its nodes simply leave the document, so main's behaviour is kept.

Limit, plainly. Most virtualizers destroy rows and build new ones. Those new nodes are not the highlighted nodes, so a comment without an anchor shows as unanchored while the content is away and after it comes back, until the page is reloaded. A reload restores it from its stored text with the existing restore, which takes the first occurrence of the text. Where the person sees "Unanchored": the Inbox's HTML attachment view shows the chip while the highlight is lost and clears it when it comes back. The annotate/plan page shows its chip only from a Refresh report, so a mid-session loss there shows only that the highlight is gone. That page is unchanged here.

Tests. bridgeCollapsedRange.test.ts, 18 tests, run in happy-dom against the bridge's own functions. happy-dom does not move live Range boundary points when the DOM changes, so the tests do the DOM spec's steps themselves (pre-remove, insert, replace data). The cases:

  • the same row moved (exact start/end, leading space)
  • two text nodes hidden and shown
  • two text nodes back apart, or reversed
  • a long two-node highlight with a capped quote stays unanchored when its nodes return
  • a row recreated with identical text (stays unanchored)
  • two rows sharing a word, and three identical rows
  • text rewritten in place with node.data and then written back
  • replaceChild, and a rewrite followed by a removal
  • an anchored highlight whose row is removed and later re-added inside the anchor (found there), and one whose anchor element is removed (stays unanchored)
  • targets never in place, with and without an anchor
  • the 5-minute bound
  • a highlight that never collapses (text appended to its node included)
  • an element-boundary start
  • page-patched getters

Results:

  • Against main's bridge, the 14 tests as of 3ee31b8: 11 fail. Main keeps the collapsed Range as "alive", so nothing is restored and nothing is reported. The 4 added since were not run against main.
  • Against this PR's first identity-rule head (3ee31b8): the two anchored tests and the two apart/reversed tests fail.
  • Also green: srcdoc.test.ts 144, HtmlViewer.reopenComment.test.tsx 11, htmlLiveProtocol.test.tsx 40. tsc for packages/ui is clean.

Not yet run in a real browser. The two-minute check:

  1. Save this as rows.html and open it in Plannotator (an HTML attachment in the Inbox shows the chip; plannotator annotate rows.html works for the highlights):
    <div id="l"><p id="a">Revenue grew.</p><p id="b">The quoted sentence.</p><p id="c">Revenue fell.</p></div>
  2. Comment on "Revenue" in "Revenue fell.". Then drag from the space before "quoted" to the end of "quoted" and comment on that too.
  3. In DevTools, switch the console to the viewer's iframe and run const l = document.getElementById('l'); const b = document.getElementById('b'); b.remove(); setTimeout(() => l.append(b), 1500). The " quoted" highlight disappears and returns on exactly the same characters, leading space included.
  4. Run const c = document.getElementById('c'); c.remove(); setTimeout(() => { const p = document.createElement('p'); p.textContent = 'Revenue fell.'; l.append(p); }, 1500). The "Revenue" highlight disappears, never appears on "Revenue grew.", and stays gone after the copy returns. In the Inbox view its comment shows "Unanchored".

HaloXie and others added 3 commits November 27, 2026 18:04
…tions re-anchor

Pages that detach and re-attach nodes (virtualized docs such as Feishu) collapse
the stored Range onto the still-connected parent. rangeAlive() saw connected
containers and kept the empty range, so the highlight vanished after scrolling
away and back and the text re-search never ran.
With a collapsed Range counted as dead, the re-search took the first
occurrence of the quoted words in the document: a highlight on
"Revenue" in the second of two rows moved onto the first row while its
row was scrolled away, and stayed there after it came back. A
collapsed Range marks the place its text was removed from, so look
for the same node coming back there (same name and text, nearest the
point, the quote at the same offset), or for the text node itself if
the page put it back or rewrote it in place. Until then the highlight
stays unanchored, as it does while its text is away. The search
limits are unchanged.

Adds the DOM test for the collapsed Range (it fails against main's
bridge) and the two-rows cases, registered in tests/dom-test-list.txt.
@backnotprop backnotprop added the ci:e2e Run the full browser suite (Inbox window e2e) on this PR label Nov 27, 2026
A highlight whose live Range collapses (its text node removed or its data
rewritten) now comes back only when the very same text nodes are in the
page again holding exactly the text they held while it was in place; then
the recorded offsets are set back. Otherwise it stays unanchored and is
reported so, until a reload restores it from its stored text. No search for
its words: two rows can share them, and the highlight would land on text
the person did not select.

Replaces the structural lost-place search (lostPlaceOf, lostPlaceConnected,
findAtLostPlace and findTextRange's at parameter). Anchored annotations
keep the anchor-scoped search; a Range whose nodes left the document
without collapsing keeps the search as before. Node references of a lost
highlight are dropped after five minutes.
@backnotprop backnotprop changed the title HTML viewer: a highlight comes back to its own text after its content was removed and returned HTML viewer: a highlight whose content was removed is restored exactly or reported as unanchored Nov 28, 2026
…ores

An anchored highlight that was in place and collapsed is now searched for
only inside its anchor element; when that finds nothing (or the anchor is
gone) it stays unanchored instead of falling back to the first occurrence in
the whole document. An anchored target never in place keeps the full
restore ladder.

A restore by identity is accepted only if the Range is not empty and, when
it spans two nodes, still covers the quoted words (whitespace ignored; a
capped quote as a prefix). An empty restore no longer resets the lost timer,
so the node references are still dropped after the bound.
@backnotprop
backnotprop marked this pull request as ready for review November 28, 2026 02:52
@backnotprop
backnotprop merged commit 792542f into main Nov 28, 2026
67 of 68 checks passed
backnotprop added a commit to bendrucker/plannotator that referenced this pull request Nov 28, 2026
…panel swipes and the HTML viewer's lost highlights

ui 0.67.0 and core 0.25.35 went to npm on 2026-11-28 with package files
that match the v0.29.6 tag (c21f65e), so this pair carries what merged
after v0.29.6, up to v0.29.7.

core 0.25.36 is additive: the linked mark "decisions" and
InboxDecision.machine (backnotprop#4066), DraftFileUploader.previewUrl (backnotprop#4165), and
holdingsOf no longer counting a question Mark read closed (backnotprop#4160). No
exports subpath changes; nothing removed.

ui 0.68.0 is a minor: Escape while an input method composes only ends the
composition, and shortcuts skip such keys; holdsImeEscape is new (backnotprop#3689,
backnotprop#4162). HtmlViewer treats a collapsed live Range as lost (backnotprop#4152). Compact
panels: OverlayScrollArea scrollMemory, SidebarContainer closing and
scrollPositions, ActionMenu compactEdgeSwipe, four new hooks and the panel
rules in theme.css (backnotprop#2861). useLaunchAnnouncement waits for ready (backnotprop#4067).
ui uses nothing new from core; its exact pin follows so a host gets one
core. Publish core first.

README and HANDOFF name the new pair, and HANDOFF adds what the published
0.25.35 / 0.67.0 already held beyond their notes (backnotprop#4114, backnotprop#4151).

bun.lock is bun install's own output: the two package version lines, and
six app workspace version lines (0.29.5 to 0.29.7) the release bumps left
behind.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:e2e Run the full browser suite (Inbox window e2e) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants