@ui5/webcomponents-tools 0.0.0-d0bcf47c7 → 0.0.0-d1315d658
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/CHANGELOG.md +643 -1
- package/README.md +5 -6
- package/assets-meta.js +153 -0
- package/bin/dev.js +12 -1
- package/components-package/eslint.js +34 -0
- package/components-package/nps.js +112 -44
- package/components-package/postcss.components.js +13 -13
- package/components-package/postcss.themes.js +43 -16
- package/components-package/vite.config.js +13 -0
- package/components-package/wdio.js +400 -327
- package/components-package/wdio.sync.js +368 -0
- package/icons-collection/nps.js +69 -28
- package/lib/copy-and-watch/index.js +145 -0
- package/lib/copy-list/index.js +28 -0
- package/lib/create-icons/index.js +124 -0
- package/lib/create-illustrations/index.js +161 -0
- package/lib/create-new-component/index.js +121 -0
- package/lib/create-new-component/jsFileContentTemplate.js +73 -0
- package/lib/create-new-component/tsFileContentTemplate.js +80 -0
- package/lib/dev-server/dev-server.js +66 -0
- package/lib/dev-server/virtual-index-html-plugin.js +52 -0
- package/lib/esm-abs-to-rel/index.js +58 -0
- package/lib/generate-custom-elements-manifest/index.js +327 -0
- package/lib/generate-js-imports/illustrations.js +72 -0
- package/lib/generate-json-imports/i18n.js +98 -0
- package/lib/generate-json-imports/themes.js +80 -0
- package/lib/hbs2lit/index.js +3 -0
- package/lib/hbs2lit/src/compiler.js +60 -0
- package/lib/hbs2lit/src/extendedAttributeMapping.js +12 -0
- package/lib/hbs2lit/src/includesReplacer.js +31 -0
- package/lib/hbs2lit/src/litVisitor2.js +278 -0
- package/lib/hbs2lit/src/partials2.js +51 -0
- package/lib/hbs2lit/src/partialsVisitor.js +187 -0
- package/lib/hbs2lit/src/svgProcessor.js +76 -0
- package/lib/hbs2ui5/RenderTemplates/LitRenderer.js +40 -0
- package/lib/hbs2ui5/index.js +119 -0
- package/lib/i18n/defaults.js +82 -0
- package/lib/i18n/toJSON.js +43 -0
- package/lib/jsdoc/config.json +29 -0
- package/lib/jsdoc/configTypescript.json +29 -0
- package/lib/jsdoc/plugin.js +2468 -0
- package/lib/jsdoc/preprocess.js +146 -0
- package/lib/jsdoc/template/publish.js +4120 -0
- package/lib/postcss-combine-duplicated-selectors/index.js +178 -0
- package/lib/postcss-css-to-esm/index.js +90 -0
- package/lib/postcss-css-to-json/index.js +47 -0
- package/lib/postcss-new-files/index.js +36 -0
- package/lib/postcss-p/postcss-p.mjs +14 -0
- package/lib/replace-global-core/index.js +25 -0
- package/lib/scoping/get-all-tags.js +37 -0
- package/lib/scoping/lint-src.js +31 -0
- package/lib/scoping/missing-dependencies.js +65 -0
- package/lib/scoping/report-tags-usage.js +28 -0
- package/lib/scoping/scope-test-pages.js +40 -0
- package/lib/test-runner/test-runner.js +71 -0
- package/package.json +55 -55
- package/bin/init-ui5-package.js +0 -3
- package/package-lock.json +0 -9994
@@ -0,0 +1,12 @@
|
|
1
|
+
function replaceEqualities(hbs) {
|
2
|
+
// converts the boolean attributes to lit like boolean attributes
|
3
|
+
return hbs.replace(/(\s+)(disabled|hidden|checked|readonly)\s*=\s*/g, " ?$2 = ")
|
4
|
+
// maps all the propertis to attributes
|
5
|
+
.replace(/([a-zA-Z][\w\-]*?)\s*=\s*"{{/g, "$1 = \"{{")
|
6
|
+
// maps the value attribute to property
|
7
|
+
.replace(/(value\s*=)/g, ".value =");
|
8
|
+
}
|
9
|
+
|
10
|
+
module.exports = {
|
11
|
+
map: replaceEqualities
|
12
|
+
};
|
@@ -0,0 +1,31 @@
|
|
1
|
+
const path = require("path");
|
2
|
+
const fs = require("fs").promises;
|
3
|
+
|
4
|
+
const replaceIncludes = async (file) => {
|
5
|
+
const filePath = path.dirname(file);
|
6
|
+
let fileContent = await fs.readFile(file, "utf-8");
|
7
|
+
|
8
|
+
const inclRegex = /{{>\s*include\s*["'](.+?)["']}}/g;
|
9
|
+
let match;
|
10
|
+
|
11
|
+
while((match = inclRegex.exec(fileContent)) !== null) {
|
12
|
+
inclRegex.lastIndex = 0;
|
13
|
+
|
14
|
+
let targetFile = match[1];
|
15
|
+
if (targetFile.startsWith(".")) {
|
16
|
+
// Relative path, f.e. {{>include "./Popup.hbs"}} or {{>include "../partials/Header.hbs"}}
|
17
|
+
targetFile = path.join(filePath, targetFile);
|
18
|
+
} else {
|
19
|
+
// Node module path, f.e. {{>include "@ui5/webcomponents/src/Popup.hbs"}}
|
20
|
+
targetFile = require.resolve(targetFile);
|
21
|
+
}
|
22
|
+
|
23
|
+
fileContent = fileContent.replace(match[0], await replaceIncludes(targetFile));
|
24
|
+
}
|
25
|
+
|
26
|
+
return fileContent;
|
27
|
+
};
|
28
|
+
|
29
|
+
module.exports = {
|
30
|
+
replace: replaceIncludes
|
31
|
+
};
|
@@ -0,0 +1,278 @@
|
|
1
|
+
const Handlebars = require("handlebars/dist/handlebars.min.js");
|
2
|
+
const path = require("path");
|
3
|
+
const Visitor = Handlebars.Visitor;
|
4
|
+
|
5
|
+
// skip ifDefined for event handlers and boolean attrs
|
6
|
+
let skipIfDefined = false;
|
7
|
+
|
8
|
+
// when true => an HTML node value, when false => an attribute value
|
9
|
+
let isNodeValue = false;
|
10
|
+
|
11
|
+
// when true => the current attribute is "style"
|
12
|
+
let isStyleAttribute = false;
|
13
|
+
|
14
|
+
// matches event handlers @click= and boolean attrs ?disabled=
|
15
|
+
const dynamicAttributeRgx = /\s(\?|@)([a-zA-Z|-]+)="?\s*$/;
|
16
|
+
|
17
|
+
if (!String.prototype.replaceAll) {
|
18
|
+
String.prototype.replaceAll = function(str, newStr){
|
19
|
+
|
20
|
+
// If a regex pattern
|
21
|
+
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
|
22
|
+
return this.replace(str, newStr);
|
23
|
+
}
|
24
|
+
|
25
|
+
// If a string
|
26
|
+
return this.replace(new RegExp(str, 'g'), newStr);
|
27
|
+
|
28
|
+
};
|
29
|
+
}
|
30
|
+
|
31
|
+
function HTMLLitVisitor(componentName, debug) {
|
32
|
+
this.blockCounter = 0;
|
33
|
+
this.keys = [];
|
34
|
+
this.blocks = {};
|
35
|
+
this.result = "";
|
36
|
+
this.mainBlock = "";
|
37
|
+
this.blockLevel = 0;
|
38
|
+
this.componentName = componentName
|
39
|
+
const blockParametersDefinitionTS = [`this: ${componentName}`, "context: UI5Element", "tags: string[]", "suffix: string | undefined"];
|
40
|
+
const blockParametersDefinitionJS = ["context", "tags", "suffix"];
|
41
|
+
this.blockParametersDefinition = process.env.UI5_TS ? blockParametersDefinitionTS : blockParametersDefinitionJS;
|
42
|
+
this.blockParametersUsage = ["this", "context", "tags", "suffix"];
|
43
|
+
this.paths = []; //contains all normalized relative paths
|
44
|
+
this.debug = debug;
|
45
|
+
if (this.debug) {
|
46
|
+
this.blockByNumber = [];
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
HTMLLitVisitor.prototype = new Visitor();
|
51
|
+
|
52
|
+
HTMLLitVisitor.prototype.Program = function(program) {
|
53
|
+
let key = `block${this.blockCounter++}`;
|
54
|
+
|
55
|
+
this.keys.push(key);
|
56
|
+
this.debug && this.blockByNumber.push(key);
|
57
|
+
|
58
|
+
// this.blocks[this.currentKey()] = "function " + this.currentKey() + ` (this: any, ` + this.blockParametersDefinition.join(", ") + ") { ";
|
59
|
+
this.blocks[this.currentKey()] = `function ${this.currentKey()} (${this.blockParametersDefinition.join(", ")}) { `;
|
60
|
+
|
61
|
+
if (this.keys.length > 1) { //it's a nested block
|
62
|
+
this.blocks[this.prevKey()] += this.currentKey() + ".call(" + this.blockParametersUsage.join(", ") + ")";
|
63
|
+
} else {
|
64
|
+
this.mainBlock = this.currentKey();
|
65
|
+
}
|
66
|
+
|
67
|
+
this.blocks[this.currentKey()] += "return html`";
|
68
|
+
Visitor.prototype.Program.call(this, program);
|
69
|
+
this.blocks[this.currentKey()] += "`;}";
|
70
|
+
|
71
|
+
this.keys.pop(key);
|
72
|
+
};
|
73
|
+
|
74
|
+
HTMLLitVisitor.prototype.ContentStatement = function(content) {
|
75
|
+
Visitor.prototype.ContentStatement.call(this, content);
|
76
|
+
// let content = content.orgiinal; // attribute="__ attribute = "__ attribute ="__
|
77
|
+
|
78
|
+
let contentStatement = content.original;
|
79
|
+
skipIfDefined = !!dynamicAttributeRgx.exec(contentStatement);
|
80
|
+
|
81
|
+
const closingIndex = contentStatement.lastIndexOf(">");
|
82
|
+
const openingIndex = contentStatement.lastIndexOf("<");
|
83
|
+
if (closingIndex !== -1 || openingIndex !== -1) { // Only change isNodeValue whenever < or > is found in the content statement
|
84
|
+
isNodeValue = closingIndex > openingIndex;
|
85
|
+
}
|
86
|
+
|
87
|
+
isStyleAttribute = !isNodeValue && contentStatement.match(/style *= *["']? *$/);
|
88
|
+
|
89
|
+
if (!isStyleAttribute && contentStatement.match(/style=/)) {
|
90
|
+
console.log("WARNING: style hard-coded", contentStatement);
|
91
|
+
}
|
92
|
+
|
93
|
+
// Scope custom element tags
|
94
|
+
contentStatement = contentStatement.replaceAll(/(<\/?\s*)([a-zA-Z0-9_]+-[a-zA-Z0-9_-]+)/g, "$1\${scopeTag(\"$2\", tags, suffix)}");
|
95
|
+
|
96
|
+
this.blocks[this.currentKey()] += contentStatement;
|
97
|
+
};
|
98
|
+
|
99
|
+
HTMLLitVisitor.prototype.MustacheStatement = function(mustache) {
|
100
|
+
Visitor.prototype.MustacheStatement.call(this, mustache);
|
101
|
+
|
102
|
+
if (mustache.path.original === "@index") {
|
103
|
+
this.blocks[this.currentKey()] += "${index}";
|
104
|
+
} else {
|
105
|
+
const path = normalizePath.call(this, mustache.path.original);
|
106
|
+
const hasCalculatingClasses = path.includes("this.classes");
|
107
|
+
|
108
|
+
let parsedCode = "";
|
109
|
+
|
110
|
+
if (isNodeValue && !mustache.escaped) {
|
111
|
+
parsedCode = `\${unsafeHTML(${path})}`;
|
112
|
+
} else if (hasCalculatingClasses) {
|
113
|
+
if (process.env.UI5_TS) {
|
114
|
+
parsedCode = `\${classMap(${path} as ClassMapValue)}`;
|
115
|
+
} else {
|
116
|
+
parsedCode = `\${classMap(${path})}`;
|
117
|
+
}
|
118
|
+
} else if (isStyleAttribute) {
|
119
|
+
parsedCode = `\${styleMap(${path})}`;
|
120
|
+
} else if (skipIfDefined){
|
121
|
+
parsedCode = `\${${path}}`;
|
122
|
+
} else {
|
123
|
+
parsedCode = `\${ifDefined(${path})}`;
|
124
|
+
}
|
125
|
+
|
126
|
+
this.blocks[this.currentKey()] += parsedCode;
|
127
|
+
}
|
128
|
+
};
|
129
|
+
|
130
|
+
HTMLLitVisitor.prototype.BlockStatement = function(block) {
|
131
|
+
if (block.path.original === "if") {
|
132
|
+
visitIfBlock.call(this, block);
|
133
|
+
} else if (block.path.original === "unless") {
|
134
|
+
visitUnlessBlock.call(this, block);
|
135
|
+
} else if (block.path.original === "each") {
|
136
|
+
visitEachBlock.call(this, block);
|
137
|
+
}
|
138
|
+
};
|
139
|
+
|
140
|
+
HTMLLitVisitor.prototype.currentKey = function() {
|
141
|
+
return this.keys[this.keys.length - 1];
|
142
|
+
};
|
143
|
+
|
144
|
+
HTMLLitVisitor.prototype.prevKey = function() {
|
145
|
+
return this.keys[this.keys.length - 2];
|
146
|
+
};
|
147
|
+
|
148
|
+
function visitSubExpression(mustache) {
|
149
|
+
this.acceptRequired(mustache, "path");
|
150
|
+
this.acceptArray(mustache.params);
|
151
|
+
this.acceptKey(mustache, "hash");
|
152
|
+
}
|
153
|
+
|
154
|
+
function visitIfBlock(block) {
|
155
|
+
visitSubExpression.call(this, block);
|
156
|
+
|
157
|
+
let params = normalizePath.call(this, block.params[0].original);
|
158
|
+
this.blocks[this.currentKey()] += "${ " + params + " ? ";
|
159
|
+
this.acceptKey(block, "program");
|
160
|
+
this.blocks[this.currentKey()] += " : ";
|
161
|
+
if (block.inverse) {
|
162
|
+
this.acceptKey(block, "inverse");
|
163
|
+
} else {
|
164
|
+
this.blocks[this.currentKey()] += "undefined";
|
165
|
+
}
|
166
|
+
this.blocks[this.currentKey()] += " }";
|
167
|
+
}
|
168
|
+
|
169
|
+
function visitUnlessBlock(block) {
|
170
|
+
visitSubExpression.call(this, block);
|
171
|
+
|
172
|
+
let params = normalizePath.call(this, block.params[0].original);
|
173
|
+
this.blocks[this.currentKey()] += "${ !" + params + " ? ";
|
174
|
+
this.acceptKey(block, "program");
|
175
|
+
this.blocks[this.currentKey()] += " : undefined }";
|
176
|
+
}
|
177
|
+
|
178
|
+
function visitEachBlock(block) {
|
179
|
+
var bParamAdded = false;
|
180
|
+
visitSubExpression.call(this, block);
|
181
|
+
|
182
|
+
const reapeatDirectiveParamsTS = "(item, index) => (item as typeof item & {_id?: any})._id || index, (item, index: number)";
|
183
|
+
const reapeatDirectiveParamsJS = "(item, index) => item._id || index, (item, index)";
|
184
|
+
const repleatDirectiveParams = process.env.UI5_TS ? reapeatDirectiveParamsTS : reapeatDirectiveParamsJS;
|
185
|
+
this.blocks[this.currentKey()] += "${ repeat(" + normalizePath.call(this, block.params[0].original) + ", " + repleatDirectiveParams + " => ";
|
186
|
+
this.paths.push(normalizePath.call(this, block.params[0].original));
|
187
|
+
this.blockLevel++;
|
188
|
+
|
189
|
+
// block params is [this, context, tags, suffix] for top level blocks
|
190
|
+
// blcok params is [this, context, tags, suffix, item, index] for nested blocks
|
191
|
+
if (!this.blockParametersUsage.includes("index")) {
|
192
|
+
// last item is not index, but an each block is processed, add the paramters for further nested blocks
|
193
|
+
bParamAdded = true;
|
194
|
+
if (process.env.UI5_TS) {
|
195
|
+
this.blockParametersDefinition.push("item: any");
|
196
|
+
this.blockParametersDefinition.push("index: number");
|
197
|
+
} else {
|
198
|
+
this.blockParametersDefinition.push("item");
|
199
|
+
this.blockParametersDefinition.push("index");
|
200
|
+
}
|
201
|
+
this.blockParametersUsage.push("item");
|
202
|
+
this.blockParametersUsage.push("index");
|
203
|
+
}
|
204
|
+
this.acceptKey(block, "program");
|
205
|
+
if (bParamAdded) {
|
206
|
+
// if parameters were added at this step, remove the last two
|
207
|
+
this.blockParametersDefinition.pop();
|
208
|
+
this.blockParametersDefinition.pop();
|
209
|
+
this.blockParametersUsage.pop();
|
210
|
+
this.blockParametersUsage.pop();
|
211
|
+
}
|
212
|
+
this.blockLevel--;
|
213
|
+
this.blocks[this.currentKey()] += ") }";
|
214
|
+
}
|
215
|
+
|
216
|
+
function normalizePath(sPath) {
|
217
|
+
let result = replaceAll(replaceAll(replaceAll(sPath, ".this", ""), "this.", ""), "this", "");
|
218
|
+
|
219
|
+
//read carefully - https://github.com/wycats/handlebars.js/issues/1028
|
220
|
+
//kpdecker commented on May 20, 2015
|
221
|
+
|
222
|
+
if (result.indexOf("@root") === 0) {
|
223
|
+
// Trying to access root context via the HBS "@root" variable.
|
224
|
+
// Example: {{@root.property}} compiles to "context.property" - called from anywhere within the template.
|
225
|
+
result = result.replace("@root", "this");
|
226
|
+
|
227
|
+
} else if (result.indexOf("../") === 0) {
|
228
|
+
let absolutePath;
|
229
|
+
const levelsUp = (result.match(/..\//g) || []).length;
|
230
|
+
|
231
|
+
if (this.blockLevel <= levelsUp) {
|
232
|
+
// Trying to access root context from nested loops.
|
233
|
+
// Example: {{../../property}} compiles to "context.property" - when currently in a nested level loop.
|
234
|
+
// Example: {{../../../property}} compile to "context.property" - when requested levels are not present. fallback to root context.
|
235
|
+
absolutePath = `this.${replaceAll(result,"../", "")}`;
|
236
|
+
} else {
|
237
|
+
// Trying to access upper context (one-level-up) and based on the current lelev, that could be "context" or "item".
|
238
|
+
// Example: {{../property}} compiles to "context.property" - when called in a top level loop.
|
239
|
+
// Example: {{../property}} compiles to "item.property" - when called in a nested level loop.
|
240
|
+
// TODO: the second example, although correctly generated to "item.property", "item" will point to the current object within the nested loop,
|
241
|
+
// not the upper level loop as intended. So accessing the upper loop from nested loop is currently not working.
|
242
|
+
absolutePath = replaceAll(this.paths[this.paths.length - 1 - levelsUp], ".", "/") + "/" + result;
|
243
|
+
}
|
244
|
+
|
245
|
+
result = replaceAll(path.normalize(absolutePath), path.sep, ".");
|
246
|
+
|
247
|
+
} else {
|
248
|
+
// When neither "@root", nor "../" are used, use the following contexts:
|
249
|
+
// - use "context" - for the top level of execution, e.g "this.blockLevel = 0".
|
250
|
+
// - use "item" - for any nested level, e.g "this.blockLevel > 0".
|
251
|
+
// Example:
|
252
|
+
//
|
253
|
+
// {{text}} -> compiles to "context.text"
|
254
|
+
// {{#each items}}
|
255
|
+
// Item text: {{text}}</div> -> compiles to "item.text"
|
256
|
+
// {{#each words}}
|
257
|
+
// Word text: {{text}}</div> -> compiles to "item.text"
|
258
|
+
// {{/each}}
|
259
|
+
// Item text: {{text}}</div> -> compiles to "item.text"
|
260
|
+
// {{/each}}
|
261
|
+
// {{text}} -> compiles to "context.text"
|
262
|
+
|
263
|
+
const blockPath = this.blockLevel > 0 ? "item" : "this";
|
264
|
+
result = result ? replaceAll(blockPath + "/" + result, "/", ".") : blockPath;
|
265
|
+
}
|
266
|
+
|
267
|
+
return result;
|
268
|
+
}
|
269
|
+
|
270
|
+
function replaceAll(str, find, repl) {
|
271
|
+
let sResult = str;
|
272
|
+
while (sResult.indexOf(find) !== -1) {
|
273
|
+
sResult = sResult.replace(find, repl);
|
274
|
+
}
|
275
|
+
return sResult;
|
276
|
+
}
|
277
|
+
|
278
|
+
module.exports = HTMLLitVisitor;
|
@@ -0,0 +1,51 @@
|
|
1
|
+
//collects the partials defined with the inline mustache syntax
|
2
|
+
//also cleans the handlebars template from the partials definitions
|
3
|
+
function collectPartials(hbs) {
|
4
|
+
let oResult = {};
|
5
|
+
|
6
|
+
const re = RegExp("{{#\\*inline\\s* \"(\\w+)\"\\s*}}((.|\\s)*?){{\\/inline}}");
|
7
|
+
const regexGroups = {partialKey: 1, partialDefinition: 2};
|
8
|
+
|
9
|
+
let m;
|
10
|
+
|
11
|
+
do {
|
12
|
+
m = re.exec(hbs);
|
13
|
+
if (m) {
|
14
|
+
// This is necessary to avoid infinite loops with zero-width matches
|
15
|
+
if (m.index === re.lastIndex) {
|
16
|
+
re.lastIndex++;
|
17
|
+
}
|
18
|
+
|
19
|
+
// The result can be accessed through the `m`-variable.
|
20
|
+
oResult[m[regexGroups.partialKey]] = m[regexGroups.partialDefinition];
|
21
|
+
|
22
|
+
//remove the partial from the hbs
|
23
|
+
hbs = hbs.replace(m[0], "");
|
24
|
+
}
|
25
|
+
} while (m);
|
26
|
+
|
27
|
+
return {partials: oResult, hbs: hbs};
|
28
|
+
}
|
29
|
+
|
30
|
+
//replaces the partials usages with their actual definitions
|
31
|
+
function replacePartials(hbs, mPartials) {
|
32
|
+
let sResultHbs = hbs;
|
33
|
+
for (let key in mPartials) {
|
34
|
+
if (mPartials.hasOwnProperty(key)) {
|
35
|
+
let val = mPartials[key];
|
36
|
+
let re = new RegExp("{{\\s*>\\s*" + key + "\\s*}}", "g"); //ex match: {{ > controlData }}
|
37
|
+
sResultHbs = sResultHbs.replace(re, val);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
return sResultHbs;
|
42
|
+
}
|
43
|
+
|
44
|
+
module.exports = {
|
45
|
+
resolve: function(hbs) {
|
46
|
+
let oResult = collectPartials(hbs);
|
47
|
+
let sResultHbs = replacePartials(oResult.hbs, oResult.partials);
|
48
|
+
|
49
|
+
return sResultHbs;
|
50
|
+
}
|
51
|
+
};
|
@@ -0,0 +1,187 @@
|
|
1
|
+
function Visitor() {
|
2
|
+
this.parents = [];
|
3
|
+
this.paths = [];
|
4
|
+
}
|
5
|
+
|
6
|
+
Visitor.prototype = {
|
7
|
+
constructor: Visitor,
|
8
|
+
mutating: true,
|
9
|
+
|
10
|
+
// Visits a given value. If mutating, will replace the value if necessary.
|
11
|
+
acceptKey: function(node, name) {
|
12
|
+
this.paths.push(name);
|
13
|
+
let value = this.accept(node[name]);
|
14
|
+
if (this.mutating) {
|
15
|
+
// Hacky sanity check: This may have a few false positives for type for the helper
|
16
|
+
// methods but will generally do the right thing without a lot of overhead.
|
17
|
+
if (value && !Visitor.prototype[value.type]) {
|
18
|
+
throw new Error("Unexpected node type \"" + value.type + "\" found when accepting " + name + " on " + node.type);
|
19
|
+
}
|
20
|
+
node[name] = value;
|
21
|
+
}
|
22
|
+
this.paths.pop();
|
23
|
+
},
|
24
|
+
|
25
|
+
// Performs an accept operation with added sanity check to ensure
|
26
|
+
// required keys are not removed.
|
27
|
+
acceptRequired: function(node, name) {
|
28
|
+
this.acceptKey(node, name);
|
29
|
+
|
30
|
+
if (!node[name]) {
|
31
|
+
throw new Error(node.type + " requires " + name);
|
32
|
+
}
|
33
|
+
},
|
34
|
+
|
35
|
+
// Traverses a given array. If mutating, empty respnses will be removed
|
36
|
+
// for child elements.
|
37
|
+
acceptArray: function(array) {
|
38
|
+
for (let i = 0, l = array.length; i < l; i++) {
|
39
|
+
this.acceptKey(array, i);
|
40
|
+
|
41
|
+
if (!array[i]) {
|
42
|
+
array.splice(i, 1);
|
43
|
+
i--;
|
44
|
+
l--;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
},
|
48
|
+
|
49
|
+
accept: function(object) {
|
50
|
+
if (!object) {
|
51
|
+
return;
|
52
|
+
}
|
53
|
+
|
54
|
+
/* istanbul ignore next: Sanity code */
|
55
|
+
if (!this[object.type]) {
|
56
|
+
throw new Error("Unknown type: " + object.type, object);
|
57
|
+
}
|
58
|
+
|
59
|
+
if (this.current) {
|
60
|
+
this.parents.unshift(this.current);
|
61
|
+
}
|
62
|
+
this.current = object;
|
63
|
+
|
64
|
+
let ret = this[object.type](object);
|
65
|
+
|
66
|
+
this.current = this.parents.shift();
|
67
|
+
|
68
|
+
if (!this.mutating || ret) {
|
69
|
+
return ret;
|
70
|
+
} else if (ret !== false) {
|
71
|
+
return object;
|
72
|
+
}
|
73
|
+
},
|
74
|
+
|
75
|
+
Program: function(program) {
|
76
|
+
this.paths.push("body");
|
77
|
+
this.acceptArray(program.body);
|
78
|
+
this.paths.pop();
|
79
|
+
},
|
80
|
+
|
81
|
+
MustacheStatement: visitSubExpression,
|
82
|
+
Decorator: visitSubExpression,
|
83
|
+
|
84
|
+
BlockStatement: visitBlock,
|
85
|
+
DecoratorBlock: visitBlock,
|
86
|
+
|
87
|
+
PartialStatement: visitPartial,
|
88
|
+
PartialBlockStatement: function(partial) {
|
89
|
+
visitPartial.call(this, partial);
|
90
|
+
|
91
|
+
this.acceptKey(partial, "program");
|
92
|
+
},
|
93
|
+
|
94
|
+
ContentStatement: function(/* content */) {},
|
95
|
+
CommentStatement: function(/* comment */) {},
|
96
|
+
|
97
|
+
SubExpression: visitSubExpression,
|
98
|
+
|
99
|
+
PathExpression: function(/* path */) {},
|
100
|
+
|
101
|
+
StringLiteral: function(/* string */) {},
|
102
|
+
NumberLiteral: function(/* number */) {},
|
103
|
+
BooleanLiteral: function(/* bool */) {},
|
104
|
+
UndefinedLiteral: function(/* literal */) {},
|
105
|
+
NullLiteral: function(/* literal */) {},
|
106
|
+
|
107
|
+
Hash: function(hash) {
|
108
|
+
this.paths.push("pairs");
|
109
|
+
this.acceptArray(hash.pairs);
|
110
|
+
this.paths.pop();
|
111
|
+
},
|
112
|
+
HashPair: function(pair) {
|
113
|
+
this.acceptRequired(pair, "value");
|
114
|
+
}
|
115
|
+
};
|
116
|
+
|
117
|
+
function visitSubExpression(mustache) {
|
118
|
+
this.acceptRequired(mustache, "path");
|
119
|
+
this.paths.push("params");
|
120
|
+
this.acceptArray(mustache.params);
|
121
|
+
this.paths.pop();
|
122
|
+
this.acceptKey(mustache, "hash");
|
123
|
+
}
|
124
|
+
function visitBlock(block) {
|
125
|
+
visitSubExpression.call(this, block);
|
126
|
+
|
127
|
+
this.acceptKey(block, "program");
|
128
|
+
this.acceptKey(block, "inverse");
|
129
|
+
}
|
130
|
+
function visitPartial(partial) {
|
131
|
+
this.acceptRequired(partial, "name");
|
132
|
+
this.paths.push("params");
|
133
|
+
this.acceptArray(partial.params);
|
134
|
+
this.paths.pop();
|
135
|
+
this.acceptKey(partial, "hash");
|
136
|
+
}
|
137
|
+
|
138
|
+
////////////
|
139
|
+
|
140
|
+
function PartialsVisitor() {
|
141
|
+
this.partialDefinitions = {};
|
142
|
+
this.partials = [];
|
143
|
+
}
|
144
|
+
|
145
|
+
PartialsVisitor.prototype = new Visitor();
|
146
|
+
|
147
|
+
PartialsVisitor.prototype.PartialStatement = function(node) {
|
148
|
+
this.partials.push({
|
149
|
+
nodes: this.paths.slice(0),
|
150
|
+
name: node.name.original
|
151
|
+
});
|
152
|
+
Visitor.prototype.PartialStatement.call(this, node);
|
153
|
+
};
|
154
|
+
|
155
|
+
PartialsVisitor.prototype.DecoratorBlock = function(node) {
|
156
|
+
if (node.path.original === "ui5.inline" || node.path.original === "inline") {
|
157
|
+
let name = node.params[0].original;
|
158
|
+
this.partialDefinitions[name] = Object.assign({}, node);
|
159
|
+
}
|
160
|
+
|
161
|
+
Visitor.prototype.DecoratorBlock.call(this, node);
|
162
|
+
|
163
|
+
return false;
|
164
|
+
};
|
165
|
+
|
166
|
+
PartialsVisitor.prototype.collect = function(node) {
|
167
|
+
return Visitor.prototype.accept(node);
|
168
|
+
};
|
169
|
+
|
170
|
+
PartialsVisitor.prototype.modify = function(node) {
|
171
|
+
for (let i = this.partials.length - 1; i >= 0; i--) {
|
172
|
+
let partial = this.partials[i];
|
173
|
+
let parentNode = node;
|
174
|
+
|
175
|
+
//find the parent node - it's always inside the body of some Program node
|
176
|
+
while (partial.nodes.length > 1) {
|
177
|
+
parentNode = parentNode[partial.nodes.shift()];
|
178
|
+
}
|
179
|
+
|
180
|
+
let nodeName = partial.nodes.shift();
|
181
|
+
if (Array.isArray(parentNode) && typeof (nodeName) === "number") {
|
182
|
+
parentNode.splice(nodeName, 1, ...this.partialDefinitions[partial.name].program.body);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
};
|
186
|
+
|
187
|
+
module.exports = PartialsVisitor;
|
@@ -0,0 +1,76 @@
|
|
1
|
+
|
2
|
+
const svgrx = new RegExp(/<svg[\s\S]*?>([\s\S]*?)<\/svg>/, 'g');
|
3
|
+
const blockrx = /block[0-9]+/g;
|
4
|
+
|
5
|
+
function processSVG(input) {
|
6
|
+
let matches;
|
7
|
+
let template = input;
|
8
|
+
let blockCounter = 0;
|
9
|
+
|
10
|
+
matches = getSVGMatches(template);
|
11
|
+
|
12
|
+
if (!matches.length) {
|
13
|
+
return template;
|
14
|
+
}
|
15
|
+
|
16
|
+
matches.forEach(match => {
|
17
|
+
//(1) extract the SVG content as a separate block
|
18
|
+
const svgContentGroup = match[1];
|
19
|
+
const block = getSVGBlock(svgContentGroup, ++blockCounter);
|
20
|
+
|
21
|
+
// (2) replace the SVG content with its block called, e.g ${blockSVG(context)}
|
22
|
+
template = template.replace(svgContentGroup, block.usage);
|
23
|
+
|
24
|
+
// (3) look for internal blocks in the SVG content and replace their `html with `svg
|
25
|
+
template = replaceInternalBlocks(template, svgContentGroup);
|
26
|
+
|
27
|
+
// (4) append the SVG block definiton (built in step 1), e.g const blockSVG = (context) => {return svg`.*`}
|
28
|
+
template += block.definition;
|
29
|
+
});
|
30
|
+
|
31
|
+
return template;
|
32
|
+
}
|
33
|
+
|
34
|
+
function getSVGMatches(template) {
|
35
|
+
let matches = [];
|
36
|
+
|
37
|
+
while (svgMatch = svgrx.exec(template)) {
|
38
|
+
matches.push(svgMatch);
|
39
|
+
if (svgrx.lastIndex === svgMatch.index) {
|
40
|
+
svgrx.lastIndex++;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
return matches;
|
45
|
+
}
|
46
|
+
|
47
|
+
function getSVGBlock(input, blockCounter) {
|
48
|
+
const definitionTS = `\nfunction blockSVG${blockCounter} (this: any, context: UI5Element, tags: string[], suffix: string | undefined) {
|
49
|
+
return svg\`${input}\`;
|
50
|
+
};`;
|
51
|
+
const definitionJS = `\nfunction blockSVG${blockCounter} (context, tags, suffix) {
|
52
|
+
return svg\`${input}\`;
|
53
|
+
};`;
|
54
|
+
|
55
|
+
return {
|
56
|
+
usage: `\${blockSVG${blockCounter}.call(this, context, tags, suffix)}`,
|
57
|
+
definition: process.env.UI5_TS ? definitionTS : definitionJS,
|
58
|
+
};
|
59
|
+
}
|
60
|
+
|
61
|
+
function replaceInternalBlocks(template, svgContent) {
|
62
|
+
const internalBlocks = svgContent.match(blockrx) || [];
|
63
|
+
|
64
|
+
internalBlocks.forEach(blockName => {
|
65
|
+
const rx = new RegExp(`function ${blockName}.*(html\`).*;`);
|
66
|
+
template = template.replace(rx, (match, p1) => {
|
67
|
+
return match.replace(p1, "svg\`");
|
68
|
+
});
|
69
|
+
});
|
70
|
+
|
71
|
+
return template;
|
72
|
+
}
|
73
|
+
|
74
|
+
module.exports = {
|
75
|
+
process: processSVG,
|
76
|
+
};
|
@@ -0,0 +1,40 @@
|
|
1
|
+
const tsImports = (controlName, hasTypes) => {
|
2
|
+
if (!process.env.UI5_TS) {
|
3
|
+
return "";
|
4
|
+
}
|
5
|
+
|
6
|
+
const importPrefix = process.env.UI5_BASE ? "../../../" : "@ui5/webcomponents-base/dist/"
|
7
|
+
|
8
|
+
return `import type UI5Element from "${importPrefix}UI5Element.js";
|
9
|
+
${importForControl(controlName, hasTypes)}
|
10
|
+
import type { ClassMapValue } from "${importPrefix}types.js";
|
11
|
+
`;
|
12
|
+
}
|
13
|
+
const importForControl = (controlName, hasTypes) => {
|
14
|
+
|
15
|
+
if (!hasTypes) {
|
16
|
+
return `type ${controlName} = any;`;
|
17
|
+
}
|
18
|
+
|
19
|
+
if (process.env.UI5_BASE) {
|
20
|
+
// base package has a component in `test/elements` instead of `src`
|
21
|
+
return `import type ${controlName} from "../../../../test/elements/${controlName}.js";`
|
22
|
+
}
|
23
|
+
return `import type ${controlName} from "../../${controlName}.js";`
|
24
|
+
}
|
25
|
+
|
26
|
+
const buildRenderer = (controlName, litTemplate, hasTypes) => {
|
27
|
+
const importPrefix = process.env.UI5_BASE ? "../../../" : "@ui5/webcomponents-base/dist/"
|
28
|
+
|
29
|
+
// typescript cannot process package imports for the same package and the paths are changed to relative for base package templates
|
30
|
+
return `/* eslint no-unused-vars: 0 */
|
31
|
+
import { html, svg, repeat, classMap, styleMap, ifDefined, unsafeHTML, scopeTag } from "${importPrefix}renderer/LitRenderer.js";
|
32
|
+
${tsImports(controlName, hasTypes)}
|
33
|
+
${litTemplate}
|
34
|
+
|
35
|
+
export default block0;`;
|
36
|
+
};
|
37
|
+
|
38
|
+
module.exports = {
|
39
|
+
generateTemplate: buildRenderer
|
40
|
+
};
|