Skip to content

fix(rules): hidden-text applies a style rule with a comment in its selector #684 - #874

Merged
nc9 merged 2 commits into
mainfrom
fix/684-hidden-text-css-comments
Oct 17, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/684-hidden-text-css-comments

Conversation

@nc9

@nc9 nc9 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Closes #684.

What changes

content/hidden-text's rule scanner skipped CSS comments while looking for a rule's {, but kept them in the selector text. So /* Section */ .b{display:none} was not a bare class selector: it was treated as complex and only marked .b as ambiguous, and the plainly hidden text went unreported. A browser drops comments while tokenizing and applies the rule.

  • New stripComments() in packages/rules/src/shared/css-tokens.ts. It removes comments the way a tokenizer does, copying quoted strings and escapes through untouched. The rule scanner runs a style rule's prelude through it before judging the selector.
  • A comment still separates tokens. When the characters on both sides of a removed comment are name characters, a space is kept: .a + comment + b reads .a b, a complex (ambiguous) selector, never the class .ab. Codex found that case.
  • .a /* x */ .b is still a descendant selector, and [title="/* x */"].b is still complex: a /* inside a string is text.

Corpus measurement (the issue's last criterion)

The measurement ran content/hidden-text before (main's hidden-text.ts and css-tokens.ts) and after this change over the 7,061 HTML pages of the dom-classifier corpus snapshot (/Volumes/BACKUP/dom-classifier-data/2026-09-22/live-snapshot/content-store.db, read-only). Only pages with a <style> element and a /* in the HTML can change, and there are 660 of them. Results on those 660: 604 pass, 50 warn, 1 fail, 5 skipped. 0 pages changed status or item count. I re-ran it on the final code with the same result. So the fix brings back the issue's case without moving results on real pages in this corpus.

Tests

  • packages/rules/tests/hidden-text-css-comments-684.test.ts, which fails before the change. It covers:
    • the issue's repro;
    • a comment before { and inside the selector;
    • .a /* x */ .b and .a/**/.b staying ambiguous;
    • .a/**/b and #a/**/b not becoming .ab / #ab, with an #ab control;
    • a comment-like string in an attribute selector.
  • Re-ran hidden-text.test.ts (155 tests).
  • tsgo is clean for rules. test-suites.ts --check, tangly check --strict and the detect-secrets hook pass.
  • Codex (gpt-6-astra) raised one P2 (token merging), now fixed; the re-check came back clean.

Docs and skill

  • rules/content/hidden-text: says comments are dropped from selectors.
  • CHANGELOG entry under Unreleased, Fixed.
  • The squirrelscan agent skill needs no change.

…lector #684

Comments are dropped from a style rule's selector before it is judged,
as a browser's tokenizer does, so a section comment before a plain class
rule no longer makes it read as complex. A comment between two name
characters still separates them. Measured on a 7,061-page corpus: no
page's finding changed.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/684-hidden-text-css-comments branch from 6f7bd28 to 4d0f2a3 Compare October 17, 2026 14:27
@nc9
nc9 marked this pull request as ready for review October 17, 2026 14:27
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Reviewed the diff. I didn't run the tests. I found no correctness, security or performance problems; the change is small and well scoped.

  • (nit) packages/rules/src/shared/css-tokens.ts (stripComments doc comment): it says "no space is left behind", but the code inserts a space when both neighbours are name characters. The second comment in the function explains this correctly, so please update the doc comment. The line is also over-long and breaks mid-sentence.
  • (nit) stripComments is O(n) in the prelude length and returns early when there is no /*, so the cost is fine. out += per character is acceptable at the 400k char cap. Copying slices between specials would be slightly cheaper, but it isn't needed.
  • (nit) Test coverage is good. Two cases could be added: an unterminated comment in a prelude (.b /* x {display:none}, which should not apply the rule), and an escaped quote or backslash before a comment. Neither looks broken; they would only pin the behaviour.
  • (nit) The PR description cites a corpus path under /Volumes/BACKUP that reviewers can't reproduce. That's fine for a one-off measurement, but the CHANGELOG line could drop "On a 7,061-page corpus no page's finding changed", since it's not user-actionable.

@nc9

nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Review triage (4d0f2a3), fixed in 5d50167: the stripComments doc comment now says when a space is kept, and the CHANGELOG line drops the corpus sentence. Not changed: the per-character copy is linear and capped, and the unterminated-comment and escape cases go through the shared skipComment / skipString helpers, which the declaration reader uses too.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Reviewed the diff for PR 874 (stripComments in css-tokens.ts, its use in parseRules, the tests, the docs and the changelog). I found no problems that should block the merge. I read the code and tests but did not run the test suite.

  • The strip is correct. It copies strings and escapes through untouched, so a /* inside [title="/* x */"] is left alone. An unterminated comment is consumed to the end of the input, and the includes("/*") early return keeps the common case cheap.
  • Inserting a space only where a comment sits between two name characters is the safe reading. The result is a complex selector, which is only marked ambiguous and not applied.
  • (nit) packages/rules/src/shared/css-tokens.ts stripComments: out += ch builds the string one character at a time. That is fine under the 400k-character budget. Pushing slices and joining once would be a cheap tidy-up if the function gets reused on larger input.
  • (nit) packages/rules/tests/hidden-text-css-comments-684.test.ts: the new tests don't cover a comment inside an at-rule prelude (@media /* x */ (...)) or an unterminated comment in a selector. Both look handled, since stripComments runs before the startsWith("@") check, but a one-line test for each would lock that in.
  • (nit) Tests for stripComments itself, in shared/, would help if other rules start using it.

The docs and changelog entries match the behaviour.

@nc9
nc9 merged commit 6689017 into main Oct 17, 2026
15 checks passed
@nc9
nc9 deleted the fix/684-hidden-text-css-comments branch October 17, 2026 14:57
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.

content/hidden-text: a comment before a selector stops a plain hiding rule from applying

1 participant