-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathish.js
More file actions
236 lines (205 loc) · 4.99 KB
/
ish.js
File metadata and controls
236 lines (205 loc) · 4.99 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const { isArray } = Array;
const { assign, freeze} = Object;
/* c8 ignore stop */
// this is an essential ad-hoc DOM facade
const ELEMENT = 1;
const ATTRIBUTE = 2;
const TEXT = 3;
const COMMENT = 8;
const DOCUMENT_TYPE = 10;
const FRAGMENT = 11;
const COMPONENT = 42;
const TEXT_ELEMENTS = new Set([
'plaintext',
'script',
'style',
'textarea',
'title',
'xmp',
]);
const VOID_ELEMENTS = new Set([
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr',
]);
const props = freeze({});
const children = freeze([]);
const append = (node, child) => {
if (node.children === children) node.children = [];
node.children.push(child);
child.parent = node;
return child;
};
const prop = (node, name, value) => {
if (node.props === props) node.props = {};
node.props[name] = value;
};
const addJSON = (value, comp, json) => {
if (value !== comp) json.push(value);
};
const setChildren = (node, json) => {
node.children = json.map(revive, node);
};
const setJSON = (node, json, index) => {
switch (json.length) {
case index: setChildren(node, json[index - 1]);
case index - 1: {
const value = json[index - 2];
if (isArray(value)) setChildren(node, value);
else node.props = assign({}, value);
}
}
return node;
};
function revive(json) {
const node = fromJSON(json);
node.parent = this;
return node;
}
const fromJSON = json => {
switch (json[0]) {
case COMMENT: return new Comment(json[1]);
case DOCUMENT_TYPE: return new DocumentType(json[1]);
case TEXT: return new Text(json[1]);
case COMPONENT: return setJSON(new Component, json, 3);
case ELEMENT: return setJSON(new Element(json[1], !!json[2]), json, 5);
case FRAGMENT: {
const node = new Fragment;
if (1 < json.length) node.children = json[1].map(revive, node);
return node;
}
}
};
class Node {
constructor(type) {
this.type = type;
this.parent = null;
}
toJSON() {
//@ts-ignore
return [this.type, this.data];
}
}
class Comment extends Node {
constructor(data) {
super(COMMENT);
this.data = data;
}
toString() {
return `<!--${this.data}-->`;
}
}
class DocumentType extends Node {
constructor(data) {
super(DOCUMENT_TYPE);
this.data = data;
}
toString() {
return `<!${this.data}>`;
}
}
class Text extends Node {
constructor(data) {
super(TEXT);
this.data = data;
}
toString() {
return this.data;
}
}
class Component extends Node {
constructor() {
super(COMPONENT);
this.name = 'template';
this.props = props;
this.children = children;
}
toJSON() {
const json = [COMPONENT];
addJSON(this.props, props, json);
addJSON(this.children, children, json);
return json;
}
toString() {
let attrs = '';
for (const key in this.props) {
const value = this.props[key];
if (value != null) {
/* c8 ignore start */
if (typeof value === 'boolean') {
if (value) attrs += ` ${key}`;
}
else attrs += ` ${key}="${value}"`;
/* c8 ignore stop */
}
}
return `<template${attrs}>${this.children.join('')}</template>`;
}
}
class Element extends Node {
constructor(name, xml = false) {
super(ELEMENT);
this.name = name;
this.xml = xml;
this.props = props;
this.children = children;
}
toJSON() {
const json = [ELEMENT, this.name, +this.xml];
addJSON(this.props, props, json);
addJSON(this.children, children, json);
return json;
}
toString() {
const { xml, name, props, children } = this;
const { length } = children;
let html = `<${name}`;
for (const key in props) {
const value = props[key];
if (value != null) {
if (typeof value === 'boolean') {
if (value) html += xml ? ` ${key}=""` : ` ${key}`;
}
else html += ` ${key}="${value}"`;
}
}
if (length) {
html += '>';
for (let text = !xml && TEXT_ELEMENTS.has(name), i = 0; i < length; i++)
html += text ? children[i].data : children[i];
html += `</${name}>`;
}
else if (xml) html += ' />';
else html += VOID_ELEMENTS.has(name) ? '>' : `></${name}>`;
return html;
}
}
class Fragment extends Node {
constructor() {
super(FRAGMENT);
this.name = '#fragment';
this.children = children;
}
toJSON() {
const json = [FRAGMENT];
addJSON(this.children, children, json);
return json;
}
toString() {
return this.children.join('');
}
}
export { ATTRIBUTE, COMMENT, COMPONENT, Comment, Component, DOCUMENT_TYPE, DocumentType, ELEMENT, Element, FRAGMENT, Fragment, Node, TEXT, TEXT_ELEMENTS, Text, VOID_ELEMENTS, append, children, fromJSON, prop, props };