refactor(reading): single data_hex word identity, hash the token (#237)#244
Merged
Conversation
Collapse the reading view's dual word-identity (a `TERM<hex>` CSS class *and*
a `data_hex` attribute carrying the same value) down to `data_hex` alone, and
replace the 2011 `¤`/hex `toClassName()` encoder with a short SHA-256 hash.
Why:
- `toClassName()` iterated per character (`mb_substr`) but tested per byte
(`ord`), so the `¤`/165 unambiguity scheme it was built around never held;
PHP 8.5 surfaced the smell by deprecating `ord()` on multi-byte strings.
- The token was carried twice (class + attribute), and the JS `TERM([a-f0-9]+)`
extractors mis-handled tokens containing `g-z`/`G-Z`/`¤`, silently falling
back to `data_hex`.
A `substr(hash('sha256', $s), 0, 16)` token is pure `[0-9a-f]`: selector-safe
(no `CSS.escape`), correct-by-construction for the occurrence-lookup regexes,
and recomputed identically on the PHP and JS sides — so the API `hex` field
keeps its exact role (no wire-format change) and styling is unaffected (the
`TERM` class had no CSS rules).
Changes:
- StringUtils::toClassName() -> 16-char sha256 prefix (toHex() kept).
- PHP emit (5 spans): drop `TERM` from the class, add a `data_hex` attribute in
TextReadingService (x3) and ExpressionService (x2). Server-rendered spans
previously carried the token only as the class, so this *adds* data_hex.
- JS: drop the `TERM${hex}` class push (text_renderer), convert 9 `.TERM${hex}`
selectors to `[data_hex="${hex}"]`, and drop the class-regex fallback in the
two remaining extractors (text_reader, text_keyboard).
- Tests: assert the hash shape instead of the `¤` output; migrate the frontend
fixtures/selectors; remove the obsolete class-name-fallback extractor test.
The numeric `id="TERM<woId>"` Table-Test element id is a different mechanism
and is intentionally untouched.
Gates: psalm (0), phpcs PSR12 (0), phpunit (no new failures), tsc, eslint,
vitest (4432 passed), build:all — all green.
This was referenced Jun 30, 2026
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.
Implements #237. Collapses the reading view's dual word-identity — a
TERM<hex>CSS class and adata_hexattribute carrying the same value — down todata_hexalone, and replaces the 2011¤/hexStringUtils::toClassName()encoder with a short SHA-256 hash.Design doc:
docs-src/developer/word-identity-data-hex.md(status updated to Implemented).Why
toClassName()iterated per character (mb_substr) but tested per byte (ord), so the¤-sentinel / 165-threshold unambiguity scheme it was built around was never actually realized. PHP 8.5 surfaced the smell by deprecatingord()on a multi-byte string..TERM<hex>selectors anddata_hexreads.TERM([a-f0-9]+)extractors mis-handled tokens containingg-z/G-Z/¤and silently fell back todata_hex.A
substr(hash('sha256', $s), 0, 16)token is pure[0-9a-f]: selector-safe (noCSS.escape), correct-by-construction for the occurrence-lookup regexes, and recomputed identically on the PHP and JS sides.Why it's safe
hexfield keeps its exact role; both sides recompute the same hash fromWoTextLC.TERMclass carried zero CSS rules (verified).The only thing given up is human-readability of the token in devtools (accepted in the proposal).
What changed
StringUtils::toClassName()→ 16-char sha256 prefix (toHex()kept)TERMfrom theclass, add adata_hexattribute —TextReadingService×3,ExpressionService×2. Server-rendered spans previously carried the token only as the class, so this addsdata_hex.TERM${word.hex}class push intext_renderer.ts.TERM${hex}→[data_hex="${hex}"](word_dom_updates.ts×5,word_result_init.ts×2,text_renderer.ts×2)data_hexdirectly (text_reader.ts,text_keyboard.ts)¤output; migrate frontend fixtures/selectors; remove the now-obsolete class-name-fallback extractor testDrift from the original proposal, resolved: the doc named 4 JS extractors, but
word_actions.ts/text_events.tshad already been refactored away — only 2 remain. The staleTextProcessingTest.phpreference also doesn't exist; thetoClassNameassertions live only inIntegrationTest.php.Intentionally untouched: the numeric
id="TERM<woId>"Table-Test element id (table_review_row.php,word_result_init.ts:191) is a different mechanism, andtoHex()(independent, tested utility).Verification
./vendor/bin/psalm --threads=1— 0 errors./vendor/bin/phpcs --standard=PSR12(changed files) — 0composer test:no-coverage— no new failures (2 pre-existingApiV1Testversion-constant failures are unrelated to this PR and present onmain)npm run typecheck,npm run lint,npm test(4432 passed / 47 skipped),npm run build:all— all greenE2E note (worth a manual smoke before/after merge): open a text, change a word's status, confirm all occurrences restyle at once — including inside a multi-word expression and on the server-rendered reading page (
TextReadingServicepath), plus keyboard word-nav.