fluent.js icon indicating copy to clipboard operation
fluent.js copied to clipboard

acceptedLanguages returns empty locale strings for whitespace-only entries on main

Open poplolioi11 opened this issue 1 month ago • 0 comments

Current main returns empty locale strings for whitespace-only entries in acceptedLanguages. This is a regression from the published @fluent/[email protected], which ignores these entries.

Reproduced at 9a925d2a38b893be735ff4429be8ad62132a204d with Node.js v24.19.0 on macOS. The published 0.7.0 package is unaffected by this case.

Reproduction

In a checkout of that main commit, after npm ci --ignore-scripts and npm run build --workspaces:

const { acceptedLanguages } = require("./fluent-langneg");

console.log(acceptedLanguages("en-US, ,\t, fr"));
// Actual:   ["en-US", "", "", "fr"]
// Expected: ["en-US", "fr"]

console.log(acceptedLanguages(" ,\t, "));
// Actual:   ["", "", ""]
// Expected: []

console.log(acceptedLanguages("en;q=0.8, , fr;q=0.9,\t, de;q=0.7"));
// Actual:   ["", "", "fr", "en", "de"]
// Expected: ["fr", "en", "de"]

The empty entries get the default quality of 1, so they can precede real language tags. A consumer taking the first result can consequently receive an empty string. These are local synthetic examples; I have not established a downstream production failure.

Cause and history

The regex added in #262 matches entries containing only spaces or tabs. The captured value is trimmed before being pushed, but a trimmed empty value is not skipped. The previous implementation filtered empty values after trimming. The existing empty-entries test only covers consecutive commas.

RFC 9110 section 5.6.1.2 requires recipients to ignore a reasonable number of empty list elements; its examples include an element containing whitespace. Section 5.6.3 defines optional whitespace as SP/HTAB.

I checked the public issues and PRs, including #38 and #262, and did not find this specific regression reported.

Proposed fix and validation

Trim the language tag once and skip it when empty, before adding it to the quality-sorting array. The attached patch adds three regression tests for spaces/tabs, all-empty input, and weighted entries.

  • Before the fix: all 3 added tests failed; the 7 existing header tests passed.
  • After the fix: 737 passed, 3 skipped across 58 test files.
  • All workspace builds, lint, and documentation generation passed.
  • The built distribution returns the expected arrays above.
  • Existing quality sorting, equal-weight order, and malformed-quality tests pass.

The patch applies cleanly to the commit above. Discovery and test preparation were AI-assisted; the reported outputs were verified by local execution.

fluent-header-whitespace-fix.patch.txt

poplolioi11 avatar Oct 24 '26 04:10 poplolioi11