@itrocks/template 0.0.32 → 0.0.34

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
@@ -7,11 +7,14 @@ type BlockStackEntry = {
7
7
  iteration: number;
8
8
  iterations: number;
9
9
  };
10
+ type Close = ')' | '}';
11
+ type Final = '' | '-->';
12
+ type Open = '(' | '{';
10
13
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
11
14
  export declare const frontScripts: SortedArray<string>;
12
15
  export declare class Template {
13
- data?: any;
14
- containerData?: any;
16
+ data?: any | undefined;
17
+ containerData?: any | undefined;
15
18
  blockBack: number;
16
19
  blockStack: BlockStackEntry[];
17
20
  doExpression: boolean;
@@ -25,6 +28,7 @@ export declare class Template {
25
28
  inLiteral: boolean;
26
29
  }[];
27
30
  target: string;
31
+ targetReplace: string;
28
32
  targetStack: string[];
29
33
  doLiteral: boolean;
30
34
  inLiteral: boolean;
@@ -49,7 +53,7 @@ export declare class Template {
49
53
  onTagClose?: (name: string) => void;
50
54
  parsers: VariableParser[];
51
55
  prefixes: string;
52
- constructor(data?: any, containerData?: any);
56
+ constructor(data?: any | undefined, containerData?: any | undefined);
53
57
  applyLiterals(text: string, parts?: string[]): string;
54
58
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
55
59
  combineLiterals(text: string, parts?: string[]): string;
@@ -89,11 +93,11 @@ export declare class Template {
89
93
  target: string;
90
94
  targetStack: string[];
91
95
  };
92
- include(path: string, data: any): Promise<any>;
96
+ include(path: string, data: any): Promise<string>;
93
97
  isContextClean(): boolean;
94
98
  literalTarget(index: number, isTitle?: boolean): void;
95
99
  parseBuffer(buffer: string): Promise<string>;
96
- parseExpression(data: any, close: string, finalClose?: string): Promise<boolean | undefined>;
100
+ parseExpression(data: any, open: Open | '<', close: Close, finalClose?: Final): Promise<boolean | undefined>;
97
101
  parseFile(fileName: string, containerFileName?: string): Promise<string>;
98
102
  parsePath(expression: string, data: any): Promise<any>;
99
103
  parseVariable(variable: string, data: any): Promise<any>;
@@ -101,7 +105,7 @@ export declare class Template {
101
105
  setSource(source: string, index?: number, start?: number, target?: string): void;
102
106
  skipBlock(): void;
103
107
  sourceToTarget(): void;
104
- startsExpression(char: string, open?: string, close?: string): boolean;
108
+ startsExpression(char: string, open?: Open, close?: Close): boolean;
105
109
  trimEndLine(string: string): string;
106
110
  }
107
111
  export {};
package/cjs/template.js CHANGED
@@ -23,6 +23,7 @@ class Template {
23
23
  tagName = '';
24
24
  tagStack = [];
25
25
  target = '';
26
+ targetReplace = '';
26
27
  targetStack = [];
27
28
  // literal
28
29
  doLiteral = false;
@@ -164,7 +165,6 @@ class Template {
164
165
  template.onTagOpen = this.onTagOpen;
165
166
  template.onTagOpened = this.onTagOpened;
166
167
  template.parsers = this.parsers;
167
- template.prefixes = this.prefixes;
168
168
  const parsed = await template.parseFile(((path[0] === node_path_1.sep) || (path[1] === ':'))
169
169
  ? path
170
170
  : (this.filePath + node_path_1.sep + path));
@@ -175,6 +175,12 @@ class Template {
175
175
  this.headLinks.push(...template.headLinks);
176
176
  const beginPosition = parsed.indexOf('<!--BEGIN-->');
177
177
  const endPosition = parsed.indexOf('<!--END-->');
178
+ if ((beginPosition === -1) && (parsed[1] === '!') && parsed.startsWith('<!DOCTYPE html>')) {
179
+ if (this.targetReplace === '') {
180
+ this.targetReplace = parsed;
181
+ }
182
+ return '';
183
+ }
178
184
  return (beginPosition > -1)
179
185
  ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
180
186
  : parsed;
@@ -231,11 +237,10 @@ class Template {
231
237
  + this.headTitle
232
238
  + this.target.slice(this.target.indexOf('</title>', position));
233
239
  }
234
- return this.target;
240
+ return (this.targetReplace !== '') ? this.targetReplace : this.target;
235
241
  }
236
- async parseExpression(data, close, finalClose = '') {
242
+ async parseExpression(data, open, close, finalClose = '') {
237
243
  const indexOut = this.index;
238
- let open = this.source[this.index];
239
244
  if (this.inLiteral && !this.literalParts.length) {
240
245
  this.targetStack.push(this.target);
241
246
  this.target = '';
@@ -428,7 +433,7 @@ class Template {
428
433
  let char = this.source[this.index];
429
434
  // expression
430
435
  if ((char === '{') && this.doExpression) {
431
- await this.parseExpression(data, '}');
436
+ await this.parseExpression(data, char, '}');
432
437
  continue;
433
438
  }
434
439
  // tag ?
@@ -495,7 +500,7 @@ class Template {
495
500
  this.index = tagIndex;
496
501
  this.target = '';
497
502
  this.inLiteral = false;
498
- const condition = await this.parseExpression(data, '}', '-->');
503
+ const condition = await this.parseExpression(data, '<', '}', '-->');
499
504
  this.blockStack.push({ blockStart, collection, condition, data, iteration, iterations });
500
505
  let blockData = condition ? (this.target ? data : undefined) : this.target;
501
506
  blockStart = this.index;
@@ -691,7 +696,7 @@ class Template {
691
696
  }
692
697
  // expression in attribute value
693
698
  if ((char === open) && this.doExpression) {
694
- await this.parseExpression(data, close);
699
+ await this.parseExpression(data, open, close);
695
700
  continue;
696
701
  }
697
702
  this.index++;
@@ -817,7 +822,7 @@ class Template {
817
822
  this.start = this.index;
818
823
  }
819
824
  startsExpression(char, open = '{', close = '}') {
820
- return RegExp('[a-z0-9"*.?\'' + open + close + this.prefixes + ']', 'i').test(char);
825
+ return RegExp(`[a-z0-9"'*./?` + open + close + this.prefixes + ']', 'i').test(char);
821
826
  }
822
827
  trimEndLine(string) {
823
828
  let index = string.length;
package/esm/template.d.ts CHANGED
@@ -7,11 +7,14 @@ type BlockStackEntry = {
7
7
  iteration: number;
8
8
  iterations: number;
9
9
  };
10
+ type Close = ')' | '}';
11
+ type Final = '' | '-->';
12
+ type Open = '(' | '{';
10
13
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
11
14
  export declare const frontScripts: SortedArray<string>;
12
15
  export declare class Template {
13
- data?: any;
14
- containerData?: any;
16
+ data?: any | undefined;
17
+ containerData?: any | undefined;
15
18
  blockBack: number;
16
19
  blockStack: BlockStackEntry[];
17
20
  doExpression: boolean;
@@ -25,6 +28,7 @@ export declare class Template {
25
28
  inLiteral: boolean;
26
29
  }[];
27
30
  target: string;
31
+ targetReplace: string;
28
32
  targetStack: string[];
29
33
  doLiteral: boolean;
30
34
  inLiteral: boolean;
@@ -49,7 +53,7 @@ export declare class Template {
49
53
  onTagClose?: (name: string) => void;
50
54
  parsers: VariableParser[];
51
55
  prefixes: string;
52
- constructor(data?: any, containerData?: any);
56
+ constructor(data?: any | undefined, containerData?: any | undefined);
53
57
  applyLiterals(text: string, parts?: string[]): string;
54
58
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
55
59
  combineLiterals(text: string, parts?: string[]): string;
@@ -89,11 +93,11 @@ export declare class Template {
89
93
  target: string;
90
94
  targetStack: string[];
91
95
  };
92
- include(path: string, data: any): Promise<any>;
96
+ include(path: string, data: any): Promise<string>;
93
97
  isContextClean(): boolean;
94
98
  literalTarget(index: number, isTitle?: boolean): void;
95
99
  parseBuffer(buffer: string): Promise<string>;
96
- parseExpression(data: any, close: string, finalClose?: string): Promise<boolean | undefined>;
100
+ parseExpression(data: any, open: Open | '<', close: Close, finalClose?: Final): Promise<boolean | undefined>;
97
101
  parseFile(fileName: string, containerFileName?: string): Promise<string>;
98
102
  parsePath(expression: string, data: any): Promise<any>;
99
103
  parseVariable(variable: string, data: any): Promise<any>;
@@ -101,7 +105,7 @@ export declare class Template {
101
105
  setSource(source: string, index?: number, start?: number, target?: string): void;
102
106
  skipBlock(): void;
103
107
  sourceToTarget(): void;
104
- startsExpression(char: string, open?: string, close?: string): boolean;
108
+ startsExpression(char: string, open?: Open, close?: Close): boolean;
105
109
  trimEndLine(string: string): string;
106
110
  }
107
111
  export {};
package/esm/template.js CHANGED
@@ -20,6 +20,7 @@ export class Template {
20
20
  tagName = '';
21
21
  tagStack = [];
22
22
  target = '';
23
+ targetReplace = '';
23
24
  targetStack = [];
24
25
  // literal
25
26
  doLiteral = false;
@@ -161,7 +162,6 @@ export class Template {
161
162
  template.onTagOpen = this.onTagOpen;
162
163
  template.onTagOpened = this.onTagOpened;
163
164
  template.parsers = this.parsers;
164
- template.prefixes = this.prefixes;
165
165
  const parsed = await template.parseFile(((path[0] === sep) || (path[1] === ':'))
166
166
  ? path
167
167
  : (this.filePath + sep + path));
@@ -172,6 +172,12 @@ export class Template {
172
172
  this.headLinks.push(...template.headLinks);
173
173
  const beginPosition = parsed.indexOf('<!--BEGIN-->');
174
174
  const endPosition = parsed.indexOf('<!--END-->');
175
+ if ((beginPosition === -1) && (parsed[1] === '!') && parsed.startsWith('<!DOCTYPE html>')) {
176
+ if (this.targetReplace === '') {
177
+ this.targetReplace = parsed;
178
+ }
179
+ return '';
180
+ }
175
181
  return (beginPosition > -1)
176
182
  ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
177
183
  : parsed;
@@ -228,11 +234,10 @@ export class Template {
228
234
  + this.headTitle
229
235
  + this.target.slice(this.target.indexOf('</title>', position));
230
236
  }
231
- return this.target;
237
+ return (this.targetReplace !== '') ? this.targetReplace : this.target;
232
238
  }
233
- async parseExpression(data, close, finalClose = '') {
239
+ async parseExpression(data, open, close, finalClose = '') {
234
240
  const indexOut = this.index;
235
- let open = this.source[this.index];
236
241
  if (this.inLiteral && !this.literalParts.length) {
237
242
  this.targetStack.push(this.target);
238
243
  this.target = '';
@@ -425,7 +430,7 @@ export class Template {
425
430
  let char = this.source[this.index];
426
431
  // expression
427
432
  if ((char === '{') && this.doExpression) {
428
- await this.parseExpression(data, '}');
433
+ await this.parseExpression(data, char, '}');
429
434
  continue;
430
435
  }
431
436
  // tag ?
@@ -492,7 +497,7 @@ export class Template {
492
497
  this.index = tagIndex;
493
498
  this.target = '';
494
499
  this.inLiteral = false;
495
- const condition = await this.parseExpression(data, '}', '-->');
500
+ const condition = await this.parseExpression(data, '<', '}', '-->');
496
501
  this.blockStack.push({ blockStart, collection, condition, data, iteration, iterations });
497
502
  let blockData = condition ? (this.target ? data : undefined) : this.target;
498
503
  blockStart = this.index;
@@ -688,7 +693,7 @@ export class Template {
688
693
  }
689
694
  // expression in attribute value
690
695
  if ((char === open) && this.doExpression) {
691
- await this.parseExpression(data, close);
696
+ await this.parseExpression(data, open, close);
692
697
  continue;
693
698
  }
694
699
  this.index++;
@@ -814,7 +819,7 @@ export class Template {
814
819
  this.start = this.index;
815
820
  }
816
821
  startsExpression(char, open = '{', close = '}') {
817
- return RegExp('[a-z0-9"*.?\'' + open + close + this.prefixes + ']', 'i').test(char);
822
+ return RegExp(`[a-z0-9"'*./?` + open + close + this.prefixes + ']', 'i').test(char);
818
823
  }
819
824
  trimEndLine(string) {
820
825
  let index = string.length;
package/package.json CHANGED
@@ -69,5 +69,5 @@
69
69
  "test": "jest"
70
70
  },
71
71
  "types": "./esm/template.d.ts",
72
- "version": "0.0.32"
72
+ "version": "0.0.34"
73
73
  }