forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-selector.ts
51 lines (42 loc) · 1.31 KB
/
html-selector.ts
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { schema } from '@angular-devkit/core';
// As per https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
// * Without mandatory `-` as the application prefix will generally cover its inclusion
// * And an allowance for upper alpha characters
// NOTE: This should eventually be broken out into two formats: full and partial (allows for prefix)
const unicodeRanges = [
[0xC0, 0xD6],
[0xD8, 0xF6],
[0xF8, 0x37D],
[0x37F, 0x1FFF],
[0x200C, 0x200D],
[0x203F, 0x2040],
[0x2070, 0x218F],
[0x2C00, 0x2FEF],
[0x3001, 0xD7FF],
[0xF900, 0xFDCF],
[0xFDF0, 0xFFFD],
[0x10000, 0xEFFFF],
];
function isValidElementName(name: string) {
let regex = '^[a-zA-Z][';
regex += '-.0-9_a-zA-Z\\u{B7}';
for (const range of unicodeRanges) {
regex += `\\u{${range[0].toString(16)}}-\\u{${range[1].toString(16)}}`;
}
regex += ']*$';
return new RegExp(regex, 'u').test(name);
}
export const htmlSelectorFormat: schema.SchemaFormat = {
name: 'html-selector',
formatter: {
async: false,
validate: name => typeof name === 'string' && isValidElementName(name),
},
};