refactor: collapse parser text/cdata handlers, centralize value checks (9.1.0) - #492
Open
derduher wants to merge 1 commit into
Open
refactor: collapse parser text/cdata handlers, centralize value checks (9.1.0)#492derduher wants to merge 1 commit into
derduher wants to merge 1 commit into
Conversation
XMLToSitemapItemStream maintained two near-identical SAX switch statements (~167 duplicated lines); the cdata switch covered only the 8 tags patched for issue #445, so CDATA in any other tag was rejected as unhandled. Both events now route through a single handleCharData, making CDATA ordinary character data in every tag. Value checks move behind the validation seam: <loc> is validated with the same validateURL the index parser uses (adding URL-parseability), and the inlined priority/duration/rating/view-count/date bounds become shared guards in lib/validation.ts with the bounds defined once in LIMITS. validateSMIOptions runs on the same guards. Ships as 9.1.0; behavior deltas recorded in the changelog and docs/adr/0001-cdata-is-character-data.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What changed
XMLToSitemapItemStreammaintained two near-identical SAX switch statements — thetexthandler (~40 tags) and acdatahandler covering only the 8 tags patched for issue #445, duplicating ~150 lines verbatim. Both events now route through a singlehandleCharData, and the parser's inline value checks are delegated to shared guards inlib/validation.ts.<priority><![CDATA[0.5]]></priority>now parses). Previously anything outside the 8-tag list was logged as "unhandled cdata".<loc>is validated with the samevalidateURLthe sitemap index parser uses, adding a URL-parseability check on top of length + protocol. A malformed URL that passed the old inline check but failsnew URL()is now dropped with a warning.isValidPriority,isValidVideoDuration,isValidVideoRating,isValidVideoViewCount,isValidISODateinlib/validation.ts;validateSMIOptionsruns on the same guards, and the bounds (0..1,0..28800,0..5) now live once inLIMITS(lib/constants.ts). None of the new guards are added to the publicindex.tssurface.docs/adr/0001-cdata-is-character-data.md; first entry in a newCONTEXT.mdglossary ("Sitemap URL").Net −44 lines (174 insertions, 218 deletions).
Why
This is the top candidate from an architecture review of the parser hot spots: the twin handlers meant every per-tag change had to be made twice (the "add a field" recipe in CLAUDE.md silently required editing both), and the inlined URL/bounds checks contradicted the repo's "all validation in lib/validation.ts" rule while enforcing a weaker
loccheck than the index parser.Reviewer notes
locvalidation is slightly stricter, and invalid-locwarnings changed to theInvalid URL in sitemap: …form already used by the index parser. Test churn is confined to 4 message assertions.locare byte-identical to before — the parser still composes its own messages and only the checks moved.errors/errorCountsurface are intentionally untouched.<loc>keeps assign-last-wins for multi-chunk character data; accumulating at closetag was considered and deferred (see the ADR).loctag #445 set, and rejection of an unparseableloc. The existingalltags.cdata.xmlfixture still passes, so [ BUG ] CDATA cannot be used forloctag #445 behavior is intact.npm run test:fullpasses (lint, ESM+CJS builds, 387 tests with coverage thresholds, xmllint schema validation).🤖 Generated with Claude Code