Skip to content

fix(dasherize): keep acronyms intact in mid-name and trailing positions - #373

Open
Ultroncn wants to merge 2 commits into
github:mainfrom
Ultroncn:fix/dasherize-acronyms
Open

Ultroncn wants to merge 2 commits into
github:mainfrom
Ultroncn:fix/dasherize-acronyms

Conversation

@Ultroncn

Copy link
Copy Markdown

Hi! This fixes #372.

Problem: dasherize() only treated an acronym as one word when it started the identifier (URLBar -> url-bar). Acronyms in the middle or at the end were mangled:

  • CopyURLElement -> copyurl-element, so register() throws ('copyurl' is not a valid custom element name)
  • srcURL -> srcur-l, @attr URL -> data-ur-l instead of data-url as documented in docs/_guide/attrs-2.md

Fix: in src/dasherize.ts, split an acronym run from a following capitalized word first, then split a lowercase letter or digit from a following capital. Now UserIDListElement -> user-id-list-element, CopyURLElement -> copy-url-element, mySVGIcon -> my-svg-icon, srcURL -> src-url, while all existing cases (URLBar, ClipX, fooBar, …) stay the same. Added the six cases from the issue to test/dasherize.ts.

Verification: npm run build (tsc) passes, and the compiled lib/dasherize.js returns the expected output for all existing plus new cases. I could not run npm test locally because no usable Chrome is available in this environment (web-test-runner needs one), so the browser test suite will need CI to confirm — apologies for that.

Fixes #372

dasherize() only treated an acronym as one word when it started the
identifier (URLBar -> url-bar). Acronyms in the middle or at the end
were mangled: CopyURLElement -> copyurl-element (register() then
throws, as 'copyurl' is not a valid custom element name), srcURL ->
srcur-l, and @attr URL -> data-ur-l instead of data-url as documented
in docs/_guide/attrs-2.md.

Split an acronym run from a following capitalized word first, then
split a lowercase letter or digit from a following capital, so
UserIDListElement -> user-id-list-element, CopyURLElement ->
copy-url-element, mySVGIcon -> my-svg-icon and srcURL -> src-url,
while all existing cases (URLBar, ClipX, fooBar, ...) stay the same.

Fixes github#372
@Ultroncn
Ultroncn requested a review from a team as a code owner November 16, 2026 21:43
Copilot AI adaptive review requested due to automatic review settings November 16, 2026 21:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The boundary change breaks names containing non-ASCII letters or underscores, causing register() to throw for inputs such as CaféBarElement and Foo_BarElement.

1 open issue
What changed in this PR

This PR addresses #372 by updating Catalyst’s dasherize() helper to preserve acronyms in the middle and at the end of identifiers used for custom elements and attributes.

Changes:

  • Separates acronym runs from following capitalized words and adds boundaries before acronyms.
  • Adds six regression cases covering controller names and attribute names.
File Description
test/​dasherize.ts Adds six mid-name and trailing acronym cases.
src/​dasherize.ts Updates word-boundary rules and explanatory comments.

Review effort: Balanced (adaptive)

Comment thread src/dasherize.ts Outdated
Comment on lines +6 to +8
// Split a lowercase letter or digit from a following capital (`fooBar` -> `foo-Bar`,
// `srcURL` -> `src-URL`).
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this against the PR head and reproduced both regressions in Node: CaféBarElement changes from café-bar-element on main to cafébar-element, and Foo_BarElement changes from foo_-bar-element to foo_bar-element.

The proposed follow-up is to preserve those existing non-ASCII/underscore boundaries while retaining the acronym fix, with regression cases for both names alongside the existing acronym cases. Could a maintainer confirm that this bounded scope is welcome? I'll wait for confirmation before updating the implementation.

…scores

The acronym fix restricted the preceding character to [a-z0-9], which
dropped existing boundaries after non-ASCII letters and underscores:
CaféBarElement became cafébar-element and Foo_BarElement became
foo_bar-element, so register() threw after stripping -element.

Use [^A-Z] as the preceding-character class instead, so a boundary is
kept before a capital after any non-uppercase character, while acronym
runs are still split by the first rule. Adds regression cases for both
names.

This branch has not been deployed

No deployments
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.

dasherize mangles mid-name and trailing acronyms (CopyURLElement throws SyntaxError, @attr URL -> data-ur-l)

2 participants