Skip to content

Commit 3eee033

Browse files
committed
feat(uuid): console errors if invalid ids
1 parent 922ae81 commit 3eee033

File tree

5 files changed

+27
-54
lines changed

5 files changed

+27
-54
lines changed

src/components/AccordionItemButton.spec.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ describe('AccordionItem', () => {
5959
});
6060
});
6161

62-
it('throws on invalid uuid', () => {
63-
expect(() => {
64-
render(
65-
<Accordion>
66-
<AccordionItem uuid={UUIDS.BAD_ID}>
67-
<AccordionItemHeading>
68-
<AccordionItemButton>
69-
Hello World
70-
</AccordionItemButton>
71-
</AccordionItemHeading>
72-
</AccordionItem>
73-
</Accordion>,
74-
);
75-
}).toThrow();
76-
});
77-
7862
describe('children prop', () => {
7963
it('is respected', () => {
8064
const { getByText } = render(

src/components/AccordionItemHeading.spec.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ describe('AccordionItem', () => {
5959
});
6060
});
6161

62-
it('throws on invalid uuid', () => {
63-
expect(() => {
64-
render(
65-
<Accordion>
66-
<AccordionItem>
67-
<AccordionItemHeading id={UUIDS.BAD_ID}>
68-
<AccordionItemButton>
69-
Hello World
70-
</AccordionItemButton>
71-
</AccordionItemHeading>
72-
</AccordionItem>
73-
</Accordion>,
74-
);
75-
}).toThrow();
76-
});
77-
7862
describe('children prop', () => {
7963
it('is respected', () => {
8064
const { getByText } = render(

src/components/AccordionItemPanel.spec.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ describe('AccordionItem', () => {
5454
});
5555
});
5656

57-
it('throws on invalid id', () => {
58-
expect(() => {
59-
render(
60-
<Accordion>
61-
<AccordionItem uuid={UUIDS.BAD_ID}>
62-
<AccordionItemPanel id={UUIDS.BAD_ID} />
63-
</AccordionItem>
64-
</Accordion>,
65-
);
66-
}).toThrow();
67-
});
68-
6957
describe('children prop', () => {
7058
it('is respected', () => {
7159
const { getByText } = render(

src/helpers/uuid.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nextUuid, resetNextUuid } from './uuid';
1+
import { assertValidHtmlId, nextUuid, resetNextUuid } from './uuid';
22

33
describe('UUID helper', () => {
44
describe('nextUuid', () => {
@@ -16,4 +16,17 @@ describe('UUID helper', () => {
1616
expect(nextUuid()).toBe('raa-0');
1717
});
1818
});
19+
20+
describe('assertValidHtmlId', () => {
21+
it("returns false in case there's a whitespace or an empty string", () => {
22+
expect(assertValidHtmlId('a a')).toBe(false);
23+
expect(assertValidHtmlId('a a')).toBe(false);
24+
expect(assertValidHtmlId('')).toBe(false);
25+
});
26+
27+
it('returns true on a valid id', () => {
28+
expect(assertValidHtmlId('💜')).toBe(true);
29+
expect(assertValidHtmlId('✅')).toBe(true);
30+
});
31+
});
1932
});

src/helpers/uuid.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ export function resetNextUuid(): void {
1515
counter = DEFAULT;
1616
}
1717

18-
// https://stackoverflow.com/a/14664879
19-
// but modified to allow additional first characters per HTML5
20-
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
21-
const idRegex = /^[_\-.a-zA-Z][\w:.-]*$/;
22-
23-
export function assertValidHtmlId(htmlId: string): void {
24-
if (!htmlId.toString().match(idRegex)) {
25-
throw new Error(
26-
`uuid must be a valid HTML Id but was given "${htmlId}"`,
18+
// HTML5 ids allow all unicode characters, except for ASCII whitespaces
19+
// https://infra.spec.whatwg.org/#ascii-whitespace
20+
const idRegex = /[\u0009\u000a\u000c\u000d\u0020]/g;
21+
22+
export function assertValidHtmlId(htmlId: string): boolean {
23+
if (htmlId === '' || idRegex.test(htmlId)) {
24+
// tslint:disable-next-line
25+
console.error(
26+
`uuid must be a valid HTML5 id but was given "${htmlId}", ASCII whitespaces are forbidden`,
2727
);
28+
29+
return false;
2830
}
31+
32+
return true;
2933
}

0 commit comments

Comments
 (0)