Skip to content

feat(bindings): Python, C and WASM couldn't see positions or XPath - #108

Merged
jamesconwaydev merged 5 commits into
mainfrom
bindings-positions-xpath
Nov 19, 2026
Merged

jamesconwaydev merged 5 commits into
mainfrom
bindings-positions-xpath

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The Python package shipped in #80 as "part of #23": source positions and XPath didn't exist yet, so it couldn't offer them. Now the core has spans (#102) and parse errors (#106), and parsec-xpath exists (#104), but none of the three bindings could reach any of it. From Python, C or JavaScript you still couldn't ask where a node was written, which errors the parser recovered from, or run //a/@href.

Change

Python (bindings/python)

  • HTMLParser(html, positions=True, errors=True). positions rather than the core's spans, since the accessors it turns on are about positions and the word reads plainly next to scripting and max_depth.
  • node.source_range, node.end_tag_range and node.attribute_range(name) give (start, end), the same pair re.Match.span() gives, in the units of what was passed in: code points into a str, bytes into bytes (even where bad bytes were read as U+FFFD). So src[start:end] is the source. line_col and error offsets use the same units. node.position is (line, column) from 1, as lxml's sourceline and editors count. parser.line_col(offset) converts any offset. All None without positions=True.
  • parser.errors is a list of ParseError records (code, offset, line, column; str() gives 2:1: unexpected-end-tag). Not SyntaxError, which is Python's own exception; these are records, not exceptions.
  • parser.xpath(query, **variables) and node.xpath(...) return what lxml returns: a list for a node-set, with elements and comments as Nodes and text and attribute values as str, else a str, float or bool. Keywords bind $variables, as in lxml. Bad queries raise XPathError, a ValueError.
  • The index is cached on the parser in a Mutex<Option<Arc<Index>>>. write() drops it under the write lock, and a query builds it under the read lock, so a query never sees an index of an older tree. Queries run detached from the interpreter, as css() does, and the RwLock rules stay as they were.

C (crates/parsec-capi), additions only:

  • Flags PARSEC_SPANS (2) and PARSEC_ERRORS (4) for parsec_parse, _with and _fragment.
  • parsec_node_span, parsec_end_span, parsec_attr_span (bool, out pointers) and parsec_line_col.
  • parsec_errors_count and parsec_errors_get, filling a parsec_syntax_error { code, code_len, offset, line, column }; code is static.
  • parsec_xpath_compile, _free, _evaluate, and a parsec_xpath_result read with _type, _count, _item (node, or an attribute as owner plus index with PARSEC_NO_ATTR for a plain node), _string, _number, _boolean, _free. The result is a copy, so it outlives the document. New status PARSEC_ERR_XPATH (13).
  • The document keeps its index in a OnceLock, which doc_mut (every changing call) clears, so const readers on many threads can share it.

WASM (bindings/wasm)

  • parse(html, { spans, errors }), node.span, endSpan and attributeSpan(name) (a Span with start, end, line, column; start and end are UTF-16 indexes, so html.slice(start, end) is the source, past the BMP too), doc.errors (ParseError[]), and doc.xpath() / node.xpath() typed as XPathResult = (Node | string)[] | string | number | boolean. Text nodes come back as Nodes, as the DOM's evaluate gives them; attributes as strings, since there's no Attr here.
  • The tree's borrow_mut() drops the index, so every edit invalidates it without each edit having to remember.
  • Size: 738,534 to 869,063 bytes (+17.7%), gzipped 308,487 to 361,421. XPath and spans are about 110 KB of that, parse errors 21 KB.

In all three, every node without a parent is a root of the index, so a query can start in a removed subtree.

Offsets in the caller's units. The core counts UTF-8 bytes. C keeps them. For non-ASCII input with spans or errors on, Python and WASM keep a copy of the text and build, on the first lookup, a table of units per 64-byte block; a lookup then reads at most 64 bytes. ASCII input, valid UTF-8 bytes, and parses without spans or errors keep nothing. Lines and columns are the core's, the column counting characters.

Docs: README (Python, C and WASM bullets and comparison rows), ROADMAP (#23 done), and both package READMEs.

Closes #23

Tests

  • pytest: 22 on CPython 3.14t (GIL off), 21 passed and 1 skipped on 3.14. Non-ASCII tests (<p>café</p><b>日本</b> and an emoji, as str, as bytes and as bytes that aren't UTF-8) check every slice equals the tag's source. New: positions, errors, lxml-shaped XPath results, XPath from nodes and after edits, and 8 threads querying while one edits.
  • tests/capi.c: new positions (with the same non-ASCII slice checks on the UTF-8), syntax errors and XPath sections, including every NULL and bad-argument path. Passes locally under ASan and UBSan; the header matches cbindgen 0.29.4 and compiles as C++.
  • wasm-pack test --node: 7 passed. tests/package.test.mjs: 9 passed, with spans (including the same non-ASCII and surrogate-pair slice checks), errors, XPath and the shipped types.
  • cargo test --workspace: 257 passed, 0 failed. clippy and cargo deny clean for the workspace and both bindings.

The core records where each node was written and parsec-xpath runs
XPath 1.0, but none of the three bindings could reach either.

- Python: HTMLParser(positions=True), Node.source_range,
  end_tag_range, attribute_range(name) and position, and
  HTMLParser.line_col. xpath() on the parser and on nodes returns what
  lxml returns, with $variables as keywords.
- C: the PARSEC_SPANS flag, parsec_node_span, parsec_end_span,
  parsec_attr_span and parsec_line_col; parsec_xpath_compile, _free,
  _evaluate and a result object. ABI additions only.
- WASM: parse(html, { spans }), node.span, endSpan, attributeSpan()
  and xpath() on documents and nodes.

Each binding builds the XPath index on the first query and drops it
on any change, so it's built once per version of the tree.
The core collects parse errors now (#106), but Python, C and WASM had
no way to ask for them or read them.

- Python: HTMLParser(errors=True) and parser.errors, a list of
  ParseError records with code, offset, line and column.
- C: the PARSEC_ERRORS flag, parsec_errors_count and
  parsec_errors_get, which fills a parsec_syntax_error.
- WASM: parse(html, { errors }) and doc.errors.

The C header's preamble now names the XPath handles in its rules.
README, ROADMAP and the Python and WASM package READMEs now list
XPath, source positions and parse errors for each binding, mark #23
done, and give the WebAssembly's new size: 868 KB, 361 KB gzipped.
Python and JavaScript got the core's UTF-8 byte offsets, so in text
past ASCII, src[start:end] and html.slice(start, end) cut in the wrong
place. Offsets now come in the units of what the caller passed:

- Python: code points into a str; bytes into bytes, even where bytes
  that weren't UTF-8 were read as U+FFFD. line_col takes the same.
- WASM: UTF-16 units into the string, past the BMP too.
- C stays in UTF-8 bytes, which is what C passes in.

Error offsets follow. For non-ASCII input with spans or errors on, the
binding keeps the text and builds, on first use, a table of units per
64-byte block, so a lookup reads 64 bytes at most. Lines and columns
are as before, the column counting characters.
@jamesconwaydev
jamesconwaydev enabled auto-merge (squash) November 19, 2026 14:57
@jamesconwaydev
jamesconwaydev merged commit be4463a into main Nov 19, 2026
39 checks passed
@jamesconwaydev
jamesconwaydev deleted the bindings-positions-xpath branch November 19, 2026 15: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.

A Python package

1 participant