Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host: Add handling for Unicode in file/class names #1198

Merged
merged 29 commits into from
May 2, 2024

Conversation

backspace
Copy link
Contributor

@backspace backspace commented Apr 24, 2024

This fixes file creation to support characters that are valid as part of file and identifier names that were previously being stripped. It also leaves the display name alone.

screencast 2024-04-30 10-26-23

Since URL handling and display is scattered across the application, this introduces a convention for consistency:

  • if the object is a URL it’s encoded, so …/hé becomes …/h%C3%A9
  • if it’s a string (or its alias LocalPath) it’s the result of decodeURI

This does mean that conversions from URL to string need to include that, if we find that it’s not being used properly, we can look into how to enforce it more consistently, maybe with a type. But RealmPaths now only accepts URLs which should help facilitate this.

@backspace backspace added the bug Something isn't working label Apr 24, 2024
@backspace backspace self-assigned this Apr 24, 2024
Copy link

github-actions bot commented Apr 24, 2024

Test Results

605 tests   601 ✔️  11m 44s ⏱️
    1 suites      4 💤
    1 files        0

Results for commit 29deff1.

♻️ This comment has been updated with latest results.

@backspace backspace marked this pull request as ready for review April 30, 2024 19:06
@backspace backspace requested a review from a team April 30, 2024 19:07
@tintinthong
Copy link
Contributor

tintinthong commented May 1, 2024

Shall we put a default classname, when all characters are invalid, eg 123.

EDIT: Actually, I can also see how this is also confusing. Ideally, we wud just put the modal in an error state using some input validation. Im impartial atm. I think it is ok as is

Comment on lines 728 to 746
function convertToClassName(input: string) {
// \p{L}: a letter
let invalidLeadingCharactersRemoved = camelCase(
input.replace(/^[^\p{L}_$]+/u, ''),
{ pascalCase: true },
);

let className = invalidLeadingCharactersRemoved.replace(
// \p{N}: a number
/[^\p{L}\p{N}_$]+/gu,
'',
);

// make sure we don't collide with a javascript built-in object
if (typeof (globalThis as any)[className] !== 'undefined') {
className = `${className}0`;
}

return className;
Copy link
Contributor

Choose a reason for hiding this comment

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

have you considered using lodash.deburr. i think it might do the thing we want here https://lodash.com/docs/4.17.15#deburr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to preserve the accented characters etc, which that strips out. That’s also why I switched away from Lodash’s camel-casing function, because it does the same. I looked all over for something that would sanitise to produce valid identifiers but I couldn’t find a package 😞

@backspace
Copy link
Contributor Author

Shall we put a default classname, when all characters are invalid, eg 123.

EDIT: Actually, I can also see how this is also confusing. Ideally, we wud just put the modal in an error state using some input validation. Im impartial atm. I think it is ok as is

hmm well if you use 123 you get invalid code because the class name is just empty:

Boxel 2024-05-01 10-05-23

So it does seem like something to protect against, thanks 🤔

@backspace backspace merged commit 7a4b13e into main May 2, 2024
28 of 29 checks passed
@delete-merged-branch delete-merged-branch bot deleted the host/unicode-filenames-cs-6690 branch May 2, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants