Perf: dispatch id/class attribute mutations through the token index (#150 Tier 2)#158
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…150 Tier 2) Stacks on #157. Closes the regression flagged in that PR's risk section: a jQuery-style `element.id = 'x'` or `classList.add('x')` on an already-inserted element used to be incidentally caught by the old full-doc rescan, but the token-index dispatcher's per-added-subtree walk would miss it. Adds an opt-in `observeAttributes` flag to createSubtreeWatcher. When any subscriber asks for it, the shared MO upgrades to `attributes: true, attributeFilter: ['id', 'class']` — and the router fans attribute mutations only to subscribers that requested them, keeping the existing rules off the hot path. The token-index opts in so its dispatcher re-walks the changed element through the same findTriggeredRules path as a freshly-added subtree. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
4ba0697 to
33e072a
Compare
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.
Stacks on #157.
Summary
Closes the one behavioral regression flagged in #157's risk section: attribute mutations on already-inserted elements. Before this PR, the token-index dispatcher only walked added subtree roots, so jQuery-style
element.id = 'cookie-banner'orclassList.add('cookie-banner')after insertion was silently missed. The old code (pre-#157) caught these incidentally because every batch ran a full-doc QSA.Changes
lib/subtree-watcher.ts: opt-inobserveAttributes?: booleanoncreateSubtreeWatcher. When any subscriber asks, the sharedMutationObserverupgrades to{ childList, subtree, attributes, attributeFilter: ['id', 'class'] }. The router fans attribute mutations only to subscribers that opted in — other rules don't see them.MO.observe()re-call on the same target.lib/selector-token-index.ts: dispatcher opts in. Attribute targets flow throughfindTriggeredRulesthe same way as added subtree roots.Why opt-in (not blanket)
22 other rules use
createSubtreeWatcherdirectly (pii-redact, secrets-redact, json-ld-sanitize, etc.). Most don't benefit from re-scanning a node just because a class toggled, and pages doing animation-driven class toggling could produce MO storms. The opt-in keeps the existing fast path for those rules intact; only the selector-hide-rule dispatcher pays the attribute-observation cost.What about MO config replacement semantics
Per the DOM spec, calling
observe()on the same MO with the same target updates the registered observer's options in place — no historical record re-emission. We rely on that for both the upgrade and downgrade paths. The router holdsobservingAttributesto make the desired config recomputable from current subscribers.Test plan
bun run test— 1254 / 1254 passbun run typecheckbun run check(biome + eslint)bun run knip<div>is inserted with no id/class, page later setsdiv.id = 'late-id'ordiv.classList.add('late-class'), the rule's removeEntirely path fires on the second drain.🤖 Generated with Claude Code