-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathis-custom-element-name.ts
More file actions
75 lines (72 loc) · 2.48 KB
/
is-custom-element-name.ts
File metadata and controls
75 lines (72 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-disable regexp/no-obscure-range */
/**
* PotentialCustomElementName
*
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname
*
* > PotentialCustomElementName ::=
* > [a-z] (PCENChar)* '-' (PCENChar)*
* > PCENChar ::=
* > "-" | "." | [0-9] | "_" | [a-z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
* > [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
* > This uses the EBNF notation from the XML specification. [XML]
*
* ASCII-case-insensitively.
* Originally, it is not possible to define a name including ASCII upper alphas in the custom element, but it is not treated as illegal by the HTML parser.
*/
const rePCENChar = [
'-',
String.raw`\.`,
String.raw`\d`,
'_',
'[a-z]',
'\u00B7',
'[\u00C0-\u00D6]',
'[\u00D8-\u00F6]',
'[\u00F8-\u037D]',
'[\u037F-\u1FFF]',
'[\u200C-\u200D]',
'[\u203F-\u2040]',
'[\u2070-\u218F]',
'[\u2C00-\u2FEF]',
'[\u3001-\uD7FF]',
'[\uF900-\uFDCF]',
'[\uFDF0-\uFFFD]',
'[\uD800-\uDBFF][\uDC00-\uDFFF]',
].join('|')
// eslint-disable-next-line regexp/no-dupe-disjunctions, regexp/no-misleading-unicode-character, regexp/no-super-linear-backtracking, regexp/no-useless-range, regexp/prefer-character-class
const rePCEN = new RegExp(`^[a-z](?:${rePCENChar})*-(?:${rePCENChar})*$`, 'i')
/**
* Valid name of custom element
*
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
*
* > - name must match the [PotentialCustomElementName](https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname) production
* > - name must not be any of the following:
* > - `<annotation-xml>`
* > - `<color-profile>`
* > - `<font-face>`
* > - `<font-face-src>`
* > - `<font-face-uri>`
* > - `<font-face-format>`
* > - `<font-face-name>`
* > - `<missing-glyph>`
*
* ASCII-case-insensitively.
* Originally, it is not possible to define a name including ASCII upper alphas in the custom element, but it is not treated as illegal by the HTML parser.
*/
export const isCustomElementName = (tagName: string) => {
switch (tagName) {
case 'annotation-xml':
case 'color-profile':
case 'font-face':
case 'font-face-src':
case 'font-face-uri':
case 'font-face-format':
case 'font-face-name':
case 'missing-glyph': {
return false
}
}
return rePCEN.test(tagName)
}