This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
/
template.html.ts
686 lines (615 loc) · 20.7 KB
/
template.html.ts
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import prettier from "prettier";
import stringReplaceAsync from "string-replace-async";
import { JSDOM } from "jsdom";
import { TemplateFormat } from "../template-format";
import {
TemplateInput,
emptyTemplate,
TemplateUsage,
TemplateUsages,
TemplatesById,
TemplateUsageElement,
PRETTIER_LINE_WIDTH,
FormatUsageResponse
} from "../../index";
import {
TemplateAttribute,
DynamicKey,
DynamicKeyType,
simpleUniqueKey,
parseDynamicKey,
EnumOption,
isString,
serializeTemplateAttribute,
OnElement,
OnCloseElement,
OnVariable,
OnText,
OnSerialize
} from "../../common";
import {
getTemplateAttributes,
TemplateAttributesArgs
} from "../../attributes";
import { BrowserInstanceArgs, getBrowser } from "../../browser";
import { ID_SYNONYMS } from "../../attributes";
import { uniq } from "lodash";
export default class HTML implements TemplateFormat {
static id = "html";
static isDefaultOption = true;
public dirname = "html";
data: string = "";
template: TemplateInput;
assignedDynamicKeys: {};
constructor(template: TemplateInput = emptyTemplate) {
this.template = template;
this.assignedDynamicKeys = {};
this.data = "";
}
onElement = async ({
tagName,
attributes,
isSelfClosing
}: OnElement): Promise<string> => {
this.data +=
`<${tagName}` + // TODO: escape elementName?
(attributes && attributes.length > 0
? " " +
attributes
.map(
// TODO: escape attributes?
(attribute: TemplateAttribute) => this.renderAttribute(attribute)
)
.join(" ")
: "") +
// The reason for the SELF_CLOSING_HTML_ELEMENTS check is to distinguish
// between
// 1) when a <div> doesn't have children we don't render <div />
// because HTML5 parsers wouldn't like that.
// 2) when a <img> doesn't have children we don't want to render <img></img>
// we just want <img/>.
//
// So when isSelfClosing===true we need to optionally render either:
// <tag />
// or
// <tag></tag>
// based on whether that tag is On The List[tm]
//
// HTML5 parsers allow (AKA ignore) the "/" so we render it to be valid
// XHTML too.
(isSelfClosing && SELF_CLOSING_HTML_ELEMENTS.includes(tagName)
? "/"
: "") +
">" +
(isSelfClosing && !SELF_CLOSING_HTML_ELEMENTS.includes(tagName)
? `</${tagName}>`
: "");
return tagName;
};
renderAttribute(attribute: TemplateAttribute): string {
if (
attribute.dynamicKeys &&
!attribute.value &&
attribute.dynamicKeys.every(dynamicKey => dynamicKey.optional)
) {
// every dynamic key is optional, so don't render anything
return "";
}
let attr = `${attribute.key}="`;
let attrs: string[] = [];
if (attribute.value) {
attrs.push(attribute.value);
}
if (attribute.dynamicKeys) {
attribute.dynamicKeys.forEach((dynamicKey: DynamicKey): string => {
// For HTML previews it's not supposed to be showing variations
// because HTML doesn't have a way of doing that, so we'll choose
// ...well, we'll choose something.
// Prefer a default value
if (dynamicKey.defaultValue) {
attrs.push(dynamicKey.defaultValue);
return;
}
// when there's a multichoice just choose the first one
if (Array.isArray(dynamicKey.type)) {
attrs.push(dynamicKey.type[0].value);
return;
}
if (dynamicKey.ifTrueValue) {
// attrs.push(dynamicKey.ifTrueValue);
return;
}
// There can be dynamicKeys that don't match above conditions
// such as {"key":"id","type":"string","optional":false}
// and in such cases we'll just use the string "example"
attrs.push("example");
});
}
attr += attrs.join(" ");
attr += '"';
return attr;
}
onCloseElement = async ({ tagName }: OnCloseElement): Promise<void> => {
this.data += `</${tagName}>`;
};
onText = async ({ text }: OnText): Promise<void> => {
this.data += text;
};
onVariable = async ({ defaultValue }: OnVariable): Promise<void> => {
this.data += defaultValue;
};
serialize = async ({}: OnSerialize): Promise<Object> => {
const html = prettier.format(this.data, {
parser: "html",
printWidth: PRETTIER_LINE_WIDTH
});
return {
[`${this.dirname}/${this.template.id}.html`]: html
};
};
registerDynamicKey = (
key: string,
type: DynamicKeyType,
optional: boolean
): string => {
this.assignedDynamicKeys[key] = { type, optional };
return key;
};
generateIndex = (filesArr: string[]): Object => {
return {};
};
makeUsage = async (
code: TemplateUsages,
templatesById: TemplatesById
): Promise<FormatUsageResponse> => {
let templateCount = 1; // start at 1 because we only increment after usage
// and we want conventional counting, not starting at 0
let cssImports: string[] = [];
const render = async (
aCode: TemplateUsage,
depth: number
): Promise<string> => {
if (isString(aCode)) {
return (aCode as string).toString();
}
if (!aCode["templateId"]) {
return "";
}
const tagName = (aCode as TemplateUsageElement).templateId;
let template: TemplateInput | undefined;
if (!tagName.match(/[A-Z]/)) {
// If it has uppercase letters it's a Component, but if it
// only has lowercase then it's HTML.
const variables = (aCode as TemplateUsageElement).variables;
// otherwise it's HTML
templatesById[tagName] = {
id: tagName,
css: "",
html: `<${tagName} ${Object.keys(variables)
.filter(key => key !== "children")
.map(varName => ` ${varName}="${variables[varName]}"`)
.join("")}><mt-variable key="children"/></${tagName}>`
};
} else {
cssImports.push(tagName);
}
template = templatesById[tagName];
if (!template) {
throw new Error(
`makeUsage "templatesById" is lacking required key = "${
(aCode as TemplateUsageElement).templateId
}" in ${Object.keys(
templatesById
).sort()}. Required from ${JSON.stringify(aCode)}.`
);
}
const inputTemplate: TemplateInput =
templatesById[(aCode as TemplateUsageElement).templateId];
let html: string = template.html;
html = await insertDefaults(inputTemplate);
// const response: InsertIdsResponse = insertIds(html, templateCount, depth);
// if (response.hasIdSynonym) {
// templateCount++;
// }
// html = response.html;
const draw = async (
dynamicKey: DynamicKey | string | boolean,
isAttribute: boolean,
enumerations?: EnumOption[] | undefined
): Promise<string> => {
const value = (aCode as TemplateUsageElement).variables[
(dynamicKey as DynamicKey).key
];
if (value === undefined) {
if ((dynamicKey as DynamicKey).optional) {
return "";
}
return "";
throw new Error(
`From dynamicKey="${JSON.stringify(
dynamicKey
)}" we were unable to find key "${
(dynamicKey as DynamicKey).key
}" in [${Object.keys(
(aCode as TemplateUsageElement).variables
)}]. Raw: ${JSON.stringify(dynamicKey)}`
);
}
if (
typeof value === "boolean" ||
(dynamicKey as DynamicKey).type === "boolean"
) {
// FIXME: figure out how to nicely serialize boolean attribute values
// because `multiple="false"` may be treated as true, and what we
// really want is to remove 'multiple' itself.
//
// Our current approach replaces `{{thing}}` we don't have our regex
// covering the attribute name too, so it makes it hard to remove
// the attribute if the value is false.
//
// If value comes from jsxToUsageCode then it parses
// attr={false} as attr="false", so we use the HTML dynamicKey
// to decide if it's .type==="boolean" and then compare the string
// to work around this.
// TODO: Decide whether to reenable these
// const blockList = ["disabled", "checked", "readonly", "autofocus"];
// if (blockList.includes((dynamicKey as DynamicKey).key)) {
// return "";
// }
return value === true
? (dynamicKey as DynamicKey).ifTrueValue || ""
: "";
} else if (isString(value)) {
if (isString(enumerations) || enumerations === undefined) {
return value as string;
}
const enumOption:
| EnumOption
| undefined = (enumerations as EnumOption[]).find(
(enumOption: EnumOption) => enumOption.name === value
);
if (enumOption) {
return enumOption.value; // so that friendly names are converted to their values for HTML
}
} else {
// const results = await Promise.all(
// (value as TemplateUsage[]).map(value => render(value, depth + 1))
// );
return;
// results.join("");
}
};
const validateDynamicKey = (dynamicKey: DynamicKey): void => {
return; // TODO: reenable this
if (isString(aCode)) {
throw new Error(`aCode was a string "${aCode}".`);
}
if (
(aCode as TemplateUsageElement).variables &&
!(aCode as TemplateUsageElement).variables.hasOwnProperty(
dynamicKey.key
)
) {
if (dynamicKey.optional) {
return;
}
throw new Error(
`Couldn't find required key of "${
dynamicKey.key
}" in [${Object.keys(
(aCode as TemplateUsageElement).variables
)}]. Entire templateUsage=${JSON.stringify(aCode)}`
);
}
};
const renderByVariableElementKey = async (
key: string,
defaultValue?: string | undefined
): Promise<string> => {
const templateUsages:
| undefined
| TemplateUsage[]
| string
| boolean = (aCode as TemplateUsageElement).variables[
key
] as TemplateUsage[];
if (templateUsages === undefined) {
if (defaultValue) return defaultValue;
console.warn(
`MetaTemplate warning: aCode.variables[${key}] was undefined from aCode = ${JSON.stringify(
aCode
)}.`
);
return "";
}
if (isString(templateUsages)) {
return templateUsages.toString() as string;
} else if (typeof templateUsages === "boolean") {
// a boolean element value...huh?
return `[${(templateUsages as boolean).toString()}]`;
} else if (Array.isArray(templateUsages)) {
return (
await Promise.all(
templateUsages.map(value => render(value, depth + 1))
)
).join("");
}
throw Error(
`Unknown templateUsages type ${typeof templateUsages} from aCode.variables[${key}]. aCode = ${JSON.stringify(
aCode
)}. templateUsages: ${JSON.stringify(templateUsages)}`
);
};
// When there are attribute variable placeholders side-by-side
// then add a space.
// FIXME: Use a bloody HTML parser for all this code
html = html.replace(/\}\}\{\{/g, "}} {{");
html = await stringReplaceAsync(
html,
/{{(.*?)}}/gi,
async (match, middle) => {
const dynamicKey: DynamicKey = parseDynamicKey(middle);
validateDynamicKey(dynamicKey);
return draw(dynamicKey, true, dynamicKey.type as EnumOption[]);
}
);
// Some attributes will be valueless eg disabled="" which is considered
// false so we shouldn't render the attribute at all.
html = html.replace(/[a-zA-Z][a-zA-Z0-9-]+?="\s*"/gi, "");
// and certain boolean attributes should be converted to valueless
html = html.replace(/([a-zA-Z][a-zA-Z0-9-]+?)="true"/gi, (match, p1) => {
if (match.includes("value=")) {
// TODO: compare against HTML5 boolean attributes
return match;
}
return p1;
});
html = await stringReplaceAsync(
html,
/<mt-if[\s\S]*?<\/mt-if>/gi,
async match => {
// FIXME: obviously regexes and HTML don't mix well
// so if nested this will break. Replace with proper parser.
const key = getAttr(match, "key");
if (!key) throw Error(`Couldn't find key in "${match}"`);
const value = (aCode as TemplateUsageElement).variables[key];
if (!!value) {
const response = match
.substring(match.indexOf(">") + 1)
.replace("</mt-if>", "");
return response;
} else {
return "";
}
}
);
html = await stringReplaceAsync(
html,
/<mt-variable.*?\/>/gi,
async match => {
// self-closing <mt-variable stuff />
const key = getAttr(match, "key");
if (!key) throw Error(`Couldn't find key in "${match}"`);
return await renderByVariableElementKey(key);
}
);
html = await stringReplaceAsync(
html,
/<mt-variable[\s\S]*?\>([\s\S]*?)<\/mt-variable>/gi,
async (match, defaultValue) => {
// expanded <mt-variable stuff /> placeholder </mt-variable>
const key = getAttr(match, "key");
if (!key) throw Error(`Couldn't find key in "${match}"`);
return await renderByVariableElementKey(key, defaultValue);
}
);
return html;
};
const codeParts = await Promise.all(
code.map(async value => await render(value, 0))
);
let html: string = codeParts
.join("")
.replace(/\n/gi, "")
.replace(/"([\s\S]*?)"/gi, (match, attr) => {
// Many attributes will look like:
// name=" value\n\n \n value \n\n\n\n"
// with unnecessary whitespace in the middle of values
// So we'll normalize the value
let normalizedAttr = attr.trim();
const TWO_SPACES = " ";
while (normalizedAttr.indexOf(TWO_SPACES) !== -1) {
normalizedAttr = normalizedAttr.replace(/[\s]+/gi, " ");
}
return `"${normalizedAttr.trim()}"`;
});
cssImports = uniq(cssImports); // because things can be used twice
html = `<!--\nRemember to add these styles:\nin CSS: ${cssImports
.map(cssImport => `${cssImport}.css`)
.join(", ")}\nOR in Sass (SCSS): ${cssImports
.map(cssImport => `${cssImport}.scss`)
.join(", ")}\n-->\n${html}`;
html = prettier.format(html, {
parser: "html",
printWidth: PRETTIER_LINE_WIDTH
});
return { code: html };
};
}
const getAttr = (attr: string, name: string): string => {
if (!attr.includes(name)) return undefined;
const frag = JSDOM.fragment(attr);
let value;
const firstChild = frag.firstChild;
if (firstChild.nodeType === 1) {
value = (firstChild as Element).getAttribute(name);
value = value.replace(/\!/, "");
}
return value;
};
// Via http://xahlee.info/js/html5_non-closing_tag.html
const SELF_CLOSING_HTML_ELEMENTS = [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr"
];
type InsertIdsResponse = { hasIdSynonym: boolean; html };
const insertIds = (
html: string,
templateCount: number,
depth: number
): InsertIdsResponse => {
// MetaTemplate automatically adds some configurable areas of code,
// but sadly this is in attributes.ts and it's not currently used for
// 'Usage' (aka Example) generation. This means that there's an unfortunate
// mismatch between the variables that a MT Input vs MT Output, and 'Usage'
// only knows about the MT Input.
//
// This function is a temporary workaround until those concepts are merged
// in MetaTemplate, and we've got the same variables.
//
// What this function tries to do is automatically increment some attributes
// that are related to IDs as per ID_SYNONYMS.
//
// Imagine there are templates that include templates that include templates
// and they're in this kind of structure:
//
// Container
// - RadioButton
// - RadioButton
//
// If we autogenerated Ids for RadioButton we'd need to give them unique Ids, so
// we have some heuristics that check for the (1) nesting level of the import,
// and (2) the number of identical Ids at that level in the current template.
// (with the idea that 2+ identical Ids within a template is probably an internal
// Id reference that can be incremented)
//
// This is all a workaround until these concepts are properly unified in
// MetaTemplate.
let hasIdSynonym = false;
const newIds = {};
const dom = new JSDOM(`<html><body>${html}</body></html>`);
const body = dom.window.document.body;
ID_SYNONYMS.forEach(id => {
const elements = body.querySelectorAll(`[${id}]`);
elements.forEach(element => {
hasIdSynonym = true;
const value = element.getAttribute(id);
const newValue: string = value
.trim()
.split(" ")
.map((idRef: string): string => {
if (!newIds[idRef]) {
let referencesWithinThisTemplate = 0;
for (let i = 0; i < ID_SYNONYMS.length; i++) {
const attrName = ID_SYNONYMS[i];
referencesWithinThisTemplate += body.querySelectorAll(
`[${attrName}="${idRef}"]`
).length;
}
// On
// If depth > 0 required because
if (depth > 0 && referencesWithinThisTemplate > 1) {
// 2 or more... assume it's an id unique to this template and increment per template
newIds[idRef] = `${idRef}${templateCount}`;
} else {
// only 1 reference probably means the id is defined outside this template and shouldn't be incremented
newIds[idRef] = idRef;
}
}
return newIds[idRef];
})
.join(" ");
element.setAttribute(id, newValue);
});
});
const result = Array.from(body.childNodes)
.map(childNode => (childNode as Element).outerHTML)
.join("");
dom.window.close();
return {
hasIdSynonym,
html: result
};
};
async function insertDefaults(template: TemplateInput): Promise<string> {
// Start a browser environment
const browserArgs: BrowserInstanceArgs = {
template
};
const browser = await getBrowser(browserArgs);
const format: TemplateFormat = {
dirname: "html",
onText: async () => {},
onElement: async (element: OnElement) => {
return element.tagName;
},
onVariable: async (variable: OnVariable) => {},
onCloseElement: async () => {},
serialize: async () => {
return {};
},
generateIndex: (filesArr: string[]) => {
return {};
},
assignedDynamicKeys: {},
registerDynamicKey: (
key: string,
type: DynamicKeyType,
optional: boolean
): string => {
return key;
}
};
const walk = async (node: Node) => {
// oldAttributes: TemplateAttribute[],
// format: any,
// args: TemplateAttributesArgs
if (node.nodeType === 1) {
const element: Element = node as Element;
const tagName = element.tagName;
const args: TemplateAttributesArgs = {
tagName,
node: element,
format,
template
};
const attributes = await getTemplateAttributes(args);
await Promise.all(
attributes.map(async attribute => {
const newAttributeValue = serializeTemplateAttribute(attribute);
await element.setAttribute(attribute.key, newAttributeValue);
})
);
}
if (node.childNodes) {
await Promise.all(Array.from(node.childNodes).map(walk));
}
};
await Promise.all(browser.bodyNodes.map(walk));
const bodyChildNodes = await Promise.all(
browser.bodyNodes.map(async node => {
if (node.nodeType === 1) {
const element: any = node as Element;
return await element.getOuterHTML();
}
return node.nodeValue;
})
);
let newHTML = bodyChildNodes.join("");
newHTML = newHTML.replace(/>/g, "> "); // Until we have proper parsing that retains whitespace we'll have to insert some instead to allow Prettier to wrap lines nicely
await browser.dispose(true);
// await browser.disposeAll(true);
return newHTML;
}