-
Notifications
You must be signed in to change notification settings - Fork 0
/
htm.js
195 lines (161 loc) · 5.08 KB
/
htm.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {push, len, assert, isInstance} from 'destam/util.js';
export const validTags = ['BASE', 'LINK', 'META', 'STYLE', 'TITLE', 'ADDRESS', 'ARTICLE',
'ASIDE', 'FOOTER', 'HEADER', 'H1', 'H2', 'H3', 'HGROUP', 'MAIN',
'NAV', 'SECTION', 'SEARCH', 'BLOCKQUOTE', 'DD', 'DIV', 'DL', 'DT',
'FIGCAPTION', 'FIGURE', 'HR', 'LI', 'OL', 'P', 'PRE', 'UL', 'A',
'ABBR', 'B', 'BDI', 'BDO', 'BR', 'CITE', 'CODE', 'DATA', 'DFN',
'EM', 'I', 'KBD', 'MARK', 'Q', 'RP', 'RT', 'RUBY', 'S', 'SAMP',
'SMALL', 'SPAN', 'STRONG', 'SUB', 'SUP', 'TIME', 'U', 'VAR',
'WBR', 'AREA', 'AUDIO', 'IMG', 'MAP', 'TRACK', 'VIDEO', 'EMBED',
'IFRAME', 'OBJECT', 'PICTURE', 'PORTAL', 'SOURCE', 'MATH',
'CANVAS', 'NOSCRIPT', 'DEL', 'INS', 'CAPTION', 'COL', 'COLGROUP',
'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'BUTTON',
'DATALIST', 'FIELDSET', 'FORM', 'INPUT', 'LABEL', 'LEGEND', 'METER',
'OPTGROUP', 'OPTION', 'OUTPUT', 'PROGRESS', 'SELECT', 'TEXTAREA',
'DETAILS', 'DIALOG', 'SUMMARY', 'SLOT', 'TEMPLATE'];
export default (h, assign = Object.assign, join = arr => arr.join('')) => (segments, ...args) => {
const special = {'<': {}, '>': {}, '=': {}, '/': {}};
const tokens = [];
let tag = 9, inString, cur = '';
const flush = () => {
if (tag) {
if ((tag & 2) && !(tag & 4)) cur += ' ';
tag = 9;
}
if (cur) push(inString || tokens, cur);
cur = '';
};
for (let i = 0; i < len(segments); i++) {
const str = segments[i];
// when scanning the string, extract special characters and simplify whitespace
for (let ii = 0; ii < len(str); ii++) {
if (str.startsWith('<!--', ii)) {
const iii = str.indexOf('-->', ii);
assert(iii !== -1, "Comment does not terminate");
ii = iii + 2;
continue;
}
const char = str[ii];
if (inString) {
if (char == inString.char_) {
flush();
push(tokens, join(inString));
inString = 0;
} else {
cur += char;
}
continue;
}
const whitespace = "\n\r\t ".indexOf(char);
if (char === '<') {
flush();
tag = 0;
}
if (!tag) {
if (whitespace >= 0) {
flush();
} else if (special[char]) {
flush();
if (char === '>') tag = 9;
push(tokens, special[char]);
} else if (char === '"' || char === "'") {
flush();
inString = [];
inString.char_ = char;
} else {
cur += char;
}
continue;
}
if (whitespace >= 0) {
tag |= whitespace == 0 ? 4 : 2;
} else {
if ((tag & 6) && (tag & 12) != 12) cur += ' ';
tag = 1;
cur += char;
}
}
if (i < len(args)) {
flush();
push(inString || tokens, args[i]);
}
}
flush();
let i = 0;
const parse = (tagName) => {
let tags = [], term = 0;
for (; !term && i < len(tokens);) {
if (tokens[i] !== special['<']) {
push(tags, tokens[i++]);
continue;
}
let name = tokens[++i];
assert(name !== special['>'], "Empty tag");
if (name === special['/']) {
name = 0;
} else {
assert(!Object.values(special).includes(name), "Unexpected special char near tag opening");
i++;
}
const props = {};
let prevTag;
for (; tokens[i] !== special['>']; i++) {
assert(i < len(tokens), "Unexpected end of input");
let token = tokens[i];
if (token === special['/']) {
assert(tokens[i + 1] === special['>'] || tokens[i + 2] === special['>'], "Tag not terminated properly");
term = tokens[i + 1];
} else if (token === special['=']) {
token = tokens[++i];
if (prevTag) {
assert(!Object.values(special).includes(token), "Unexpected special char near attribute value");
props[prevTag] = token;
prevTag = 0;
} else {
assign(props, token);
}
} else {
assert(!Object.values(special).includes(token), "Unexpected special char near attribute name");
props[prevTag = token] = true;
}
}
i++;
if (name) {
/* node:coverage disable */
assert((() => {
const strHasOnly = (chars, str) => {
for (let ii = 0; ii < len(str); ii++) {
if (chars.indexOf(str.charAt(ii)) == -1) {
return 0;
}
}
return 1;
};
for (const prop in props) {
if (!isInstance(prop, Array)) {
continue;
}
const name = prop[0];
if (typeof name === 'string' &&
!strHasOnly("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-$", name)) {
return false;
}
}
return true;
})(), "Invalid attribute name: " + Object.keys(props).join(", "));
/* node:coverage enable */
assert(typeof name !== 'string' || validTags.includes(name.toUpperCase()) || name.includes('-'),
"Invalid tag name: " + name);
let ch;
push(tags, h(name, props, ...(term ? [] : (ch = parse(name), len(ch) ? ch : [null]))));
term = 0;
}
}
assert(!tagName || term, `Tag not terminated properly: <${tagName}>`);
assert(!tagName || term === special[">"] || term === tagName, `Tag not terminated properly: <${tagName}></${term}>`);
assert(!term || tagName || term === special[">"], `Tag termination at top level: </${term}>`);
assert(!term || tagName, `Annonymous tag termination at top level`);
return tags;
};
return parse();
};