fix: match a language range against a longer tag at a subtag boundary - #104
Open
spokodev wants to merge 1 commit into
Open
fix: match a language range against a longer tag at a subtag boundary#104spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
A one-subtag range already matches a longer tag (`en` matches `en-US`),
but a multi-subtag range did not match a still-longer tag even when it is
an exact prefix at a `-` boundary:
language('en-US', ['en-US-x']) // [] (expected ['en-US-x'])
language('zh-Hant', ['zh-Hant-CN']) // [] (expected ['zh-Hant-CN'])
RFC 4647 §3.3.1 (Basic Filtering): a range matches a tag when it equals the
tag or equals a prefix of the tag where the next character is `-`. The
prefix branch only compared the range's full value to the tag's first
subtag (`spec.full === p.prefix`), which holds only for two-subtag tags.
Generalize that branch to the boundary-prefix check it was approximating.
It keeps the same specificity (the range is less specific than the tag) and
is a strict superset of the old condition, so no previous match changes and
`en-U` still does not match `en-US` (the `-` boundary is required).
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.
Problem
A one-subtag range matches a longer tag (
enmatchesen-US), but a multi-subtag range does not match a still-longer tag, even when it is an exact prefix of it at a-boundary:The behaviour is inconsistent across subtag depth.
Cause
RFC 4647 §3.3.1 (Basic Filtering): a range matches a tag when it equals the tag, or equals a prefix of the tag where the character following the prefix is
-.specify()modelled a tag as justprefix-suffixand compared the range's full value to the tag's first subtag:p.prefixis only the first subtag, so foren-US-xit isen, and the rangeen-USnever matches.Fix
Generalize that branch to the boundary-prefix check it was approximating — the tag begins with the range followed by
-:For any tag that the old condition matched, the tag has a suffix and
p.fullisspec.full + '-' + suffix, so the new check is a strict superset — no previously-matching pair stops matching, and the specificity is unchanged (the range is still less specific than the tag). The-boundary is required, soen-Ustill does not matchen-US.Verification
whenAcceptLanguage('en-US')case asserting['en-US-x']matches while['en-USX']and['en-GB']do not.