UNPKG

1.74 kBJavaScriptView Raw
1
2let attributeMode = false;
3let tagStartMode = false;
4let specialMode = false;
5let stringMode = false;
6let stringChar = '';
7
8for (let i = 0, l = html.length; i < l; i++) {
9 let char = html[i];
10
11 if (specialMode) {
12
13 if (stringMode) {
14
15 if (
16 (
17 char === '\'' ||
18 char === '\"' ||
19 char === '\`'
20 ) &&
21 stringChar === char &&
22 html[i-1] !== '\\'
23 ) {
24 stringChar = '';
25 stringMode = false;
26 }
27
28 continue;
29 }
30
31 if (!stringMode && char === '\'' || char === '\"' || char === '\`') {
32 stringChar = char;
33 stringMode = true;
34 continue;
35 }
36
37 }
38
39 if (!char) {
40 continue;
41 } else if (html.slice(i, 8) === '<script>') {
42 specialMode = true;
43 i += 8;
44 } else if (html.slice(i, 7) === '<style>') {
45 specialMode = true;
46 i += 7;
47 } else if (html.slice(i, 4) === '<!--') {
48 specialMode = true;
49 i += 4;
50 } else if (html.slice(i, 9) === '</script>') {
51 specialMode = false;
52 i += 9;
53 } else if (html.slice(i, 8) === '</style>') {
54 specialMode = false;
55 i += 8;
56 } else if (html.slice(i, 3) === '-->') {
57 specialMode = false;
58 i += 3;
59 } else if (specialMode) {
60 continue;
61 } else if (!tagStartMode && !attributeMode && char === '<') {
62 tagStartMode = true;
63 attributeMode = false;
64 } else if (tagStartMode && char === ' ') {
65 tagStartMode = false;
66 attributeMode = true;
67 } else if (attributeMode && char === '>') {
68 tagStartMode = false;
69 attributeMode = true;
70 }
71
72}