Skip to content

feat: an id for a removed node still read the node - #121

Merged
jamesconwaydev merged 1 commit into
mainfrom
sync-removed-nodes
Nov 20, 2026
Merged

jamesconwaydev merged 1 commit into
mainfrom
sync-removed-nodes

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

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::remove is now for good. The node and everything in it, a template's contents and a shadow host's shadow tree included, read as nothing: None from the readers that return an Option, no children or attributes, "" from the serializers and text, and no selector matches. append, insert_before and attach_shadow return the new EditError::Removed, and the attribute setters leave the node alone. kind keeps the kind, and an element keeps its name.
  • Document::detach is the old remove, now public: the node comes out of the tree and stays usable, so you can move it. is_removed(id) is new.
  • Removed nodes are marked with one bit per node, allocated on the first remove. Parsing and matching live nodes cost nothing extra.

C API. It follows the core, because the bug #12 describes is exactly what Lexbor users hit:

  • parsec_remove is for good, like lxb_dom_node_destroy_deep. The new parsec_detach does what lxb_dom_node_remove does, and parsec_node_is_removed is new too.
  • There's a new status, 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 with parsec_attr_at, or use it as an XPath context.
  • The other readers give PARSEC_NO_NODE, 0, NULL, "" or false. Node type, namespace and name don't change, as in the core's kind.
  • The XPath index leaves removed nodes out of its roots.
  • I regenerated the header with cbindgen 0.29.4.

Python.

  • decompose() is for good, since selectolax's frees the node. A decomposed node reads as empty but keeps its tag and its is_*_node answers. Calling xpath() from it, or inserting or copying it anywhere, raises ValueError. Decomposing the document node raises too, as selectolax refuses its root.
  • remove() detaches: the node stays readable, so you can put it back with move=True.
  • replace_with() keeps the node it replaced, as the DOM's replaceWith does. A node moved in from another document leaves its original readable there.
  • unwrap(), strip_tags(), the inner_html setter and merge_text_nodes() remove what they throw away.
  • I updated the stubs and the README table of differences from selectolax. One difference to flag: in selectolax, remove() is an alias of decompose(). Here it keeps the node, as you asked.

WebAssembly. node.remove() detaches, as the DOM's ChildNode.remove() keeps the node usable. Nothing else in the binding takes a node out.

Everything else. The only other remove of a node in the workspace is in parsec-css's cascade test, which throws a <style>'s old text away. It stays remove. 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/Sync documents (#11) are marked done too, both already closed, so M2 is complete.

Closes #12

Tests

  • The core's new tests: every reader on a removed node, editing and inserting it, a parent's subtree, a detached node going back in, template contents, shadow trees and selectors.
  • tests/capi.c has a new removed_and_detached section. It checks every reader and edit on a removed element, text node and comment, and that a detached node can be moved. It also checks PARSEC_ERR_REMOVED from 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.
  • pytest: test_a_decomposed_node_reads_as_nothing, test_a_decomposed_node_cant_go_back, test_remove_and_replace_with_keep_the_node and test_what_unwrap_strip_and_merge_drop_is_gone. The existing tests that moved a node now take it out with remove(). 38 passed, 1 skipped.
  • WebAssembly: attributes_and_edits checks 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 --workspace passes, and so do cargo clippy -D warnings on the workspace, both Python feature sets and wasm32. cargo fmt --check, cargo deny check, the header drift check and sync-from-netweir.sh --check all pass too.

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 foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_REMOVED comes through edit_status and through element(), so parsec_attr_at, _set and _remove refuse a removed node. parsec_attr_get reads through the gated attrs_written and gives NULL, as the header says.
  • parsec_detach refuses a removed node rather than letting it back in.
  • parsec_xpath_evaluate refuses 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, because doc_mut drops it on every edit.
  • every_status_has_a_message now runs to 14.

Bindings.

  • In Python, only decompose removes for good, as selectolax's frees the node. remove(), the move half of put(move=True) and replace_with use detach, so they behave as they did before. A decomposed node can't be put back, copied or queried, and subtree and roots skip it.
  • In wasm, remove() is detach, matching the DOM's ChildNode.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.

@jamesconwaydev
jamesconwaydev merged commit ff26ac9 into main Nov 20, 2026
39 checks passed
@jamesconwaydev
jamesconwaydev deleted the sync-removed-nodes branch November 20, 2026 00:31
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.

Node handles that know when their node was removed

2 participants