Repository navigation
feat: an id for a removed node still read the node - #121
Merged
Merged
Conversation
Syncs Netweir 7de7b48, bringing netweir/netweir#346: Document::remove now takes a node and everything in it (template contents and shadow trees too) out for good. Their ids read as nothing: None from the Option readers, no children or attributes, "" from the serializers and text, no selector matches, and EditError::Removed from append, insert_before and attach_shadow. The old remove, out of the tree and still usable, is now the public Document::detach, and is_removed(id) says which is which. A bit per node marks them, allocated on the first remove. The C API follows the core, since a held node quietly reading after something else removed it is the Lexbor bug #12 is about: - parsec_remove is for good, as lxb_dom_node_destroy_deep; the new parsec_detach is lxb_dom_node_remove; parsec_node_is_removed is new. - PARSEC_ERR_REMOVED (14) for inserting, detaching, setting or reading attributes by index on, or querying XPath from, a removed node. The other readers give PARSEC_NO_NODE, 0, NULL, "" or false; node type, namespace and name stay, as the core's kind keeps them. - Removed nodes are left out of the XPath index's roots. Python: decompose() is for good, as selectolax's frees the node. A decomposed node reads as empty but keeps its tag; xpath() from it and inserting or copying it raise ValueError, and so does decomposing the document node, as in selectolax. remove() detaches, and so do replace_with() for the node it replaced and a move from another document for the original. unwrap(), strip_tags(), the inner_html setter and merge_text_nodes() remove what they throw away. WebAssembly: node.remove() detaches, as the DOM's ChildNode.remove() keeps the node usable. Nothing else there takes a node out. parsec-css's cascade test removes a <style>'s old text for good, which stays remove. README and ROADMAP mark #12, the depth limit and Send and Sync documents done, and with them M2. Closes #12
foothillsdigital
approved these changes
Nov 20, 2026
foothillsdigital
left a comment
Collaborator
There was a problem hiding this comment.
Checked 254e76c.
The sync. NETWEIR_COMMIT moves 0107514 → 7de7b48, which is just netweir#346. With crate names normalised, Parsec's diff matches netweir's line for line in css.rs, dom.rs, edit.rs, serialize.rs, shadow.rs, tests/wpt-selectors.rs and parsec-xpath/tests/xpath.rs.
C API. It follows the core as it should:
PARSEC_ERR_REMOVEDcomes throughedit_statusand throughelement(), soparsec_attr_at,_setand_removerefuse a removed node.parsec_attr_getreads through the gatedattrs_writtenand gives NULL, as the header says.parsec_detachrefuses a removed node rather than letting it back in.parsec_xpath_evaluaterefuses a removed context, and leaves removed nodes out of the roots. The cached index can't hold a node that's been removed since it was built, becausedoc_mutdrops it on every edit.every_status_has_a_messagenow runs to 14.
Bindings.
- In Python, only
decomposeremoves for good, as selectolax's frees the node.remove(), the move half ofput(move=True)andreplace_withusedetach, so they behave as they did before. A decomposed node can't be put back, copied or queried, andsubtreeandrootsskip it. - In wasm,
remove()isdetach, matching the DOM'sChildNode.remove().
Tests. cargo clippy --workspace --all-targets -D warnings is clean, and cargo test --workspace --exclude parsec-html-python passes (317, 0 failed). The Python suite passes against a freshly built wheel (38 passed, 1 skipped). CI's capi jobs on all three platforms and capi-checks, with tests/capi.c under ASan and UBSan and the header matching cbindgen, are green. Approving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An id for a node you'd removed still read that node as if nothing had happened. That's the Lexbor bug people hit most (lexbor#132): you hold a node, other code removes it, and your next read gets the old text. Parsec never had the use-after-free, since slots aren't reused, but it had the stale read.
Change
The sync. Netweir 7de7b48, which is netweir/netweir#346:
Document::removeis now for good. The node and everything in it, a template's contents and a shadow host's shadow tree included, read as nothing:Nonefrom the readers that return anOption, no children or attributes,""from the serializers andtext, and no selector matches.append,insert_beforeandattach_shadowreturn the newEditError::Removed, and the attribute setters leave the node alone.kindkeeps the kind, and an element keeps its name.Document::detachis the oldremove, now public: the node comes out of the tree and stays usable, so you can move it.is_removed(id)is new.C API. It follows the core, because the bug #12 describes is exactly what Lexbor users hit:
parsec_removeis for good, likelxb_dom_node_destroy_deep. The newparsec_detachdoes whatlxb_dom_node_removedoes, andparsec_node_is_removedis new too.PARSEC_ERR_REMOVED(14), with a message. You get it when you insert or detach a removed node, set an attribute on it, read one by index withparsec_attr_at, or use it as an XPath context.PARSEC_NO_NODE, 0, NULL,""or false. Node type, namespace and name don't change, as in the core'skind.Python.
decompose()is for good, since selectolax's frees the node. A decomposed node reads as empty but keeps itstagand itsis_*_nodeanswers. Callingxpath()from it, or inserting or copying it anywhere, raisesValueError. Decomposing the document node raises too, as selectolax refuses its root.remove()detaches: the node stays readable, so you can put it back withmove=True.replace_with()keeps the node it replaced, as the DOM'sreplaceWithdoes. A node moved in from another document leaves its original readable there.unwrap(),strip_tags(), theinner_htmlsetter andmerge_text_nodes()remove what they throw away.remove()is an alias ofdecompose(). Here it keeps the node, as you asked.WebAssembly.
node.remove()detaches, as the DOM'sChildNode.remove()keeps the node usable. Nothing else in the binding takes a node out.Everything else. The only other
removeof a node in the workspace is in parsec-css's cascade test, which throws a<style>'s old text away. It staysremove. bench and parsec-encoding don't take nodes out at all.README and ROADMAP now mark #12 done. Along with it, the depth limit (#10) and
Send/Syncdocuments (#11) are marked done too, both already closed, so M2 is complete.Closes #12
Tests
tests/capi.chas a newremoved_and_detachedsection. It checks every reader and edit on a removed element, text node and comment, and that a detached node can be moved. It also checksPARSEC_ERR_REMOVEDfrom XPath, and a NULL handle or bad node passed to the new calls. It passes locally under-fsanitize=address,undefined, all 82 html5lib encoding cases included.test_a_decomposed_node_reads_as_nothing,test_a_decomposed_node_cant_go_back,test_remove_and_replace_with_keep_the_nodeandtest_what_unwrap_strip_and_merge_drop_is_gone. The existing tests that moved a node now take it out withremove(). 38 passed, 1 skipped.attributes_and_editschecks that a removed node's contents still read and match, and the package test moves a removed node back in.wasm-pack test --node: 10 passed. Package test: 12 passed.cargo test --workspacepasses, and so docargo clippy -D warningson the workspace, both Python feature sets and wasm32.cargo fmt --check,cargo deny check, the header drift check andsync-from-netweir.sh --checkall pass too.