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

Fix ID generation to ensure it produces valid CSS identifiers #3139

Merged
merged 1 commit into from
Mar 1, 2024
Merged

Fix ID generation to ensure it produces valid CSS identifiers #3139

merged 1 commit into from
Mar 1, 2024

Conversation

alrra
Copy link
Contributor

@alrra alrra commented Feb 14, 2024

Description

  • Move the logic of generating the ID into its own utility function.

  • Ensure the utility function generates IDs that are valid CSS identifiers.

    shortid.generate() by itself will generates IDs that are not always valid 1 CSS identifiers (e.g. will start with a digit). So, when other parts of the codebase will use those IDs as a selector, a SyntaxError 2 will be thrown (i.e. '#...' is not a valid selector).

    The slds- prefix is used so to keep things simple. shortid.characters() requires "a string of all 64 unique characters" 3, which means that in order to generate valid CSS identifiers a-z, A-Z, and some "ISO 10646 characters U+00A0 and higher" 1 will need to be included.


CONTRIBUTOR checklist (do not remove)

Please complete for every pull request

  • First-time contributors should sign the Contributor License Agreement. It's a fancy way of saying that you are giving away your contribution to this project. If you haven't before, wait a few minutes and a bot will comment on this pull request with instructions.
  • npm run lint:fix has been run and linting passes.
  • Mocha, Jest (Storyshots), and components/component-docs.json CI tests pass (npm test).
  • Tests have been added for new props to prevent regressions in the future. See readme.
  • Review the appropriate Storybook stories. Open http://localhost:9001/.
  • Review tests are passing in the browser. Open http://localhost:8001/.
  • Review markup conforms to SLDS by looking at DOM snapshot strings.

REVIEWER checklist (do not remove)

  • CircleCI tests pass. This includes linting, Mocha, Jest, Storyshots, and components/component-docs.json tests.
  • Tests have been added for new props to prevent regressions in the future. See readme.
  • Review the appropriate Storybook stories. Open http://localhost:9001/.
  • The Accessibility panel of each Storybook story has 0 violations (aXe). Open http://localhost:9001/.
  • Review tests are passing in the browser. Open http://localhost:8001/.
  • Review markup conforms to SLDS by looking at DOM snapshot strings.
Required only if there are markup / UX changes
  • Add year-first date and commit SHA to last-slds-markup-review in package.json and push.
  • Request a review of the deployed Heroku app by the Salesforce UX Accessibility Team.
  • Add year-first review date, and commit SHA, last-accessibility-review, to package.json and push.
  • While the contributor's branch is checked out, run npm run local-update within locally cloned site repo to confirm the site will function correctly at the next release.

Footnotes

  1. From https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier
    In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. " 2

  2. https://dom.spec.whatwg.org/#selectors

  3. https://github.com/dylang/shortid/blob/937ce2c8dd7001baec1785cb8dce6e6fe1bcf61f/README.md#shortidcharactersstring

* Move the logic of generating the ID into its own utility
  function.

* Ensure the utility function generates IDs that are valid
  CSS identifiers.

  `shortid.generate()` by itself will generates IDs that
  are not always valid¹ CSS identifiers (e.g. will start
  with a digit). So, when other parts of the codebase will
  use those IDs as a selector, a `SyntaxError`² will be thrown
  (i.e. `'#...' is not a valid selector`).

  The `slds-` prefix is used so to keep things simple.
  `shortid.characters()` requires "a string of all 64 unique
  characters"³, which means that in order to generate valid
  CSS identifiers a-z, A-Z, and some "ISO 10646 characters
  U+00A0 and higher"¹ will need to be included.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

¹ From https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier

 " In CSS, identifiers (including element names, classes, and IDs
   in selectors) can contain only the characters [a-zA-Z0-9] and
   ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and
   the underscore (_); they cannot start with a digit, two hyphens,
   or a hyphen followed by a digit. "

² https://dom.spec.whatwg.org/#selectors

³ https://github.com/dylang/shortid/blob/937ce2c8dd7001baec1785cb8dce6e6fe1bcf61f/README.md#shortidcharactersstring
Copy link

welcome bot commented Feb 14, 2024

Thanks for opening this pull request! 💯

This is a community-driven project, and we can't do it without your participation. Please check out our contributing guidelines and review the Contributor Checklist if you haven't already, to make sure everything is squared away. CircleCI will take about 10 minutes to run through the same items that are on the Contributor checklist with a pass/fail check below. Please fix any issues that cause CircleCI to fail or ask for clarification--we try, but sometimes the errors can be unclear.
A maintainer will try to respond within 7 days. If you haven’t heard anything by then, please bump this thread. To ensure codebase quality, large code line changes may take more than 2 weeks to review, but may take longer depending on the number of pull requests in the queue. Feel free to ask for a status update at any time--you won't be bothering anyone.
Once feedback has been given, please reply to the feedback giver once the feedback on been addressed, so that they can continue the review.
If you need a release while you are waiting for a code review, you can publish a built tag to your own fork. See directions in the release README.

Copy link
Contributor

@interactivellama interactivellama left a comment

Choose a reason for hiding this comment

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

@alrra Thank you for your contribution!

@interactivellama interactivellama merged commit fb71cd1 into salesforce:master Mar 1, 2024
5 checks passed
Copy link

welcome bot commented Mar 1, 2024

Congrats on merging your first pull request to Design System React! 🎉
If you have a moment, please fill out this five question survey, we would appreciate it.
On behalf of Salesforce's customers, partners, product specialists and employees, we would like offer sincere thanks and appreciation for helping make our user experience better. We look forward to working with you more in the future.
This definitely calls for a high five! Alt High Five

@alrra alrra deleted the fix-id-generation branch March 1, 2024 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants