Skip to content

Commit

Permalink
Merge e384dff into f01e83d
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig authored May 20, 2024
2 parents f01e83d + e384dff commit 7952cc3
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 7952cc3

Please sign in to comment.