Skip to content

fix(title-case): don't capitalize inside HTML character references#366

Open
naveentehrpariya wants to merge 1 commit into
blakeembrey:mainfrom
naveentehrpariya:fix/title-case-html-entities
Open

fix(title-case): don't capitalize inside HTML character references#366
naveentehrpariya wants to merge 1 commit into
blakeembrey:mainfrom
naveentehrpariya:fix/title-case-html-entities

Conversation

@naveentehrpariya

Copy link
Copy Markdown

Fixes #348.

Problem

titleCase treats an HTML character reference such as & as an ordinary word and capitalizes the first letter after &:

titleCase("this & that"); // "This &Amp; That"  ❌
titleCase("a < b");        // "A ≪ B"          ❌

The mangled entity (&Amp;) is no longer valid and breaks HTML validation, with no usable workaround (the smallWords option expects an alphanumeric word, not &).

Fix

Leave whole-token HTML character references untouched — both named (&, <, …) and numeric (&, 😀). This mirrors the existing special-casing for URLs/emails/hashtags. The trailing ; is not a sentence terminator, so the surrounding words keep their normal title-case behaviour.

titleCase("this & that");   // "This & That"   ✅
titleCase("a < b > c");   // "A < B > C"   ✅
titleCase("fish & chips");      // "Fish & Chips"      (bare & unaffected)

Tests

Added cases for named, decimal and hex entities, plus a bare-& guard. The named-entity cases fail without this change; the full title-case suite (107 tests) passes with it.

`titleCase("this & that")` produced `This &Amp; That`, breaking the
entity. Title case treated `&` as a normal word and uppercased the
first letter after `&`.

Leave whole-token HTML character references (named like `&` and
numeric like `&blakeembrey#38;` / `😀`) untouched. The trailing `;` is not a
sentence terminator, so surrounding words keep their casing.

Fixes blakeembrey#348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't titlecase HTML entity character references

1 participant