-
Notifications
You must be signed in to change notification settings - Fork 6
/
z.js
276 lines (146 loc) · 6.54 KB
/
z.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
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
((global) => {
const charMap = {"&": "&", "<": "<", ">": ">"};
const replaceTag = (tag) => charMap[tag] || tag;
const escapeHTML = (str) => str.replace(/[&<>]/g, replaceTag);
/* General helpers */
const identity = (x) => x;
const isArr = (x) => Array.isArray(x);
const isObj = (x) => !isArr(x) && typeof x == "object";
const isScalar = (v) => (typeof v === "string" || typeof v === "boolean" || typeof v === "number");
const areAttrs = (attrs) => isObj(attrs);
/* Zahar specific helpers */
const isNode = (node) => isArr(node) && typeof node[0] == "string";
const isNodeArr = (node) => isArr(node) && isNode(node[0]);
const $ = x => document.querySelector(x);
const $all = (x) => document.querySelectorAll(x);
const node$ = (node,x) => node.querySelector(x);
const node$all = (node,x) => node.querySelectorAll(x);
const clearChildren = el => {
while(el.hasChildNodes())
el.removeChild(el.lastChild);
};
/* Functions for rendering nodes */
const escapeStr = st => (typeof st === "string") ? `"${st}"` : st;
const normalizeAttrs = (attrs) => {
const { data = {} } = attrs;
const dataAttrs = Object.entries(data).reduce((result, [dk, dv]) => Object.assign(result, {["data-" + dk]: dv}), {});
delete attrs.data;
Object.assign(dataAttrs, attrs);
return dataAttrs;
};
const serializeAttrs = ({attrs, serializer, delimiter}) => {
return Object.entries(attrs).map(([k,v]) => serializer(k, v)).join(delimiter);
};
const serializeStyle = (style) => serializeAttrs({attrs: style, serializer: (k, v) => k + ":" + v, delimiter: ";"});
const attrsText = attrsMap => {
const attrs = normalizeAttrs(attrsMap)
const serializer = (k, join, v) => {
if(v == null || v === false) return "";
else if(k === "style") return `${k}='${serializeStyle(v)}'`;
else if(isScalar(v)) return k + join + escapeStr(v);
else throw Error("Unknown value inside attributes: " + JSON.stringify(attrsMap));
}
return serializeAttrs({attrs, serializer: (k, v) => serializer(k, "=", v), delimiter: " "});
};
const setAttrs = (el, attrs = {}) => Object.entries(normalizeAttrs(attrs)).map(([k,v]) => {
// Don't set attributes when there’s no value present or is set to false
// Setting value to false for some DOM elements requires absence of attribute
if(v == null || v == false) return;
// Set style
if(k === "style") { el.setAttribute(k, serializeStyle(v)); }
else if (isScalar(v)) { el.setAttribute(k,v); }
else throw Error("Unknown value inside attributes: " + JSON.stringify(attrs));
});
const processTag = (tag) => {
const attrs = {};
let id = null;
const idStart = tag.indexOf("#");
if(idStart > 0) {
let idEnd = tag.indexOf(".", idStart);
if(idEnd < 0) idEnd = tag.length;
id = tag.slice(idStart + 1, idEnd);
tag = tag.slice(0, idStart) + tag.slice(idEnd);
}
let [parsed_tag, ...classes] = tag.split(".");
Object.assign(attrs, id && {id}, (classes.length > 0) && {class: classes.join(" ")});
return [parsed_tag, attrs];
};
const normalizeNode = (node) => {
let [tag, ...contents] = node;
let attrs = {};
if(areAttrs(contents[0])) {
attrs = contents[0];
contents = contents.slice(1);
}
let [parsed_tag, parsed_attrs] = processTag(tag);
Object.assign(attrs, parsed_attrs);
return [parsed_tag, attrs, ...contents];
};
const textToDOM = node => document.createTextNode(node);
const nodeToDOM = node => {
let [tag, attrs, ...contents] = normalizeNode(node);
if(typeof tag !== "string")
throw Error("Please provide a valid node to render. You passed: " + node.toString());
let el = document.createElement(tag);
const { events = {} } = attrs;
Object.entries(events).forEach(([k,v]) => el.addEventListener(k, v));
if(contents == undefined || contents == null)
console.warn("You passed in a node with no contents in it: [" + node.toString() + " * No content * ]");
contents.map(c => el.appendChild(buildDOM(c)));
delete attrs.events;
setAttrs(el, attrs);
return el;
};
const nodesToDOM = (nodes) => {
let container = document.createDocumentFragment();
let result = nodes.map(x => container.appendChild(x));
return container;
};
const textToFrag = (text) => document.createRange().createContextualFragment(text);
// A node or an array of nodes can be passed in to this function
// Node: [ scalar | [tag, attrs, Node]] | [ Node ]
// This is done so that when generating DOM with data
// the API forgives on creating [[[[Node]]]] like structures and
// renders the data however nested it is.
// TODO: I have to evaluate how this decision plays out after
// enough experience with the library
const walkNode = ({scalarFn = identity, nodeFn = identity, nodesFn = identity, node}) => {
if (node === undefined || node == null) {
return scalarFn("");
} else if (isScalar(node)) return scalarFn(node);
else if (isNode(node)) return nodeFn(node);
else if (isNodeArr(node)) return nodesFn(node.map(n => nodeFn(n)));
else throw Error("Please provide a valid node for parsing. You passed in " + JSON.stringify(node));
};
const htmlText = (node) => {
let [tag, attrs, ...contents] = normalizeNode(node);
if(!tag) throw Error("Please provide a tag");
attrs = attrsText(attrs);
return `<${tag}${attrs}>${contents.map(serialize).join("")}</${tag}>`;
};
const buildDOM = (data) => walkNode({scalarFn: textToDOM, nodeFn: nodeToDOM, nodesFn: nodesToDOM, node: data});
const serialize = (node) => walkNode({scalarFn: escapeHTML, nodeFn: htmlText, nodesFn: nodes => nodes.join(""), node});
const append = (parentNode, domData) => {
parentNode.appendChild(buildDOM(domData));
return parentNode;
};
// Creates HTML DOM from the provided text, clears the parentNode and then appends it on the given parentNode
const render = (parentNode, domData) => {
if(typeof parentNode === "string") parentNode = $(parentNode);
clearChildren(parentNode);
//Append directly if it's a tree, otherwise it's an array and append each of them
return append(parentNode, domData);
};
const doc = (head = "",body = "") => {
if(!body) {
body = head;
head = "";
}
return "<!doctype html>" + serialize(["html", ["head", ...head], ["body", ...body]]);
};
const css = link => serialize(["link", {rel: "stylesheet", type: "text/css", href: link}]);
const z = {$, $all, node$, node$all, clearChildren, setAttrs, nodeToDOM, textToFrag, serialize, append, render};
const nodejsZ = { isNode, serialize, doc, css };
global.z = z;
if(typeof module !== "undefined" && module.exports) module.exports = nodejsZ;
})(this);