Skip to content

Handle missing previous selections in filtered AddBack methods - #635

Merged
mna merged 2 commits into
PuerkitoBio:masterfrom
anishpentyala:fix/addback-without-previous
Nov 21, 2026
Merged

mna merged 2 commits into
PuerkitoBio:masterfrom
anishpentyala:fix/addback-without-previous

Conversation

@anishpentyala

Copy link
Copy Markdown
Contributor

Fixes #634.

AddBackFiltered and AddBackMatcher panic when called on a valid selection with
no previous selection, such as a document's initial selection or the result of
exhausting the selection stack with End. They dereference prevSel before
AddSelection can apply its existing nil handling. Plain AddBack already works.

Return a new selection with the current nodes unchanged when there is no
previous set to filter. End restores the original selection, consistent with
AddBack. Behavior with an existing previous selection is unchanged.

The methods were introduced in #229 for #226. This fixes the missing-stack case;
it does not change nil receiver handling discussed in #264 or the AddNodes
backing-array issue in #631.

Tests cover initial documents, exhausted stacks, standalone and empty selections,
valid and invalid selector strings, Matcher, plain AddBack controls, and existing
previous selections. All12 failing nil-dereference cases fail on unchanged
master;6 control cases pass there. Each result also checks node identities and
selection-stack behavior.

Linux, Go1.26.6:

  • Full go test ./... and go test -race ./... pass.
  • go vet ./..., changed-file gofmt and git diff --check pass.

Other operating systems and Go versions were not tested locally.

@mna

mna commented Nov 21, 2026

Copy link
Copy Markdown
Member

The fix LGTM, thanks!

@mna
mna merged commit 46b683f into PuerkitoBio:master Nov 21, 2026
6 checks passed
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.

AddBackFiltered and AddBackMatcher panic on a fresh document selection

2 participants