Skip to content

Commit

Permalink
Allow all special characters in IDs, except whitespace. (#2231)
Browse files Browse the repository at this point in the history
## Summary:
This came up with the Perseus team, where they had some Unicode characters in an ID and that was throwing errors. I did some more research and learned that only whitespace is considered to be invalid, so I updated our logic around this.

Issue: WB-1704

## Test plan:
I ran the tests and they passed.

Author: jeresig

Reviewers: jandrade

Required Reviewers:

Approved By: jandrade

Checks: ✅ codecov/project, ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Lint (ubuntu-latest, 20.x), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ⏭️  Chromatic - Skip on Release PR (changesets), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ gerald, ⏭️  dependabot

Pull Request URL: #2231
  • Loading branch information
jeresig authored May 21, 2024
1 parent f01e83d commit 5dfac06
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-birds-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-core": patch
---

Allow all special characters in IDs, except whitespace.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UniqueIDFactory #constructor special characters in scope, throws 1`] = `"Invalid factory scope: invalid$%^&*("`;

exports[`UniqueIDFactory #constructor whitespace in scope, throws 1`] = `"Invalid factory scope: not valid scope"`;

exports[`UniqueIDFactory #constructor whitespace only scope, throws 1`] = `"Invalid factory scope: "`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ describe("UniqueIDFactory", () => {
expect(underTest).toThrowErrorMatchingSnapshot();
});

test("special characters in scope, throws", () => {
test("special characters in scope, should not throw", () => {
// Arrange
const scope = "invalid$%^&*(";

// Act
const underTest = () => new UniqueIDFactory(scope);

// Assert
expect(underTest).toThrowErrorMatchingSnapshot();
expect(underTest).not.toThrow();
});
});

Expand Down
11 changes: 5 additions & 6 deletions packages/wonder-blocks-core/src/util/unique-id-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ export default class UniqueIDFactory implements IIdentifierFactory {
* identifier. It does not assert that a string IS a valid identifier (for
* example, that it doesn't start with numbers). We don't need to do that
* here because all identifiers are prefixed to avoid needing that check.
*
* According to this post:
* https://stackoverflow.com/questions/70579/html-valid-id-attribute-values/31773673#31773673
* The only characters that are not allowed in an id are whitespace characters.
*/
_hasValidIdChars(value?: string | null): boolean {
if (typeof value !== "string") {
return false;
}

const invalidCharsReplaced = value.replace(/[^\d\w-]/g, "-");
return value === invalidCharsReplaced;
return typeof value === "string" ? !/\s/.test(value) : false;
}

/**
Expand Down

0 comments on commit 5dfac06

Please sign in to comment.