Skip to content

Use valid fragment contexts in WrapHtml and WrapInnerHtml - #633

Open
FFspiky wants to merge 3 commits into
PuerkitoBio:masterfrom
FFspiky:fix/wrap-inner-non-element-nodes
Open

FFspiky wants to merge 3 commits into
PuerkitoBio:masterfrom
FFspiky:fix/wrap-inner-non-element-nodes

Conversation

@FFspiky

@FFspiky FFspiky commented Nov 17, 2026 •

Copy link
Copy Markdown

WrapHtml parses the wrapper in the selected node’s parent. When wrapping the document element, the parent is a document node. Use the existing detached-node fallback for that case. Text and comment nodes with an element parent continue to be wrapped separately, as on master.

WrapInnerHtml uses the selected node itself as the fragment context. Only element nodes can provide that context. It skips non-element nodes in a mixed selection and still wraps the contents of the selected elements.

The regression tests check the serialized DOM for mixed text, comments and elements, and that wrapping the document element keeps the original element inside the wrapper. WrapAllHtml has no changes in this diff. The first-node context used by that method is covered by #620.

On macOS arm64 with Go 1.27.4, the mixed-content WrapHtml assertion passed on master and failed on the previous PR head (2c74c08). The revised branch passed go test ./... -race -cover -shuffle=on -count=1, go vet ./..., and go build ./.... The full race suite also passed with the patch applied to master at cc872d8.

The four fork CI jobs passed the full race suite, vet and build on Linux and Windows with both Go 1.26.x and 1.27.x. These jobs checked out the PR commit 36faee23.

Codex assisted with the implementation, verification, and this description.

@FFspiky

FFspiky commented Nov 17, 2026

Copy link
Copy Markdown
Author

The test workflow is waiting for approval to run. Could a maintainer approve it when convenient? The local Go 1.27.4 race/coverage suite, vet and build passed; the upstream Go/OS matrix is still unverified.

@mna

mna commented Nov 21, 2026

Copy link
Copy Markdown
Member

Hello,

Thanks for the PR, I'm double-checking if there are other places that would need the same fix and I believe WrapHtml and WrapAllHtml would also require that guard. Would you mind making the changes there too, with similar tests? I'll merge the PR once those are covered.

Thanks,
Martin

@FFspiky FFspiky changed the title Ignore non-element nodes in WrapInnerHtml Skip non-element nodes in HTML wrapping methods Nov 22, 2026
@FFspiky

FFspiky commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Thanks, Martin. I've updated both methods in 2c74c08. WrapHtml wraps each selected element separately, and WrapAllHtml groups the elements under one wrapper. Both leave selected text and comments outside the wrapper and return the original selection.

The regression tests exercise all three wrapping methods with mixed, non-element-only and empty selections. They also cover wrapping the document element with WrapHtml. I reproduced the failures on the previous PR commit. The full suite passes locally with the race detector on Go 1.27.4, and vet and build pass as well. The changes are ready for another look.

GitHub is waiting for workflow approval on this commit.

@mna

mna commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks for the update. On closer look I need to think about this a little more, I only want to fix the case that may panic, not change the behaviour of any of those methods. It's probably ok to wrap a non-element node with the provided html, I think it's only an issue when its parent (which is used as context to parse the provided html) is not an element.

@FFspiky FFspiky changed the title Skip non-element nodes in HTML wrapping methods Use valid fragment contexts in WrapHtml and WrapInnerHtml Nov 24, 2026
@FFspiky

FFspiky commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks, Martin. You were right about wrapping text and comments. I checked the DOM on master: calling WrapHtml("<b></b>") on a mixed selection wraps the text, the comment, and the element individually. The previous PR head skipped two of those. The revised patch preserves that output and only falls back when the parent cannot serve as an element context.

WrapInnerHtml passes the selected node itself to the parser, so it still needs the element check. I also removed the selection filter and the entire WrapAllHtml change from this PR. #620 already covers the text-first case and the separate question of which context to use for element selections.

The mixed-content assertion now passes on both master and this branch. The full race suite passed on the revised branch and with the patch applied to current master.

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.

2 participants