steam-backlog-enforcer/web/src/test/visibleWords.ts
Krzysztof kuhy Rudnicki 13df1dd14d web: command buttons paint glyphs only
Labels, descriptions and lock reasons are spoken only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:36:41 +01:00

27 lines
1.0 KiB
TypeScript

// Textgate guard helper (textgate-rework R6): the words an element paints.
// Spoken-only text (.sr-only, [hidden]) and content the caller passes in
// `allowed` (game names, server messages) do not count. aria-hidden text
// still paints, so it counts.
const WORD_RE = /[A-Za-z]{2,}/
function spokenOnly(el: Element | null): boolean {
for (let cur = el; cur; cur = cur.parentElement) {
if (cur.classList.contains('sr-only')) return true
if (cur instanceof HTMLElement && cur.hidden) return true
}
return false
}
/** Visible text nodes holding a word of 2+ letters, minus `allowed` content. */
export function visibleWords(root: Element, allowed: readonly string[] = []): string[] {
const out: string[] = []
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
const t = n.textContent?.trim() ?? ''
if (!WORD_RE.test(t) || spokenOnly(n.parentElement) || allowed.includes(t)) continue
out.push(t)
}
return out
}