Skip to content

Add getElementById and getElementsByTagName support#620

Draft
andrewiggins wants to merge 14 commits into
Shopify:mainfrom
andrewiggins:wpt-get-element-by-id
Draft

Add getElementById and getElementsByTagName support#620
andrewiggins wants to merge 14 commits into
Shopify:mainfrom
andrewiggins:wpt-get-element-by-id

Conversation

@andrewiggins

Copy link
Copy Markdown

Summary

  • add shared tree-order getElementById() lookup on Document and DocumentFragment, including reflected Element.id behavior and literal identifier matching
  • add getElementsByTagName() on its standard Document and Element hosts for common HTML, non-HTML, and wildcard descendant searches
  • fix middle-position insertBefore() sibling links and make appendChild() return its appended child
  • classify the canonical upstream ID and tag-name WPT suites, with specific deferrals for adjacent DOM gaps

Stacked dependency

This PR is stacked on #617 and its branch starts from that PR's current head, cb18d72. Because the base branch belongs to a fork, GitHub requires this PR to target main; until #617 merges, the displayed diff also includes the runner infrastructure.

Related PR #618 independently adds getElementById(). This implementation incorporates its DocumentFragment surface and shared literal traversal, while additionally handling empty identifiers, reflecting Element.id, fixing WPT-exposed tree ordering, and recording classified WPT coverage.

WPT coverage

  • Document-getElementById.html: 12 supported, 6 deferred
  • Document-getElementsByTagName.html: 7 supported, 11 deferred
  • Element-getElementsByTagName.html: 8 supported, 11 deferred

Deferred cases require concrete HTML element constructors, indexed NamedNodeMap access, an outerHTML setter, live HTMLCollection semantics, HTML qualified-name edge cases, or adjacent Node constants.

Validation

  • pnpm lint
  • pnpm type-check
  • pnpm vitest run — 20 files, 208 tests passed
  • pnpm test:wpt — 4/4 files passed with complete capability classification

Inserting before a child that already had a previous sibling updated the inserted and reference nodes, but left the previous sibling pointing at the reference node. Traversals following NEXT therefore skipped the inserted subtree even though childNodes contained it.\n\nThe Document-getElementById WPT case 'where insertion order and tree order don’t match' exposed this by inserting duplicate-ID elements between existing siblings and expecting lookup to follow tree order.
Implement the NonElementParentNode lookup on Document and DocumentFragment through a shared tree-order traversal. Reflect Element.id, match identifiers literally, preserve string coercion, and reject empty identifiers as required by the WPT coverage.\n\nClassify all Document-getElementById WPT cases, supporting the common mutation and tree-order paths while deferring cases that require adjacent HTML element, NamedNodeMap, or outerHTML support.
Expose tag-name traversal on Document and Element, matching the DOM API hosts without adding the method to DocumentFragment. Share the traversal implementation while preserving case-insensitive HTML matching, case-sensitive non-HTML matching, and wildcard lookups.
Match the DOM mutation contract by returning the child passed to appendChild. Besides enabling normal chaining, the canonical Element.getElementsByTagName WPT uses this return value while constructing its fixture.
Run the canonical Document and Element getElementsByTagName suites. Record seven supported Document cases and eight supported Element cases, while deferring HTMLCollection semantics, HTML qualified-name edge cases, and the wildcard test's adjacent Node constant dependency.\n\nExpand the release note to describe the implemented lookup behavior and its intentionally deferred conformance surface.
querySelector and querySelectorAll now accept a pre-parsed Matcher[] in
addition to string selectors. MatcherType, Combinator, Matcher, and Part
are exported from selectors.ts. getElementById and getElementsByTagName
delegate to the selector engine instead of maintaining independent
tree-walk implementations, eliminating duplicate traversal code and
fixing CSS-escaping issues for ids with special characters. HTML
tag-name matching in the selector engine is now case-insensitive per
the CSS spec.
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.

1 participant