@itrocks/template 0.3.0 → 0.3.2

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/cjs/template.d.ts CHANGED
@@ -68,6 +68,7 @@ export declare class Template {
68
68
  publicUrl: (path: string) => string;
69
69
  urlAttributes: string[];
70
70
  constructor(data?: any | undefined, containerData?: any | undefined);
71
+ private addDependency;
71
72
  applyLiterals(text: string, parts?: string[]): string;
72
73
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
73
74
  combineLiterals(text: string, parts?: string[]): string;
@@ -114,7 +115,7 @@ export declare class Template {
114
115
  parseBuffer(buffer: string): Promise<string>;
115
116
  parseExpression(data: any, open: Open | '<', close: Close, finalClose?: Final): Promise<boolean | undefined>;
116
117
  parseFile(fileName: string, containerFileName?: string | false): Promise<string>;
117
- parsePath(expression: string, data: any): Promise<any>;
118
+ parsePath(expression: string, data: any, preserveIncludes?: boolean): Promise<any>;
118
119
  parseVariable(variable: string, data: any): Promise<any>;
119
120
  parseVars(): Promise<string>;
120
121
  setSource(source: string, index?: number, start?: number, target?: string): void;
package/cjs/template.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Template = exports.HtmlResponse = exports.frontScripts = exports.depends = void 0;
4
4
  exports.templateDependsOn = templateDependsOn;
5
5
  const app_dir_1 = require("@itrocks/app-dir");
6
+ const class_type_1 = require("@itrocks/class-type");
6
7
  const rename_1 = require("@itrocks/rename");
7
8
  const sorted_array_1 = require("@itrocks/sorted-array");
8
9
  const promises_1 = require("node:fs/promises");
@@ -28,6 +29,12 @@ class HtmlResponse {
28
29
  toString() { return this.html; }
29
30
  }
30
31
  exports.HtmlResponse = HtmlResponse;
32
+ class IncludedHtml {
33
+ html;
34
+ constructor(html) {
35
+ this.html = html;
36
+ }
37
+ }
31
38
  class Template {
32
39
  data;
33
40
  containerData;
@@ -89,6 +96,9 @@ class Template {
89
96
  this.blockStack.push({ blockStart: 0, data: containerData, iteration: done });
90
97
  }
91
98
  }
99
+ addDependency(link) {
100
+ this.addLinks.push(link.replace(/(\s(?:href|src)\s*=\s*)(["'])(.*?)\2/g, (_match, attribute, quote, url) => attribute + quote + this.publicUrl(url) + quote));
101
+ }
92
102
  applyLiterals(text, parts = []) {
93
103
  return text.replace(/\$([0-9]+)/g, (_, index) => parts[+index]);
94
104
  }
@@ -141,7 +151,7 @@ class Template {
141
151
  }
142
152
  if (DEBUG)
143
153
  console.log('addLink(', dependency, ')');
144
- this.addLinks.push(dependency);
154
+ this.addDependency(dependency);
145
155
  continue;
146
156
  }
147
157
  dependency = (0, node_path_1.normalize)(dependency).slice(app_dir_1.appDir.length);
@@ -149,13 +159,13 @@ class Template {
149
159
  case 'css':
150
160
  if (DEBUG)
151
161
  console.log('addLink(', '<link href="' + dependency + '" rel="stylesheet">', ')');
152
- this.addLinks.push('<link href="' + dependency + '" rel="stylesheet">');
162
+ this.addDependency('<link href="' + dependency + '" rel="stylesheet">');
153
163
  continue;
154
164
  case 'js':
155
165
  exports.frontScripts.push(dependency);
156
166
  if (DEBUG)
157
167
  console.log('addLink(', '<script src="' + dependency + '" type="module"></script>', ')');
158
- this.addLinks.push('<script src="' + dependency + '" type="module"></script>');
168
+ this.addDependency('<script src="' + dependency + '" type="module"></script>');
159
169
  continue;
160
170
  }
161
171
  }
@@ -200,7 +210,8 @@ class Template {
200
210
  };
201
211
  }
202
212
  async include(path, data) {
203
- const template = new (Object.getPrototypeOf(this).constructor)(data, this.blockStack[0]?.data);
213
+ const TemplateType = (0, class_type_1.constructorOf)(this);
214
+ const template = new TemplateType(data, this.blockStack[0]?.data);
204
215
  template.addLinks = this.addLinks;
205
216
  template.applicationPath = this.applicationPath;
206
217
  template.doExpression = this.doExpression;
@@ -289,29 +300,15 @@ class Template {
289
300
  let addLink;
290
301
  const addLinks = new Array;
291
302
  while (addLink = this.addLinks.shift()) {
292
- for (const attribute of ['href', 'src']) {
293
- let start = addLink.indexOf(attribute + '=');
294
- if (start < 0)
295
- continue;
296
- start += attribute.length;
297
- while ((addLink[start] !== '"') && (addLink[start] !== "'")) {
298
- start++;
299
- }
300
- const quote = addLink[start++];
301
- const stop = addLink.indexOf(quote, start);
302
- const link = this.publicUrl(addLink.substring(start, stop));
303
- addLink = addLink.slice(0, start) + link + addLink.slice(stop);
304
- if (DEBUG)
305
- console.log('check(', link, ')');
306
- if (!this.doneLinks.includes(link)) {
307
- if (DEBUG)
308
- console.log('+ addLink(', addLink, ')');
309
- if (DEBUG)
310
- console.log('+ doneLink(', link, ')');
311
- addLinks.push(addLink);
312
- this.doneLinks.push(link);
313
- }
314
- }
303
+ const link = addLink.match(/\s(?:href|src)\s*=\s*(["'])(.*?)\1/)?.[2];
304
+ if (link === undefined || this.doneLinks.includes(link))
305
+ continue;
306
+ if (DEBUG)
307
+ console.log('+ addLink(', addLink, ')');
308
+ if (DEBUG)
309
+ console.log('+ doneLink(', link, ')');
310
+ addLinks.push(addLink);
311
+ this.doneLinks.push(link);
315
312
  }
316
313
  if (addLinks.length) {
317
314
  const position = this.target.lastIndexOf('>', this.target.indexOf('</head>')) + 1;
@@ -362,9 +359,12 @@ class Template {
362
359
  }
363
360
  const expression = this.target + this.source.substring(this.start, this.index - minus);
364
361
  const lastTarget = this.targetStack.pop();
365
- let parsed = await this.parsePath(expression, data);
362
+ let parsed = await this.parsePath(expression, data, true);
366
363
  const html = (parsed instanceof HtmlResponse) ? parsed.html : parsed;
367
- if ((typeof html === 'string') && /<[a-z]/i.test(html) && (this.publicUrl('/') !== '/')) {
364
+ if (parsed instanceof IncludedHtml) {
365
+ parsed = parsed.html;
366
+ }
367
+ else if ((typeof html === 'string') && /<[a-z]/i.test(html) && (this.publicUrl('/') !== '/')) {
368
368
  const template = new Template({});
369
369
  template.applicationPath = this.applicationPath;
370
370
  template.doExpression = false;
@@ -433,7 +433,9 @@ class Template {
433
433
  console.log('----- parseFile', containerFileName ? 'contained' : 'fetched', this.included ? 'included' : 'final', fileName);
434
434
  if (containerFileName) {
435
435
  const data = this.data;
436
- this.data = Object.assign({ content: () => this.include(fileName, data) }, this.blockStack[0]?.data);
436
+ this.data = Object.assign({
437
+ content: async () => new IncludedHtml(await this.include(fileName, data))
438
+ }, this.blockStack[0]?.data);
437
439
  return this.parseFile((0, node_path_1.normalize)(containerFileName));
438
440
  }
439
441
  this.fileName = fileName.substring(fileName.lastIndexOf(node_path_2.sep) + 1);
@@ -447,7 +449,7 @@ class Template {
447
449
  }
448
450
  return target;
449
451
  }
450
- async parsePath(expression, data) {
452
+ async parsePath(expression, data, preserveIncludes = false) {
451
453
  if (DEBUG)
452
454
  console.log('parsePath', expression);
453
455
  if (expression === '') {
@@ -455,6 +457,12 @@ class Template {
455
457
  }
456
458
  if (((expression[0] === '.') && ((expression[1] === '/') || ((expression[1] === '.') && (expression[2] === '/'))))
457
459
  || (expression[0] === '/')) {
460
+ const include = async (path, content) => {
461
+ const html = await this.include(this.includePath(path), content);
462
+ return preserveIncludes
463
+ ? new IncludedHtml(html)
464
+ : html;
465
+ };
458
466
  let expressionEnd = expression.length - 1;
459
467
  if (expression[expressionEnd] === '-') {
460
468
  let blockBack = 1;
@@ -464,13 +472,13 @@ class Template {
464
472
  expressionEnd--;
465
473
  }
466
474
  const blockStack = this.blockStack;
467
- return this.include(this.includePath(expression.slice(0, expressionEnd)), blockStack[blockStack.length - blockBack].data);
475
+ return include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
468
476
  }
469
477
  if (expression[expressionEnd] === ')') {
470
478
  const openPosition = expression.lastIndexOf('(');
471
- return this.include(this.includePath(expression.slice(0, openPosition)), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
479
+ return include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
472
480
  }
473
- return this.include(this.includePath(expression), data);
481
+ return include(expression, data);
474
482
  }
475
483
  let onlyDots = true;
476
484
  for (const c of expression) {
package/package.json CHANGED
@@ -5,6 +5,7 @@
5
5
  },
6
6
  "dependencies": {
7
7
  "@itrocks/app-dir": "latest",
8
+ "@itrocks/class-type": "latest",
8
9
  "@itrocks/rename": "latest",
9
10
  "@itrocks/sorted-array": "latest"
10
11
  },
@@ -53,5 +54,5 @@
53
54
  "build": "tsc",
54
55
  "test": "npm run build && node --test --test-isolation=none test/*.test.js"
55
56
  },
56
- "version": "0.3.0"
57
+ "version": "0.3.2"
57
58
  }