Skip to content

Share namespace scope tracking with W3CDom - #2559

Merged
jhy merged 2 commits into
masterfrom
w3c-namespaces
Aug 10, 2026
Merged

jhy merged 2 commits into
masterfrom
w3c-namespaces

Conversation

@jhy

@jhy jhy commented Aug 10, 2026

Copy link
Copy Markdown
Owner

This follows the XML namespace scope optimization in #2556.

XmlTreeBuilder and W3Dom.W3CBuilder both need to track namespace bindings as elements open and close. This extracts that work into a shared NamespaceBindings helper instead of maintaining two implementations.

The helper keeps one active bindings map and records only the changes needed to restore each parent scope. This preserves the XML parser’s improved memory use and processing time while making the same scoped lookup available to the W3C converter.

W3CDom uses one tracker for source bindings and another for declarations already emitted into the W3C tree. That replaces repeated ancestor searches with direct lookups and avoids emitting unnecessary declarations.

W3C DOM fixes

This also fixes namespace conversion in several cases:

  • Namespace declarations and prefixed attributes now carry the correct namespace URI, so namespace-aware DOM lookups work as expected. This implements the prefixed-attribute handling discussed in #1839.
  • Attributes added after parsing, or included through subtree conversion, now use inherited prefix declarations.
  • Namespace declarations now apply regardless of attribute order, and an empty declaration shadows an inherited binding only within its scope.
  • With namespace awareness disabled, inherited and undeclared prefixes receive the declarations needed for XML serialization. This preserves and extends the undeclared-prefix behavior from #2087.
  • Valid HTML names that are not XML QNames are normalized before being passed to the W3C DOM. For example, a:b:c becomes a:b_c.
  • Attributes that the W3C DOM still cannot represent are skipped without dropping their containing element.
  • Elements that cannot be represented fall back to text without moving their children or following siblings outside the surrounding tree.

Extract the optimized binding tracker from XmlTreeBuilder into a shared internal helper and use it during W3C conversion.

The shared tracker removes repeated ancestor lookups and gives W3CDom reliable namespace state, making conversion both more efficient and more correct.
@jhy jhy added bug A confirmed bug, that we should fix improvement An improvement / new feature idea fixed An {bug|improvement} that has been {fixed|implemented} labels Aug 10, 2026
@jhy jhy added this to the 1.23.2 milestone Aug 10, 2026
@jhy
jhy merged commit 209151f into master Aug 10, 2026
30 of 31 checks passed
@jhy
jhy deleted the w3c-namespaces branch August 10, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A confirmed bug, that we should fix fixed An {bug|improvement} that has been {fixed|implemented} improvement An improvement / new feature idea

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant