@fluffjs/cli 0.4.3 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CodeGenerator.d.ts +3 -0
- package/CodeGenerator.js +35 -13
- package/Parse5Helpers.d.ts +7 -1
- package/Parse5Helpers.js +5 -2
- package/TemplateParser.js +2 -1
- package/interfaces/ElementNode.d.ts +2 -0
- package/package.json +1 -1
package/CodeGenerator.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ export declare class CodeGenerator {
|
|
|
57
57
|
private readonly bindingsMap;
|
|
58
58
|
private rootFragment;
|
|
59
59
|
private readonly collectedTemplates;
|
|
60
|
+
private readonly namespaceStack;
|
|
60
61
|
constructor(componentSelectors?: Set<string>, componentSelector?: string);
|
|
61
62
|
static resetGlobalState(): void;
|
|
62
63
|
static internString(str: string): number;
|
|
@@ -80,6 +81,8 @@ export declare class CodeGenerator {
|
|
|
80
81
|
private renderNodesToParent;
|
|
81
82
|
private renderNodeToParent;
|
|
82
83
|
private renderElementToParent;
|
|
84
|
+
private getCurrentNamespace;
|
|
85
|
+
private renderToTemplateWithNamespace;
|
|
83
86
|
private renderTextToParent;
|
|
84
87
|
private isComponentTag;
|
|
85
88
|
private serializeBinding;
|
package/CodeGenerator.js
CHANGED
|
@@ -35,6 +35,7 @@ export class CodeGenerator {
|
|
|
35
35
|
bindingsMap = new Map();
|
|
36
36
|
rootFragment = null;
|
|
37
37
|
collectedTemplates = [];
|
|
38
|
+
namespaceStack = [Parse5Helpers.NS_HTML];
|
|
38
39
|
constructor(componentSelectors = new Set(), componentSelector = '') {
|
|
39
40
|
this.componentSelectors = componentSelectors;
|
|
40
41
|
this.componentSelector = componentSelector;
|
|
@@ -70,6 +71,8 @@ export class CodeGenerator {
|
|
|
70
71
|
generateHtml(template) {
|
|
71
72
|
this.rootFragment = parse5.parseFragment('');
|
|
72
73
|
this.collectedTemplates.length = 0;
|
|
74
|
+
this.namespaceStack.length = 0;
|
|
75
|
+
this.namespaceStack.push(Parse5Helpers.NS_HTML);
|
|
73
76
|
this.renderNodesToParent(template.root, this.rootFragment);
|
|
74
77
|
for (const tpl of this.collectedTemplates) {
|
|
75
78
|
tpl.parentNode = this.rootFragment;
|
|
@@ -314,9 +317,36 @@ export class CodeGenerator {
|
|
|
314
317
|
if (tagName.startsWith(RESTRICTED_ELEMENT_PREFIX)) {
|
|
315
318
|
tagName = tagName.slice(RESTRICTED_ELEMENT_PREFIX.length);
|
|
316
319
|
}
|
|
317
|
-
const
|
|
320
|
+
const currentNamespace = this.namespaceStack[this.namespaceStack.length - 1];
|
|
321
|
+
const elementNamespace = node.namespaceURI ?? currentNamespace;
|
|
322
|
+
const el = Parse5Helpers.createElement(tagName, attrs, elementNamespace);
|
|
318
323
|
Parse5Helpers.appendChild(parent, el);
|
|
324
|
+
if (elementNamespace !== currentNamespace) {
|
|
325
|
+
this.namespaceStack.push(elementNamespace);
|
|
326
|
+
}
|
|
319
327
|
this.renderNodesToParent(node.children, el);
|
|
328
|
+
if (elementNamespace !== currentNamespace) {
|
|
329
|
+
this.namespaceStack.pop();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
getCurrentNamespace() {
|
|
333
|
+
return this.namespaceStack[this.namespaceStack.length - 1];
|
|
334
|
+
}
|
|
335
|
+
renderToTemplateWithNamespace(nodes, attrName, attrValue) {
|
|
336
|
+
const tpl = Parse5Helpers.createElement('template', [{ name: attrName, value: attrValue }]);
|
|
337
|
+
const tplContent = Parse5Helpers.getTemplateContent(tpl);
|
|
338
|
+
const currentNs = this.getCurrentNamespace();
|
|
339
|
+
if (currentNs !== Parse5Helpers.NS_HTML) {
|
|
340
|
+
const wrapperTag = currentNs === Parse5Helpers.NS_SVG ? 'svg' : 'math';
|
|
341
|
+
const wrapper = Parse5Helpers.createElement(wrapperTag, [{ name: 'data-fluff-ns-wrapper', value: '' }], currentNs);
|
|
342
|
+
Parse5Helpers.appendChild(tplContent, wrapper);
|
|
343
|
+
this.renderNodesToParent(nodes, wrapper);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
this.renderNodesToParent(nodes, tplContent);
|
|
347
|
+
}
|
|
348
|
+
this.collectedTemplates.push(tpl);
|
|
349
|
+
return tpl;
|
|
320
350
|
}
|
|
321
351
|
renderTextToParent(node, parent) {
|
|
322
352
|
Parse5Helpers.appendText(parent, node.content);
|
|
@@ -435,9 +465,7 @@ export class CodeGenerator {
|
|
|
435
465
|
for (let i = 0; i < node.branches.length; i++) {
|
|
436
466
|
const branch = node.branches[i];
|
|
437
467
|
const templateId = `${this.componentSelector}-${id}-${i}`;
|
|
438
|
-
|
|
439
|
-
this.renderNodesToParent(branch.children, Parse5Helpers.getTemplateContent(tpl));
|
|
440
|
-
this.collectedTemplates.push(tpl);
|
|
468
|
+
this.renderToTemplateWithNamespace(branch.children, 'data-fluff-branch', templateId);
|
|
441
469
|
}
|
|
442
470
|
Parse5Helpers.appendComment(parent, `/fluff:if:${id}`);
|
|
443
471
|
}
|
|
@@ -454,13 +482,9 @@ export class CodeGenerator {
|
|
|
454
482
|
this.markerConfigs.set(id, config);
|
|
455
483
|
Parse5Helpers.appendComment(parent, `fluff:for:${id}`);
|
|
456
484
|
const templateId = `${this.componentSelector}-${id}`;
|
|
457
|
-
|
|
458
|
-
this.renderNodesToParent(node.children, Parse5Helpers.getTemplateContent(tpl));
|
|
459
|
-
this.collectedTemplates.push(tpl);
|
|
485
|
+
this.renderToTemplateWithNamespace(node.children, 'data-fluff-tpl', templateId);
|
|
460
486
|
if (node.emptyContent) {
|
|
461
|
-
|
|
462
|
-
this.renderNodesToParent(node.emptyContent, Parse5Helpers.getTemplateContent(emptyTpl));
|
|
463
|
-
this.collectedTemplates.push(emptyTpl);
|
|
487
|
+
this.renderToTemplateWithNamespace(node.emptyContent, 'data-fluff-empty', templateId);
|
|
464
488
|
}
|
|
465
489
|
Parse5Helpers.appendComment(parent, `/fluff:for:${id}`);
|
|
466
490
|
}
|
|
@@ -481,9 +505,7 @@ export class CodeGenerator {
|
|
|
481
505
|
for (let i = 0; i < node.cases.length; i++) {
|
|
482
506
|
const caseNode = node.cases[i];
|
|
483
507
|
const templateId = `${this.componentSelector}-${id}-${i}`;
|
|
484
|
-
|
|
485
|
-
this.renderNodesToParent(caseNode.children, Parse5Helpers.getTemplateContent(tpl));
|
|
486
|
-
this.collectedTemplates.push(tpl);
|
|
508
|
+
this.renderToTemplateWithNamespace(caseNode.children, 'data-fluff-case', templateId);
|
|
487
509
|
}
|
|
488
510
|
Parse5Helpers.appendComment(parent, `/fluff:switch:${id}`);
|
|
489
511
|
}
|
package/Parse5Helpers.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
import type * as parse5 from 'parse5';
|
|
2
|
+
import { type html } from 'parse5';
|
|
3
|
+
export type Parse5NS = html.NS;
|
|
1
4
|
import type { Parse5ChildNode, Parse5Document, Parse5DocumentFragment, Parse5Element, Parse5Node, Parse5ParentNode } from './Typeguards.js';
|
|
2
5
|
export type Parse5NodeVisitor = (node: Parse5Node) => boolean | undefined;
|
|
3
6
|
export declare class Parse5Helpers {
|
|
7
|
+
static readonly NS_HTML = parse5.html.NS.HTML;
|
|
8
|
+
static readonly NS_SVG = parse5.html.NS.SVG;
|
|
9
|
+
static readonly NS_MATHML = parse5.html.NS.MATHML;
|
|
4
10
|
static createElement(tagName: string, attrs: {
|
|
5
11
|
name: string;
|
|
6
12
|
value: string;
|
|
7
|
-
}[]): Parse5Element;
|
|
13
|
+
}[], namespaceURI?: Parse5NS): Parse5Element;
|
|
8
14
|
static appendChild(parent: Parse5ParentNode, child: Parse5ChildNode): void;
|
|
9
15
|
static appendText(parent: Parse5ParentNode, value: string): void;
|
|
10
16
|
static appendComment(parent: Parse5ParentNode, data: string): void;
|
package/Parse5Helpers.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { html as parse5Html } from 'parse5';
|
|
2
2
|
import { Typeguards } from './Typeguards.js';
|
|
3
3
|
export class Parse5Helpers {
|
|
4
|
-
static
|
|
4
|
+
static NS_HTML = parse5Html.NS.HTML;
|
|
5
|
+
static NS_SVG = parse5Html.NS.SVG;
|
|
6
|
+
static NS_MATHML = parse5Html.NS.MATHML;
|
|
7
|
+
static createElement(tagName, attrs, namespaceURI) {
|
|
5
8
|
const el = {
|
|
6
9
|
nodeName: tagName,
|
|
7
10
|
tagName,
|
|
8
11
|
attrs,
|
|
9
12
|
childNodes: [],
|
|
10
|
-
namespaceURI:
|
|
13
|
+
namespaceURI: namespaceURI ?? Parse5Helpers.NS_HTML,
|
|
11
14
|
parentNode: null
|
|
12
15
|
};
|
|
13
16
|
if (tagName === 'template') {
|
package/TemplateParser.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BindingInfo } from './BindingInfo.js';
|
|
2
2
|
import type { TemplateNode } from './TemplateNode.js';
|
|
3
|
+
import type { Parse5NS } from '../Parse5Helpers.js';
|
|
3
4
|
export interface ElementNode {
|
|
4
5
|
type: 'element';
|
|
5
6
|
tagName: string;
|
|
@@ -7,5 +8,6 @@ export interface ElementNode {
|
|
|
7
8
|
bindings: BindingInfo[];
|
|
8
9
|
children: TemplateNode[];
|
|
9
10
|
id?: string;
|
|
11
|
+
namespaceURI?: Parse5NS;
|
|
10
12
|
}
|
|
11
13
|
//# sourceMappingURL=ElementNode.d.ts.map
|