-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagesnippets.js
413 lines (412 loc) · 14.2 KB
/
pagesnippets.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
pageSnippets (https://github.com/suppenhuhn79/pagesnippets)
Copyright 2021 Christoph Zager, licensed under the Apache License, Version 2.0
See the full license text at http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* @deprecated Use pagesnippets2.js instead.
*/
const pageSnippets =
{
NAMESPACE_URI: "https://github.com/suppenhuhn79/pagesnippets",
import: (url) =>
{
function _fetch (url)
{
return new Promise((resolve, reject) =>
{
fetch(url).then(
(response) => (response.status === 200) ? resolve(response.text()) : reject(new ReferenceError("Server returned " + response.status + " (" + response.statusText + ") when trying to fetch " + response.url)),
(reason) => reject(reason));
});
};
function _produce (owner = window, data = {}, _parentSnippetKey = "")
{
const NODETYPE_ELEMENT = 1;
const NODETYPE_TEXT = 3;
const HTML_NAMESPACE_URI = "http://www.w3.org/1999/xhtml";
function __getObjectValueByPath (object, path, pathSeparator = ".")
{
let result = undefined;
if (!!object && !!path)
{
let steps = path.split(pathSeparator);
result = (steps.length === 1) ? object[steps[0]] : __getObjectValueByPath(object[steps[0]], steps.splice(1).join(pathSeparator), pathSeparator);
}
return result;
}
function __resolveVariables (sourceNode, text, data, stringTransformer = null)
{
let rex = /\{\{(.*?)\}\}/g;
let result = text;
let rexResult = rex.exec(text);
while (!!rexResult)
{
let value = __getObjectValueByPath(data, rexResult[1], ".");
if (value === undefined)
{
console.info("\"" + rexResult[1] + "\" is not defined, set to <empty-string>.", sourceNode, currentSnippetKey);
value = "";
}
result = result.replace("{{" + rexResult[1] + "}}", ((typeof stringTransformer === "function") ? stringTransformer(String(value)) : value));
rexResult = rex.exec(text);
}
return result;
}
function __resolveUnicodeEntities (text)
{
let rex = /&#x([0-9a-f]+);/i;
let rem = rex.exec(text);
while (rem)
{
text = text.replace(rem[0], JSON.parse("\"\\u" + rem[1].padStart(4, "0") + "\""));
rem = /&#x([0-9a-f]+);/i.exec(text);
}
return text;
}
function __addAttributes (sourceNode, targetElement, owner, data)
{
for (let attribute of sourceNode.attributes)
{
if (attribute.namespaceURI === pageSnippets.NAMESPACE_URI)
{
if (/^on\S+/.test(attribute.localName))
{
let referencedFunction = __getObjectValueByPath(owner, attribute.value);
if (typeof referencedFunction === "function")
{
targetElement[attribute.localName] = referencedFunction;
}
else
{
console.warn("Event handler \"" + attribute.value + "\" is not defined.", sourceNode, currentSnippetKey);
}
}
else if (attribute.localName !== "postproduction")
{
console.warn("Attribute not allowed \"" + attribute.name + "\".", sourceNode, currentSnippetKey);
}
}
else
{
targetElement.setAttributeNS(attribute.namespaceURI, attribute.localName, __resolveVariables(sourceNode, attribute.value, data));
}
}
}
function __processNode (sourceNode, targetElement, owner, data)
{
for (let childSourceNode of sourceNode.childNodes)
{
switch (childSourceNode.nodeType)
{
case NODETYPE_ELEMENT:
if (childSourceNode.namespaceURI === pageSnippets.NAMESPACE_URI)
{
switch (childSourceNode.localName)
{
case "call-function":
__psCallFunction(childSourceNode, targetElement, owner, data);
break;
case "choose":
__psChoose(childSourceNode, targetElement, owner, data);
break;
case "for-each":
__psForEach(childSourceNode, targetElement, owner, data);
break;
case "for-empty":
__psForEmpty(childSourceNode, targetElement, owner, data);
break;
case "if":
__psIf(childSourceNode, targetElement, owner, data);
break;
case "insert-snippet":
__psInsertSnippet(childSourceNode, targetElement, owner, data);
break;
case "text":
targetElement.appendChild(document.createTextNode(__resolveUnicodeEntities(__resolveVariables(childSourceNode, childSourceNode.firstChild.data, data))));
break;
default:
console.warn("Element not allowed here.", childSourceNode, currentSnippetKey);
}
}
else
{
let element = document.createElementNS(childSourceNode.namespaceURI ?? HTML_NAMESPACE_URI, childSourceNode.tagName);
__addAttributes(childSourceNode, element, owner, data);
__processNode(childSourceNode, element, owner, data);
__psPostProduction(childSourceNode, element, owner, data);
targetElement.appendChild(element);
}
break;
case NODETYPE_TEXT:
if (/^\s*$/.test(childSourceNode.textContent) === false)
{
targetElement.appendChild(document.createTextNode(__resolveUnicodeEntities(__resolveVariables(sourceNode, childSourceNode.textContent, data))));
}
break;
}
}
}
function __psPostProduction (sourceNode, targetElement, owner, data)
{
let postProductionFunctionName = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "postproduction");
if (!!postProductionFunctionName)
{
targetElement.removeAttributeNS(pageSnippets.NAMESPACE_URI, "postproduction");
let referencedFunction = __getObjectValueByPath(owner, postProductionFunctionName);
if (typeof referencedFunction === "function")
{
referencedFunction(targetElement, data);
}
else
{
console.error("Postproduction function \"" + postProductionFunctionName + "\" is not defined.", sourceNode, currentSnippetKey);
}
}
}
function __psCallFunction (sourceNode, targetElement, owner, data)
{
let functionName = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "name") ?? sourceNode.getAttribute("name");
let referencedFunction = __getObjectValueByPath(owner, functionName);
if (typeof referencedFunction === "function")
{
referencedFunction(targetElement, data);
}
else
{
console.error("Function to call \"" + functionName + "\" is not defined.", sourceNode, currentSnippetKey);
}
}
function __psForEach (sourceNode, targetElement, owner, data)
{
let listKey = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "list") ?? sourceNode.getAttribute("list");
if (!!data[listKey])
{
let variablesList = data[listKey];
for (let i = 0, ii = variablesList.length; i < ii; i += 1)
{
let listItem = (["string", "number", "boolean"].includes(typeof variablesList[i])) ? { "_value": variablesList[i] } : Object.assign({}, variablesList[i]);
listItem["_position"] = i + 1;
listItem["_count"] = ii;
__processNode(sourceNode, targetElement, owner, Object.assign({}, data, listItem));
}
}
else
{
console.warn("\"" + listKey + "\" is not defined.", sourceNode, currentSnippetKey);
}
}
function __psForEmpty (sourceNode, targetElement, owner, data)
{
let listKey = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "list") ?? sourceNode.getAttribute("list");
if ((!data[listKey]) || (data[listKey].length === 0))
{
__processNode(sourceNode, targetElement, owner, data);
}
}
function __psChoose (sourceNode, targetElement, owner, data)
{
const CHOOSE_MODE_STRICT = "strict";
const CHOOSE_MODE_LAX = "lax";
let chooseMode = (RegExp("^" + CHOOSE_MODE_STRICT + "$|^" + CHOOSE_MODE_LAX + "$").exec((sourceNode.getAttribute("mode") ?? CHOOSE_MODE_STRICT)) ?? [""])[0];
if (chooseMode === "")
{
console.warn("Invalid choose-mode \"" + sourceNode.getAttribute("mode") + "\", using \"strict\".", sourceNode, currentSnippetKey);
chooseMode = CHOOSE_MODE_STRICT;
}
let anyMatch = false;
for (let childSourceNode of sourceNode.children)
{
if (childSourceNode.namespaceURI === pageSnippets.NAMESPACE_URI)
{
switch (childSourceNode.localName)
{
case "if":
let thisMatch = __psIf(childSourceNode, targetElement, owner, data);
anyMatch = anyMatch || thisMatch;
break;
case "else":
if (anyMatch === false)
{
__processNode(childSourceNode, targetElement, owner, data);
}
break;
default:
console.warn("Element not allowed here.", childSourceNode, currentSnippetKey);
}
if (anyMatch && (chooseMode === CHOOSE_MODE_STRICT))
{
break;
}
}
else
{
console.warn("Element not allowed here.", childSourceNode, currentSnippetKey);
}
}
}
function __psIf (sourceNode, targetElement, owner, data)
{
let testExpression = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "test") ?? sourceNode.getAttribute("test");
testExpression = __resolveVariables(sourceNode, testExpression, data, (str) => str.replace("'", "\\'"));
let testResult;
try
{
testResult = eval(testExpression);
}
catch (ex)
{
console.error("Cannot evaluate expression \"" + testExpression + "\": " + ex.message, sourceNode, currentSnippetKey);
}
if (testResult === true)
{
__processNode(sourceNode, targetElement, owner, data);
}
return testResult;
}
function __psInsertSnippet (sourceNode, targetElement, owner, data)
{
let snippetPath = sourceNode.getAttributeNS(pageSnippets.NAMESPACE_URI, "name") ?? sourceNode.getAttribute("name");
let snippet;
try
{
snippet = __getObjectValueByPath(pageSnippets, snippetPath, "/");
}
finally
{
if (!!snippet)
{
targetElement.appendChild(snippet.produce(owner, data, currentSnippetKey));
}
else
{
console.error("Unknown snippet \"" + snippetPath + "\".", sourceNode, currentSnippetKey);
}
}
}
let currentSnippetKey = ((_parentSnippetKey !== "") ? _parentSnippetKey + "->" : "") + "@" + this.src + ":" + this.snippetKey;
let result = document.createElementNS(this.namespaceURI ?? HTML_NAMESPACE_URI, this.localName);
__addAttributes(this, result, owner, data);
__processNode(this, result, owner, data);
__psPostProduction(this, result, owner, data);
return result;
};
return new Promise((resolve, reject) => _fetch(url).then(
(data) =>
{
function _cleanPath (path)
{
let templateRoot = url.replace(/[^./]+\.[\S]+$/, "");
return templateRoot.concat(path).replace(/[^/]+\/\.\.\//g, "");
}
function _parse (node, targetObject, groupName, scriptsCollection)
{
for (let childNode of node.children)
{
if (childNode.namespaceURI === pageSnippets.NAMESPACE_URI)
{
if (childNode.localName === "snippet")
{
_appendSnippet(childNode, targetObject, groupName);
}
else if (childNode.localName === "snippet-group")
{
let childGroupName = childNode.getAttribute("name");
targetObject[childGroupName] ??= {};
_parse(childNode, targetObject[childGroupName], groupName + ((groupName !== "") ? "/" : "") + childGroupName, scriptsCollection);
}
else if ((groupName === "") && (childNode.localName === "stylesheet"))
{
_includeStylesheet(childNode);
}
else if ((groupName === "") && (childNode.localName === "script"))
{
scriptsCollection.push(childNode);
}
else
{
console.warn("Element not allowed here.", childNode, "@" + url + ":" + ((groupName === "") ? "(root)" : groupName));
}
}
else
{
console.warn("Unexpected element.", childNode, "@" + url + ":" + ((groupName === "") ? "(root)" : groupName));
}
}
}
function _appendSnippet (node, targetObject, groupName)
{
let snippetName = node.getAttribute("name");
targetObject[snippetName] = Object.assign(node.firstElementChild,
{
snippetKey: groupName + ((groupName !== "") ? "/" : "") + node.getAttribute("name"),
src: url,
produce: _produce
});
if (node.childElementCount > 1)
{
console.warn("Only one child element allowed.", node, "@" + url + ":" + ((groupName === "") ? "(root)" : groupName));
}
}
function _includeStylesheet (node)
{
let styleNode = document.createElement("link");
let src = _cleanPath(node.getAttribute("src"));
if (document.querySelector("link[rel=\"stylesheet\"][href=\"" + src + "\"]") === null)
{
styleNode.setAttribute("rel", "stylesheet");
styleNode.setAttribute("href", src);
document.head.appendChild(styleNode);
}
}
function _includeScripts (scriptsCollection)
{
let scriptsToLoad = scriptsCollection.length;
function __onScriptLoadend (loadEvent)
{
if (loadEvent.type === "error")
{
console.error("Error while loading \"" + loadEvent.target.src + "\"", loadEvent.target, "@" + url + ":(root)");
}
if ((scriptsToLoad -= 1) === 0)
{
resolve();
}
}
for (let scriptNode of scriptsCollection)
{
let src = _cleanPath(scriptNode.getAttribute("src"));
if (document.querySelector("script[src=\"" + src + "\"]") === null)
{
let scriptNode = document.createElement("script");
scriptNode.setAttribute("src", src);
scriptNode.addEventListener("load", __onScriptLoadend);
scriptNode.addEventListener("error", __onScriptLoadend);
document.head.appendChild(scriptNode);
}
else
{
scriptsToLoad -= 1;
}
}
if ((scriptsCollection.length === 0) || (scriptsToLoad === 0))
{
resolve();
}
}
let xmlDocument = new DOMParser().parseFromString(data, "text/xml");
if ((xmlDocument.documentElement.namespaceURI === pageSnippets.NAMESPACE_URI) && (xmlDocument.documentElement.localName === "pagesnippets"))
{
let scriptsCollection = [];
_parse(xmlDocument.firstElementChild, pageSnippets, "", scriptsCollection);
_includeScripts(scriptsCollection); // this does finally resolve()
}
else
{
reject(new Error("\"" + url + "\" it not a pagesnippets XML-document."));
}
},
(ex) => reject(new Error(ex)))
);
}
};