Skip to content

Match moved elements instead of reporting them as changed - #8

Merged
birazzzz merged 1 commit into
mainfrom
fix/match-moved-text
Oct 21, 2026
Merged

birazzzz merged 1 commit into
mainfrom
fix/match-moved-text

Conversation

@birazzzz

Copy link
Copy Markdown
Collaborator

The problem

Found by using wrapguard on fynepdf.com's /developers page (fynepdf-frontend#298). Element keys are structural (main > section[9] > h2), so adding a section renumbered every section after it, and check compared the new section's text with the old next section:

✗ 5 pieces of text now wrap differently on 1 page.

None of them did.

The fix

check now decides which saved element each scanned element is compared with, in this order (pairElements in src/diff.ts):

  1. Named elements (data-wrapguard or the element's own id) by name
  2. Everything else in page order by text, as a longest common subsequence, the way a text diff lines up lines. Insertions and removals shift nothing, and repeated text ("Learn more" ×3) pairs in order
  3. Same text out of order: it moved past other content
  4. Same key, new text: a copy edit, compared with what it replaced (the main use case, unchanged)

Whatever is left is new or gone. Text that only moved is listed as "Moved, same text" with --verbose and in the Markdown details, counted in the summary line only when there is any, and never fails the check. Very large pages (over 16M old × new elements) skip step 2 and fall back to steps 3 and 4.

Checked

  • 45 tests, 7 new in test/diff.test.ts: section inserted above, moved and rewrapped (still a failure), repeated text after an insertion, section removed from the middle, copy edit in place, named key kept when another element had its new text, text jumping past other content
  • The real case: the same /developers before-snapshot against the page with the new section now gives ✓ No wrapping changes, with 7 new and 11 moved (the call-to-action, one section down)
  • Output without moves is unchanged, so the README demo is still accurate
  • README: how matching works, under "Naming elements"; the old "names follow page structure" limit replaced by the one that's left (a copy edit and a move in the same commit)

Not released. This would go out as 0.2.0 (new moved field in the JSON output and DiffResult).

Element keys are structural, so adding a section renumbered everything after
it and check compared the new section's text with the old next section. On
fynepdf.com/developers that gave 5 false "wraps differently" results.

check now pairs elements in this order: named keys (data-wrapguard or own
id) by name; the rest in page order by text, as a longest common
subsequence, so insertions and removals shift nothing and repeated text
pairs in order; leftover same text out of order as moved; same key with new
text as a copy edit. Text that only moved is listed under "Moved, same text"
with --verbose and in the Markdown details, and never fails the check.

The same /developers case now reports 7 new, 11 moved, no wrapping changes.
@birazzzz
birazzzz merged commit 5096ec8 into main Oct 21, 2026
3 checks passed
@birazzzz birazzzz mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant