-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.es
317 lines (276 loc) · 12.9 KB
/
dom.es
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* eslint-disable no-param-reassign */
/* eslint-disable no-underscore-dangle */
/* eslint-disable spaced-comment */
/* eslint max-len: ["error", { "code": 150, "comments": 200 }] */
/* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */
import {dashPropToAbbrClassName} from './src/css/dashPropToAbbrClassName.es';
import {valueDashPropToAbbrClassName} from './src/css/valueDashPropToAbbrClassName.es';
import {addDefaultUnit} from './src/css/addDefaultUnit.es';
import {classAppendAndCssFromMedia} from './src/css/classAppendAndCssFromMedia.es';
import {classAppendAndCssFromStyle} from './src/css/classAppendAndCssFromStyle.es';
import {toClassName} from './src/css/toClassName.es';
import {uniqCss} from './src/css/uniqCss.es';
import {HTML_AND_SVG_ELEMENTS} from './src/html/elements.es';
import {att2Str} from './src/html/att2Str.es';
import {isVoid} from './src/html/isVoid.es';
import {cdata} from './src/html/cdata.es';
import {doctype} from './src/html/doctype.es';
import {isArray} from './src/util/isArray.es';
import {isFunction} from './src/util/isFunction.es';
import {isSet} from './src/util/isSet.es';
import {isString} from './src/util/isString.es';
import {toStr} from './src/util/toStr.es'; // Used in throw
//const WARN = true;
//const DEBUG = false;
//const TRACE = false;
/*
XML elements must follow these naming rules:
Element names are case-sensitive
Element names must start with a letter or underscore
Element names cannot start with the letters xml (or XML, or Xml, etc)
Element names can contain letters, digits, hyphens, underscores, and periods
Element names cannot contain spaces
*/
/*
A little weird way of exporting, but it works when:
1. imported directly
2. transpiled to dist
3. webpacked to lib
*/
const PROPERTY_TAG = '-t';
exports.PROPERTY_TAG = PROPERTY_TAG;
// const PROPERTY_ATTRIBUTES = '_a';
const PROPERTY_CHILDREN = '-c';
exports.PROPERTY_CHILDREN = PROPERTY_CHILDREN;
const PROPERTY_CSS = '-cs';
const PROPERTY_HTML = '-h';
const PROPERTY_SPEC = '-s';
// const PROPERTY_PARENT = '_p';
const METHODS = ['constructor', 'add', 'build', 'getCss', 'render', 'setDown', 'setUp'];
const UNIQUE_ELEMENTS = ['html', 'head', 'body'];
class Node {
/* static get children() { // Read-only accessor
return this[PROPERTY_CHILDREN];
} */
constructor(tag, spec, content) {
if (METHODS.includes(tag)) { throw new Error(`Illegal tag:${tag} matches method name!`); }
// TRACE && console.log(`new Node(${toStr(tag)}, ${toStr(spec)}, ${toStr(content)})`);
this.root = this;
this[PROPERTY_TAG] = tag;
if (UNIQUE_ELEMENTS.includes(tag)) { this[tag] = this; } // Reference self
// TRACE && console.log(`${tag} spec is ${(isString(spec) && 'string') || (spec instanceof Node && 'Node') || (Array.isArray(spec) && 'Array') || 'Unknown type'}`);
if (isString(spec) || spec instanceof Node || isArray(spec) || isFunction(spec)) {
content = spec;
spec = {};
}
this[PROPERTY_SPEC] = spec;
this[PROPERTY_CSS] = [];
this[PROPERTY_CHILDREN] = [];
this.add(content);
// TRACE && console.log(`tag:${toStr(this[PROPERTY_TAG])}, spec:${toStr(this[PROPERTY_SPEC])}, children:${toStr(this[PROPERTY_CHILDREN])}`);
} // constructor
setDown(key, value, { skip = null } = {}) {
//console.log(`this:${this[PROPERTY_TAG]} setDown(${key}, ${value[PROPERTY_TAG]}El)`);
//console.log(`Setting ${this[PROPERTY_TAG]}.${key}=${value[PROPERTY_TAG]} (in setDown)`);
this[key] = value;
//console.log(`${this[PROPERTY_TAG]} has ${this[PROPERTY_CHILDREN].length} children`);
this[PROPERTY_CHILDREN].forEach(child => {
if (child instanceof Node) {
if (child === skip) {
//console.log(`Skipping tree of ${child[PROPERTY_TAG]} when setting ${key}`);
} else {
child.setDown(key, value);
}
}
});
return this;
}
/*setUp(key, value) {
this[key] = value;
if (this.parent) {
this.parent.setUp(key, value);
}
return this;
}*/
hasDescendant(node) {
if (this[PROPERTY_CHILDREN].includes(node)) { return true; }
/* eslint-disable consistent-return */
this[PROPERTY_CHILDREN].forEach(child => {
if (child instanceof Node && child.hasDescendant(node)) { return true; } // "recurse"
});
/* eslint-enable consistent-return */
return false;
}
add(content) {
if (!isSet(content)) { return this; }
//console.log(`add(${toStr(content)}) children:${toStr(this[PROPERTY_CHILDREN])}`);
// Make sure children is an array.
if (!isArray(this[PROPERTY_CHILDREN])) { this[PROPERTY_CHILDREN] = [this[PROPERTY_CHILDREN]]; }
// Make it possible to add more than one content at a time.
const newContents = Array.isArray(content) ? content : [content];
const newNodes = [];
newContents.forEach(newContent => {
while (isFunction(newContent)) {
newContent = newContent();
}
if (isString(newContent)) {
this[PROPERTY_CHILDREN].push(newContent);
} else if (newContent instanceof Node) {
if (this.hasDescendant(newContent)) { // Protect against circular
throw new Error(`The node:${newContent[PROPERTY_TAG]} already exists in the tree! Not adding to ${this[PROPERTY_TAG]}!`);
}
this[PROPERTY_CHILDREN].push(newContent); // Add to exsisting tree.
newContent.parent = this;
//console.log(`Setting down from toBeAdded:${toBeAdded[PROPERTY_TAG]}.root to this:${this[PROPERTY_TAG]}.root (in add)`);
newContent.setDown('root', this.root); // Make sure new tree has correct root. Needed later to apply UE everywhere (new sister trees too).
// Add path properties:
const addedNodeTag = newContent[PROPERTY_TAG];
//if (!UNIQUE_ELEMENTS.includes(addedNodeTag)) { // Path properties for ue added later (avoid conflict)
if (this[addedNodeTag]) { // Already has path property
//console.log(`Extending ${this[PROPERTY_TAG]}.${addedNodeTag} (in add)`);
if (Array.isArray(this[addedNodeTag])) { // Already have more than 1 child with same tag
if (this[addedNodeTag].includes(newContent)) { throw new Error(`Path property ${addedNodeTag} already contains node!`); }
this[addedNodeTag].push(newContent);
} else { // Only have 1 child with same tag, convert to array
if (this[addedNodeTag] === newContent) { throw new Error(`Path property ${addedNodeTag} already references node!`); }
this[addedNodeTag] = [this[addedNodeTag], newContent];
}
} else { // Don't have that path properties yet
//console.log(`A ${this[PROPERTY_TAG]} child:${addedNodeTag} is not ue only setting on this:${this[PROPERTY_TAG]}`);
//console.log(`Setting ${this[PROPERTY_TAG]}.${addedNodeTag} to addedNode (in add)`);
this[addedNodeTag] = newContent;
}
//} // !UNIQUE_ELEMENT
newNodes.push(newContent);
} else if (isSet(newContent)) {
throw new Error(`Unhandeled type when adding newContent:${toStr(newContent)}`);
}
}); // forEach newContent
/*
When adding, the new stuff can be:
* A single node
* A three
* An array with any number of single nodes or trees
As such useful properties of any new children, should be added to existing tree (root skip new stuff).
And useful properties of existing tree (this) should be added to all new chilren.
But you have to take into consideration all added sibling trees.
*/
newNodes.forEach(addedNode => {
UNIQUE_ELEMENTS.forEach(ue => { // Allow direct acces to some unique elements
if (this[ue]) { // Existing tree has ue
//if (addedNode[ue]) { throw new Error(`Both existing tree:${this[PROPERTY_TAG]} and added tree:${addedNode[PROPERTY_TAG]} has ue:${ue}!`); }
if (!addedNode[ue]) {
//console.log(`Setting down from addedNode:${addedNode[PROPERTY_TAG]}.${ue} to this:${this[PROPERTY_TAG]}.${ue} (in add)`);
addedNode.setDown(ue, this[ue]); // Apply ue to added tree
}
} else if (addedNode[ue]) { // Added tree has ue
//console.log(`Setting down from this.root:${this.root[PROPERTY_TAG]}.${ue} to addedNode:${addedNode[PROPERTY_TAG]}.${ue} (in add)`);
this.root.setDown(ue, addedNode[ue], { skip: addedNode }); // Apply to entire tree which includes new siblings, but skip own tree.
}
}); // forEach ue
}); // forEach addedNode
//console.log(`add children:${toStr(this[PROPERTY_CHILDREN])}`);
return this;
} // add
build({
abbreviateCssPropertyNames = true,
abbreviateCssPropertyValues = true,
addDefaultUnits = true,
autoprefixer = true
} = {}) {
const tag = this[PROPERTY_TAG]; // DEBUG && console.log(`tag:${toStr(tag)}`);
const spec = this[PROPERTY_SPEC] || {}; // DEBUG && console.log(`spec:${toStr(spec)}`);
const options = {
dashPropToAbbrClassNameFn: abbreviateCssPropertyNames ? dashPropToAbbrClassName : toClassName,
valueDashPropToAbbrClassNameFn: abbreviateCssPropertyValues ? valueDashPropToAbbrClassName : toClassName,
addDefaultUnitFn: addDefaultUnits ? addDefaultUnit : value => value,
autoprefixer
};
if (spec.style) {
const s = classAppendAndCssFromStyle(spec.style, options);
spec.class = [].concat(spec.class, s.classAppend).filter(n => n); // Remove null elements;
this[PROPERTY_CSS] = this[PROPERTY_CSS].concat(s.css);
spec.style = null;
}
if (spec._media) {
const o = classAppendAndCssFromMedia(spec._media, options);
spec.class = [].concat(spec.class, o.classAppend).filter(n => n); // Remove null elements;
this[PROPERTY_CSS] = this[PROPERTY_CSS].concat(o.css);
}
const attributes = att2Str({ ...spec, _media: null }, options); //DEBUG && console.log(`attributes:${toStr(attributes)}`);
if (isVoid(tag)) { this[PROPERTY_HTML] = `<${tag}${attributes}/>`; return this; }
const children = this[PROPERTY_CHILDREN]; //DEBUG && console.log(`children:${toStr(children)}`);
if (!children) { this[PROPERTY_HTML] = `<${tag}${attributes}></${tag}>`; return this; }
if (isString(children)) { this[PROPERTY_HTML] = `<${tag}${attributes}>${children}</${tag}>`; return this; }
if (children instanceof Node) {
children.build();
this[PROPERTY_HTML] = `<${tag}${attributes}>${children[PROPERTY_HTML]}</${tag}>`;
this[PROPERTY_CSS] = this[PROPERTY_CSS].concat(children[PROPERTY_CSS]);
return this;
}
if (Array.isArray(children)) {
let innerHtml = '';
children.forEach(child => {
if (child instanceof Node) {
innerHtml += child.build()[PROPERTY_HTML];
this[PROPERTY_CSS] = this[PROPERTY_CSS].concat(child[PROPERTY_CSS]);
//try {} catch (e) {throw new Error(`child doesn't have a build method message:${e.message} child:${toStr(child)} children:${toStr(children)}`);}
} else if (isString(child)) {
innerHtml += child;
}
}); // forEach(child
this[PROPERTY_HTML] = `<${tag}${attributes}>${innerHtml}</${tag}>`;
return this;
} // childen isArray
//WARN && console.warn(`NODE: children not String, Node or Array of Nodes!`);
this[PROPERTY_HTML] = '';
return this;
} // build
getCss() {
return uniqCss(this.build()[PROPERTY_CSS]);
}
render() {
return this.build()[PROPERTY_HTML];
}
} // class Node
exports.Node = Node;
class Dom extends Node {
constructor(spec, content) {
//TRACE && console.log(`new Dom(${toStr(spec)}, ${toStr(content)})`);
super('', spec, content);
}
build() {
const children = this[PROPERTY_CHILDREN]; //TRACE && console.log(`children:${toStr(children)}`);
if (!children) { this[PROPERTY_HTML] = ''; return this; }
if (isString(children)) { this[PROPERTY_HTML] = children; return this; }
if (children instanceof Node) {
this[PROPERTY_HTML] = children.build()[PROPERTY_HTML];
this[PROPERTY_CSS] = children[PROPERTY_CSS] ? children[PROPERTY_CSS] : [];
return this;
}
if (Array.isArray(children)) {
let innerHtml = '';
children.forEach(child => {
if (child instanceof Node) {
innerHtml += child.build()[PROPERTY_HTML];
this[PROPERTY_CSS] = this[PROPERTY_CSS].concat(child[PROPERTY_CSS]);
//try {} catch (e) {throw new Error(`child doesn't have a build method message:${e.message} child:${toStr(child)} children:${toStr(children)}`);}
} else if (isString(child)) {
innerHtml += child;
}
}); // forEach(child
this[PROPERTY_HTML] = innerHtml;
return this;
}
//WARN && console.warn(`DOM: children not String, Node or Array of Nodes!`);
this[PROPERTY_HTML] = '';
return this;
} // build
} // class Dom
exports.Dom = Dom;
HTML_AND_SVG_ELEMENTS.forEach(k => {
exports[k] = (...args) => new Node(k, ...args);
});
exports.cdata = (...args) => new Dom(cdata(...args));
exports.doctype = (...args) => new Dom(doctype(...args));
//export default exports; // Not needed