nsp-server-pages 0.1.2 → 0.2.0

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.
Files changed (40) hide show
  1. package/cjs/index.js +6 -1
  2. package/cjs/package.json +1 -0
  3. package/cjs/src/app.js +17 -17
  4. package/cjs/src/catch.js +4 -3
  5. package/cjs/src/loaders.js +1 -1
  6. package/cjs/src/mount.js +6 -6
  7. package/cjs/src/parser/attr.js +92 -0
  8. package/{src/parse-el.js → cjs/src/parser/el.js} +15 -28
  9. package/cjs/src/parser/jsp.js +88 -0
  10. package/cjs/src/parser/scriptlet.js +47 -0
  11. package/cjs/src/parser/tag.js +130 -0
  12. package/{src/parse-text.js → cjs/src/parser/text.js} +13 -25
  13. package/cjs/src/stack-store.js +31 -0
  14. package/cjs/src/taglib.js +7 -7
  15. package/esm/index.js +2 -0
  16. package/esm/package.json +4 -0
  17. package/{src → esm/src}/app.js +16 -16
  18. package/{src → esm/src}/catch.js +4 -3
  19. package/{src → esm/src}/mount.js +6 -6
  20. package/esm/src/parser/attr.js +88 -0
  21. package/{cjs/src/parse-el.js → esm/src/parser/el.js} +11 -32
  22. package/esm/src/parser/jsp.js +83 -0
  23. package/esm/src/parser/scriptlet.js +43 -0
  24. package/esm/src/parser/tag.js +126 -0
  25. package/{cjs/src/parse-text.js → esm/src/parser/text.js} +9 -29
  26. package/esm/src/stack-store.js +27 -0
  27. package/{src → esm/src}/taglib.js +7 -7
  28. package/package.json +14 -13
  29. package/types/hooks.d.ts +103 -0
  30. package/{index.d.ts → types/index.d.ts} +61 -35
  31. package/cjs/src/parse-attr.js +0 -91
  32. package/cjs/src/parse-jsp.js +0 -206
  33. package/cjs/src/parse-scriptlet.js +0 -71
  34. package/index.js +0 -1
  35. package/src/parse-attr.js +0 -87
  36. package/src/parse-jsp.js +0 -201
  37. package/src/parse-scriptlet.js +0 -67
  38. /package/{src → esm/src}/bundle.js +0 -0
  39. /package/{src → esm/src}/concat.js +0 -0
  40. /package/{src → esm/src}/loaders.js +0 -0
@@ -0,0 +1,27 @@
1
+ export class StackStore {
2
+ constructor(value) {
3
+ this.stack = [];
4
+ if (arguments.length) {
5
+ this.set(value);
6
+ }
7
+ }
8
+ open(value) {
9
+ this.stack.unshift(value);
10
+ }
11
+ close() {
12
+ return this.stack.shift();
13
+ }
14
+ get() {
15
+ return this.stack[0];
16
+ }
17
+ set(value) {
18
+ this.stack[0] = value;
19
+ }
20
+ find(test) {
21
+ for (const data of this.stack) {
22
+ if (test(data)) {
23
+ return data;
24
+ }
25
+ }
26
+ }
27
+ }
@@ -1,6 +1,6 @@
1
1
  import { toXML } from "to-xml";
2
- export const addTagLib = (app, tagLibDef) => {
3
- const { fnMap, tagMap } = app;
2
+ export function addTagLib(tagLibDef) {
3
+ const { fnMap, tagMap } = this;
4
4
  const { ns, fn, tag } = tagLibDef;
5
5
  if (fn) {
6
6
  for (const name in fn) {
@@ -12,14 +12,14 @@ export const addTagLib = (app, tagLibDef) => {
12
12
  tagMap.set(`${ns}:${name}`, tag[name]);
13
13
  }
14
14
  }
15
- };
16
- export const prepareTag = (app, name, attr, body) => {
17
- const { tagMap } = app;
15
+ }
16
+ export function prepareTag(name, attr, body) {
17
+ const { tagMap } = this;
18
18
  const tagFn = tagMap.get(name) || defaultTagFn;
19
19
  const attrFn = !attr ? () => ({}) : (typeof attr !== "function") ? () => attr : attr;
20
- const tagDef = { name, app, attr: attrFn, body };
20
+ const tagDef = { name, app: this, attr: attrFn, body };
21
21
  return tagFn(tagDef);
22
- };
22
+ }
23
23
  const defaultTagFn = (tagDef) => {
24
24
  const { name } = tagDef;
25
25
  // tagDef.app.log(`Unknown tag: ${name}`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nsp-server-pages",
3
3
  "description": "NSP JavaScript Server Pages for Node.js",
4
- "version": "0.1.2",
4
+ "version": "0.2.0",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
@@ -13,25 +13,26 @@
13
13
  "devDependencies": {
14
14
  "@rollup/plugin-node-resolve": "^15.2.1",
15
15
  "@types/mocha": "^10.0.1",
16
- "@types/node": "^20.5.4",
16
+ "@types/node": "^20.5.6",
17
17
  "mocha": "^10.2.0",
18
- "typescript": "^5.1.6"
18
+ "typescript": "^5.2.2"
19
19
  },
20
20
  "exports": {
21
21
  "import": {
22
- "types": "./index.d.ts",
23
- "default": "./index.js"
22
+ "types": "./types/index.d.ts",
23
+ "default": "./esm/index.js"
24
24
  },
25
25
  "require": "./cjs/index.js"
26
26
  },
27
27
  "files": [
28
- "cjs/index.js",
28
+ "cjs/*.js",
29
29
  "cjs/package.json",
30
- "cjs/src/*.js",
31
- "index.d.ts",
32
- "index.js",
33
- "package.json",
34
- "src/*.js"
30
+ "cjs/src/**/*.js",
31
+ "esm/*.js",
32
+ "esm/package.json",
33
+ "esm/src/**/*.js",
34
+ "types/*.d.ts",
35
+ "package.json"
35
36
  ],
36
37
  "homepage": "https://github.com/kawanet/nsp-server-pages#readme",
37
38
  "keywords": [
@@ -41,7 +42,7 @@
41
42
  "taglib"
42
43
  ],
43
44
  "license": "Apache-2.0",
44
- "main": "index.js",
45
+ "main": "./esm/index.js",
45
46
  "repository": {
46
47
  "type": "git",
47
48
  "url": "git+https://github.com/kawanet/nsp-server-pages.git"
@@ -54,5 +55,5 @@
54
55
  },
55
56
  "sideEffects": false,
56
57
  "type": "module",
57
- "types": "./index.d.ts"
58
+ "types": "./types/index.d.ts"
58
59
  }
@@ -0,0 +1,103 @@
1
+ import type {NSP} from "./index";
2
+
3
+ type RuntimeErrorHookType = "error";
4
+
5
+ type RuntimeScriptletHookTypes =
6
+ "directive"
7
+ | "declaration"
8
+ | "expression"
9
+ | "scriptlet";
10
+
11
+ type BeforeParseHookTypes =
12
+ "before.parse.attr"
13
+ | "before.parse.comment"
14
+ | "before.parse.declaration"
15
+ | "before.parse.directive"
16
+ | "before.parse.el"
17
+ | "before.parse.expression"
18
+ | "before.parse.jsp"
19
+ | "before.parse.text";
20
+
21
+ type ParseHookTypes =
22
+ "parse.attr"
23
+ | "parse.comment"
24
+ | "parse.declaration"
25
+ | "parse.directive"
26
+ | "parse.el"
27
+ | "parse.expression"
28
+ | "parse.jsp"
29
+ | "parse.text";
30
+
31
+ type AfterParseHookTypes =
32
+ "after.parse.attr"
33
+ | "after.parse.comment"
34
+ | "after.parse.declaration"
35
+ | "after.parse.directive"
36
+ | "after.parse.el"
37
+ | "after.parse.expression"
38
+ | "after.parse.jsp"
39
+ | "after.parse.text";
40
+
41
+ type ParseTagHookTypes = `parse.tag.${string}`;
42
+
43
+ type KnownHookTypes =
44
+ RuntimeErrorHookType
45
+ | RuntimeScriptletHookTypes
46
+ | BeforeParseHookTypes
47
+ | ParseHookTypes
48
+ | AfterParseHookTypes
49
+ | ParseTagHookTypes;
50
+
51
+ export interface Hooks {
52
+ /**
53
+ * ==== RUNTIME HOOKS ====
54
+ *
55
+ * hook called when an Error thrown.
56
+ * return a string to output the error message and cancel the exception.
57
+ * return undefined to stop the page with the exception.
58
+ */
59
+ hook(type: RuntimeErrorHookType, fn: (e: Error, context: any) => string | void): void;
60
+
61
+ /**
62
+ * hooks called with JSP directive, declaration, scriptlet on runtime.
63
+ */
64
+ hook(type: RuntimeScriptletHookTypes, fn: (src: string, context: any) => string | void): void;
65
+
66
+ /**
67
+ * ==== TRANSPILER HOOKS ====
68
+ *
69
+ * hooks called with input JSP document before transpiling started.
70
+ * return a string to modify the input.
71
+ * return undefined not to modify the input.
72
+ */
73
+ hook(type: BeforeParseHookTypes, fn: (src: string) => string | void): void;
74
+
75
+ /**
76
+ * hooks called with JSP document to replace our default transpiler.
77
+ * return a string if you have own transpiler for the type.
78
+ * return undefined for our default transpiler to work.
79
+ */
80
+ hook(type: ParseHookTypes, fn: (src: string) => string | void): void;
81
+
82
+ /**
83
+ * hooks called with output JavaScript code after transpiling done.
84
+ * return a string to modify the output.
85
+ * return undefined not to modify the output.
86
+ */
87
+ hook(type: AfterParseHookTypes, fn: (src: string) => string | void): void;
88
+
89
+ /**
90
+ * hooks called with TagParserDef to transpile tag implementation inline.
91
+ *
92
+ * @example
93
+ * nsp.hook("parse.tag.c:set", tag => {
94
+ * return `v => { v[${tag.attr.get("var")}] = ${tag.attr.get("value")} }`;
95
+ * });
96
+ */
97
+ hook<A>(type: ParseTagHookTypes, fn: (tag: NSP.TagParserDef<A>) => string | void): void;
98
+
99
+ /**
100
+ * ==== OTHER HOOKS ====
101
+ */
102
+ hook(type: Exclude<string, KnownHookTypes>, fn: (...args: any[]) => any): void;
103
+ }
@@ -4,9 +4,11 @@
4
4
  * @see https://github.com/kawanet/nsp-server-pages
5
5
  */
6
6
 
7
+ import type {Hooks} from "./hooks.js";
8
+
7
9
  export const createNSP: (options?: NSP.Options) => NSP.App;
8
10
 
9
- declare namespace NSP {
11
+ export declare namespace NSP {
10
12
  type NodeFn<T> = (context: T) => string | Promise<string>;
11
13
 
12
14
  type Node<T> = string | NodeFn<T>;
@@ -52,10 +54,10 @@ declare namespace NSP {
52
54
  storeKey?: string;
53
55
 
54
56
  /**
55
- * indent size for JavaScript source generated
56
- * @default 0
57
+ * indent spaces for JavaScript source generated
58
+ * @default 0 (no indent)
57
59
  */
58
- indent?: number;
60
+ indent?: number | string;
59
61
 
60
62
  /**
61
63
  * add comments at toJS() result
@@ -74,23 +76,10 @@ declare namespace NSP {
74
76
  * @default false
75
77
  */
76
78
  nullish?: boolean;
77
-
78
- /**
79
- * expression filter before transpile starts
80
- */
81
- prefilter?: (src: string) => string;
82
-
83
- /**
84
- * expression filter after transpile done
85
- */
86
- postfilter?: (src: string) => string;
87
79
  }
88
80
 
89
- interface App {
90
- fnMap: Map<string, (...args: any[]) => any>;
91
- loaders: LoaderFn[];
81
+ interface App extends Hooks {
92
82
  options: Options;
93
- tagMap: Map<string, TagFn<any>>;
94
83
 
95
84
  /**
96
85
  * register a tag library
@@ -142,27 +131,20 @@ declare namespace NSP {
142
131
  mount(path: RegExp | string, fn: LoaderFn): void;
143
132
 
144
133
  /**
145
- * register a hook function
134
+ * register a hook function. see hooks.d.ts for detail
146
135
  */
147
- hook(type: "error", fn: (e: Error, context?: any) => string | void): void;
148
-
149
- hook(type: "directive", fn: (src: string, context?: any) => string | void): void;
150
-
151
- hook(type: "declaration", fn: (src: string, context?: any) => string | void): void;
152
-
153
- hook(type: "scriptlet", fn: (src: string, context?: any) => string | void): void;
154
136
 
155
- hook(type: string, fn: (...args: any[]) => any): void;
137
+ // hook(type: string, fn: (...args: any[]) => any): void;
156
138
 
157
139
  /**
158
140
  * parse a JSP document
159
141
  */
160
- parse(src: string): Parser;
142
+ parse(src: string): JspParser;
161
143
 
162
144
  /**
163
145
  * get a private data store in context
164
146
  */
165
- store<S>(context: any, key: string, initFn?: () => S): S;
147
+ store<P>(context: any, key: string): StackStore<P>;
166
148
 
167
149
  /**
168
150
  * generates a NodeFn for the tag
@@ -187,22 +169,66 @@ declare namespace NSP {
187
169
  tag?: { [name: string]: TagFn<any> };
188
170
  }
189
171
 
172
+ interface StackStore<P> {
173
+ /**
174
+ * set value to the store
175
+ */
176
+ set(value: P): void;
177
+
178
+ /**
179
+ * get value from the store
180
+ */
181
+ get(): P;
182
+
183
+ /**
184
+ * open a new layer
185
+ */
186
+ open(value?: P): void;
187
+
188
+ /**
189
+ * close the current layer
190
+ */
191
+ close(): P;
192
+
193
+ /**
194
+ * find a value from layers with the test function
195
+ */
196
+ find(test: (data: P) => boolean): P;
197
+ }
198
+
190
199
  interface ToJSOption {
191
- indent?: number;
200
+ LF?: string;
192
201
  }
193
202
 
194
- /**
195
- * Parser for JSP document
196
- */
197
- interface Parser {
203
+ interface Transpiler {
198
204
  /**
199
- * transpile the JSP document to JavaScript source code
205
+ * transpile to JavaScript source code
200
206
  */
201
207
  toJS(option?: ToJSOption): string;
208
+ }
202
209
 
210
+ /**
211
+ * Parser for JSP document
212
+ */
213
+ interface JspParser extends Transpiler {
203
214
  /**
204
215
  * compile the JSP document as a NodeFn
205
216
  */
206
217
  toFn<T>(): NodeFn<T>;
207
218
  }
219
+
220
+ interface TagParserDef<A> {
221
+ app: App;
222
+ name: string;
223
+ attr: AttrParser<A>;
224
+ body: string;
225
+ LF: string;
226
+ nextLF: string;
227
+ }
228
+
229
+ interface AttrParser<A> extends Transpiler {
230
+ keys(): (keyof A)[];
231
+
232
+ get(key: keyof A): string; // JavaScript
233
+ }
208
234
  }
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseAttr = void 0;
4
- const parse_text_js_1 = require("./parse-text.js");
5
- /**
6
- * Parser for HTML tag attributes <tagName attr="value"/>
7
- */
8
- const parseAttr = (app, src) => new AttrParser(app, src);
9
- exports.parseAttr = parseAttr;
10
- class AttrParser {
11
- constructor(app, src) {
12
- this.app = app;
13
- this.src = src;
14
- //
15
- }
16
- /**
17
- * Transpile HTML tag attributes to JavaScript source code
18
- */
19
- toJS(option) {
20
- return attrToJS(this.app, this.src, option);
21
- }
22
- /**
23
- * Compile HTML tag attributes to JavaScript function instance
24
- */
25
- toFn() {
26
- const { app } = this;
27
- const { nspName, vName } = app.options;
28
- const js = this.toJS();
29
- try {
30
- const fn = Function(nspName, vName, `return ${js}`);
31
- return (context) => fn(app, context);
32
- }
33
- catch (e) {
34
- app.log("AttrParser: " + js.substring(0, 1000));
35
- throw e;
36
- }
37
- }
38
- }
39
- /**
40
- * Transpile HTML tag attributes to JavaScript source code
41
- */
42
- const attrToJS = (app, tag, option) => {
43
- tag = tag?.replace(/^\s*<\S+\s*/s, "");
44
- tag = tag?.replace(/\s*\/?>\s*$/s, "");
45
- const indent = +app.options.indent || 0;
46
- const currentIndent = +option?.indent || 0;
47
- const nextIndent = currentIndent + indent;
48
- const currentLF = currentIndent ? "\n" + " ".repeat(currentIndent) : "\n";
49
- const nextLF = nextIndent ? "\n" + " ".repeat(nextIndent) : "\n";
50
- const keys = [];
51
- const index = {};
52
- tag?.replace(/([^\s='"]+)(\s*=(?:\s*"([^"]*)"|\s*'([^']*)'|([^\s='"]*)))?/g, (_, key, eq, v1, v2, v3) => {
53
- if (!index[key])
54
- keys.push(key);
55
- index[key] = (eq ? unescapeXML(v1 || v2 || v3 || "") : true);
56
- return "";
57
- });
58
- const items = keys.map(key => {
59
- let value = index[key];
60
- if (!/^[A-Za-z_]\w+$/.test(key)) {
61
- key = JSON.stringify(key);
62
- }
63
- if ("string" === typeof value) {
64
- value = (0, parse_text_js_1.parseText)(app, value).toJS({ indent: nextIndent });
65
- }
66
- return `${key}: ${value}`;
67
- });
68
- // no arguments
69
- if (!keys.length)
70
- return 'null';
71
- const js = items.join(`,${nextLF}`);
72
- const trailingComma = (keys.length > 1) ? "," : "";
73
- return `{${nextLF}${js}${trailingComma}${currentLF}}`;
74
- };
75
- const UNESCAPE = {
76
- "&amp;": "&",
77
- "&lt;": "<",
78
- "&gt;": ">",
79
- "&apos;": "'",
80
- "&quot;": '"'
81
- };
82
- const unescapeXML = (str) => {
83
- return str?.replace(/(&(?:lt|gt|amp|apos|quot|#(?:\d{1,6}|x[0-9a-fA-F]{1,5}));)/g, (str) => {
84
- if (str[1] === "#") {
85
- const code = (str[2] === "x") ? parseInt(str.substring(3), 16) : parseInt(str.substr(2), 10);
86
- if (code > -1)
87
- return String.fromCharCode(code);
88
- }
89
- return UNESCAPE[str] || str;
90
- });
91
- };
@@ -1,206 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jspToJS = exports.parseJSP = void 0;
4
- const parse_text_js_1 = require("./parse-text.js");
5
- const parse_attr_js_1 = require("./parse-attr.js");
6
- const parse_scriptlet_js_1 = require("./parse-scriptlet.js");
7
- const emptyText = {
8
- '""': true,
9
- "''": true,
10
- "``": true,
11
- "null": true,
12
- "undefined": true,
13
- "": true,
14
- };
15
- const isElement = (node) => ("function" === typeof node?.toJS);
16
- /**
17
- * Parser for JSP document
18
- */
19
- const parseJSP = (app, src) => new JspParser(app, src);
20
- exports.parseJSP = parseJSP;
21
- /**
22
- * Root element or an taglib element
23
- */
24
- class Element {
25
- constructor(app, tagName, tagLine) {
26
- this.app = app;
27
- this.tagName = tagName;
28
- this.tagLine = tagLine;
29
- this.children = [];
30
- //
31
- }
32
- append(node) {
33
- this.children.push(node);
34
- }
35
- /**
36
- * Transpile JSP document to JavaScript source code
37
- */
38
- toJS(option) {
39
- const { app, tagLine } = this;
40
- const { comment, nspName, trimSpaces, vName } = app.options;
41
- const indent = +app.options.indent || 0;
42
- const currentIndent = +option?.indent || 0;
43
- const nextIndent = currentIndent + indent;
44
- const currentLF = currentIndent ? "\n" + " ".repeat(currentIndent) : "\n";
45
- const nextLF = nextIndent ? "\n" + " ".repeat(nextIndent) : "\n";
46
- const { children } = this;
47
- const args = children.map(item => {
48
- if (isElement(item)) {
49
- return item.toJS({ indent: nextIndent });
50
- }
51
- else if (!/\S/.test(item)) {
52
- // item with only whitespace
53
- return (trimSpaces !== false) ? '""' : JSON.stringify(item);
54
- }
55
- else {
56
- if (trimSpaces !== false) {
57
- item = item.replace(/^\s*[\r\n]/s, "\n");
58
- item = item.replace(/\s*[\r\n]\s*$/s, "\n");
59
- item = item.replace(/^[ \t]+/s, " ");
60
- item = item.replace(/[ \t]+$/s, " ");
61
- }
62
- let js = (0, parse_text_js_1.parseText)(app, item).toJS({ indent: nextIndent });
63
- if (/\(.+?\)|\$\{.+?}/s.test(js)) {
64
- js = `${vName} => ${js}`; // array function
65
- }
66
- return js;
67
- }
68
- }).filter(v => !emptyText[v]);
69
- const hasBody = !!children.length;
70
- // keep at least single empty string if all arguments are empty strings
71
- if (hasBody && !args.length) {
72
- args.push('""');
73
- }
74
- const { tagName } = this;
75
- const isRoot = !tagName;
76
- const last = args.length - 1;
77
- args.forEach((v, idx) => {
78
- const isComment = /^\/\/[^\n]*$/s.test(v);
79
- if (idx !== last && !isComment) {
80
- args[idx] += ",";
81
- }
82
- else if (idx === last && isComment) {
83
- args[idx] += currentLF;
84
- }
85
- });
86
- let body = args.join(nextLF);
87
- const bodyL = /^`\n/s.test(body) ? (isRoot ? "" : " ") : nextLF;
88
- const bodyR = /(\n`|[)\s])$/s.test(body) ? "" : currentLF;
89
- if (isRoot) {
90
- return `${nspName}.bundle(${bodyL}${body}${bodyR})`; // root element
91
- }
92
- // attributes as the second argument
93
- let attr = (0, parse_attr_js_1.parseAttr)(app, tagLine).toJS({ indent: args.length ? nextIndent : currentIndent });
94
- if (/\(.+?\)|\$\{.+?}/s.test(attr)) {
95
- attr = `${vName} => (${attr})`; // array function
96
- }
97
- const commentV = comment ? `// ${tagLine?.replace(/\s*[\r\n]\s*/g, " ") ?? ""}${currentLF}` : "";
98
- const nameV = JSON.stringify(tagName);
99
- const hasAttr = /:/.test(attr);
100
- const attrV = (hasBody || hasAttr) ? `, ${attr}` : "";
101
- const bodyV = hasBody ? `,${bodyL}${body}${bodyR}` : "";
102
- return `${commentV}${nspName}.tag(${nameV}${attrV}${bodyV})`;
103
- }
104
- }
105
- /**
106
- * Tree of elements
107
- */
108
- class Tree {
109
- constructor(root) {
110
- this.root = root;
111
- this.tree = [];
112
- this.tree.push(root);
113
- }
114
- append(node) {
115
- this.tree.at(0).append(node);
116
- }
117
- open(node) {
118
- this.append(node);
119
- this.tree.unshift(node);
120
- }
121
- close(tagName) {
122
- const openTag = this.getTagName();
123
- if (openTag !== tagName) {
124
- throw new Error(`mismatch closing tag: ${openTag} !== ${tagName}`);
125
- }
126
- this.tree.shift();
127
- }
128
- getTagName() {
129
- return this.tree.at(0).tagName;
130
- }
131
- isRoot() {
132
- return this.tree.length === 1;
133
- }
134
- }
135
- class JspParser {
136
- constructor(app, src) {
137
- this.app = app;
138
- this.src = src;
139
- //
140
- }
141
- /**
142
- * Transpile JSP document to JavaScript source code
143
- */
144
- toJS(option) {
145
- const { app, src } = this;
146
- return (0, exports.jspToJS)(app, src, option);
147
- }
148
- /**
149
- * Compile JSP document to JavaScript function
150
- */
151
- toFn() {
152
- const { app } = this;
153
- const { nspName } = app.options;
154
- const js = this.toJS();
155
- try {
156
- const fn = Function(nspName, `return ${js}`);
157
- return fn(app);
158
- }
159
- catch (e) {
160
- app.log("JspParser: " + js?.substring(0, 1000));
161
- throw e;
162
- }
163
- }
164
- }
165
- const nameRE = `[A-Za-z][A-Za-z0-9]*`;
166
- const stringRE = `"(?:\\\\[.]|[^\\\\"])*"|'(?:\\\\[.]|[^\\\\'])*'`;
167
- const insideRE = `[^"']|${stringRE}`;
168
- const tagRegExp = new RegExp(`(</?${nameRE}:(?:${insideRE})*?>)|(<%(?:${insideRE})*?%>)`, "s");
169
- const jspToJS = (app, src, option) => {
170
- const root = new Element(app);
171
- const tree = new Tree(root);
172
- const array = src.split(tagRegExp);
173
- for (let i = 0; i < array.length; i++) {
174
- const i3 = i % 3;
175
- let str = array[i];
176
- if (i3 === 1 && str) {
177
- // taglib
178
- const tagName = str.match(/^<\/?([^\s=/>]+)/)?.[1];
179
- if (/^<\//.test(str)) {
180
- tree.close(tagName);
181
- continue;
182
- }
183
- const element = new Element(app, tagName, str);
184
- if (/\/\s*>$/.test(str)) {
185
- tree.append(element);
186
- }
187
- else {
188
- tree.open(element);
189
- }
190
- }
191
- else if (i3 === 2 && str) {
192
- // <% scriptlet %>
193
- const item = (0, parse_scriptlet_js_1.parseScriptlet)(app, str);
194
- tree.append(item);
195
- }
196
- else if (i3 === 0) {
197
- // text node
198
- tree.append(str);
199
- }
200
- }
201
- if (!tree.isRoot()) {
202
- throw new Error("missing closing tag: " + tree.getTagName());
203
- }
204
- return root.toJS(option);
205
- };
206
- exports.jspToJS = jspToJS;