nsp-server-pages 0.2.1 → 0.2.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/src/app.js CHANGED
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.App = void 0;
4
- const mount_js_1 = require("./mount.js");
4
+ const bundle_js_1 = require("./bundle.js");
5
+ const catch_js_1 = require("./catch.js");
6
+ const concat_js_1 = require("./concat.js");
5
7
  const loaders_js_1 = require("./loaders.js");
8
+ const mount_js_1 = require("./mount.js");
6
9
  const jsp_js_1 = require("./parser/jsp.js");
7
- const catch_js_1 = require("./catch.js");
8
- const bundle_js_1 = require("./bundle.js");
10
+ const store_js_1 = require("./store.js");
9
11
  const taglib_js_1 = require("./taglib.js");
10
- const concat_js_1 = require("./concat.js");
11
- const stack_store_js_1 = require("./stack-store.js");
12
12
  class App {
13
13
  constructor(options) {
14
14
  this.loaders = [];
@@ -85,7 +85,7 @@ class App {
85
85
  const map = (context[storeKey] ??= new Map());
86
86
  let value = map.get(key);
87
87
  if (value == null) {
88
- value = new stack_store_js_1.StackStore();
88
+ value = new store_js_1.Store();
89
89
  map.set(key, value);
90
90
  }
91
91
  return value;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jspToJS = exports.JSP = void 0;
4
+ const store_js_1 = require("../store.js");
4
5
  const scriptlet_js_1 = require("./scriptlet.js");
5
- const stack_store_js_1 = require("../stack-store.js");
6
6
  const tag_js_1 = require("./tag.js");
7
7
  /**
8
8
  * Parser for JSP document
@@ -44,7 +44,7 @@ const insideRE = `[^"']|${stringRE}`;
44
44
  const tagRegExp = new RegExp(`(</?${nameRE}:(?:${insideRE})*?>)|(<%(?:${insideRE})*?%>)`, "s");
45
45
  const jspToJS = (app, src, option) => {
46
46
  const root = new tag_js_1.Tag(app);
47
- const tree = new stack_store_js_1.StackStore(root);
47
+ const tree = new store_js_1.Store(root);
48
48
  const array = src.split(tagRegExp);
49
49
  for (let i = 0; i < array.length; i++) {
50
50
  const i3 = i % 3;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Store = void 0;
4
+ /**
5
+ * The Store class was implemented using a stack {stack: P[]} at first.
6
+ * Now it was changed to a linked list to allow access to the parent.
7
+ */
8
+ class Store {
9
+ constructor(value) {
10
+ this.item = { value };
11
+ }
12
+ open(value) {
13
+ this.item = { parent: this.item, value };
14
+ }
15
+ close() {
16
+ const item = this.item;
17
+ this.item = item.parent;
18
+ return item.value;
19
+ }
20
+ get() {
21
+ return this.item.value;
22
+ }
23
+ set(value) {
24
+ this.item.value = value;
25
+ }
26
+ find(test) {
27
+ let item = this.item;
28
+ while (item) {
29
+ if (test(item.value)) {
30
+ return item.value;
31
+ }
32
+ item = item.parent;
33
+ }
34
+ }
35
+ }
36
+ exports.Store = Store;
package/cjs/src/taglib.js CHANGED
@@ -2,17 +2,45 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.prepareTag = exports.addTagLib = void 0;
4
4
  const to_xml_1 = require("to-xml");
5
+ const isTagCon = (v) => ("function" === typeof v?.prototype?.render);
6
+ const tagConToTagFn = (Tag) => {
7
+ return (tag) => {
8
+ return (context) => {
9
+ const result = new Tag(tag, context).render();
10
+ if (result)
11
+ return result;
12
+ };
13
+ };
14
+ };
5
15
  function addTagLib(tagLibDef) {
6
16
  const { fnMap, tagMap } = this;
7
17
  const { ns, fn, tag } = tagLibDef;
8
18
  if (fn) {
9
19
  for (const name in fn) {
10
- fnMap.set(`${ns}:${name}`, fn[name]);
20
+ const impl = fn[name];
21
+ if (typeof impl === "function") {
22
+ // FnFn is called with App instance as this
23
+ fnMap.set(`${ns}:${name}`, impl.bind(this));
24
+ }
25
+ else if (impl != null) {
26
+ throw new Error(`Invalid taglib implementation: \${${ns}:${name}()}`);
27
+ }
11
28
  }
12
29
  }
13
30
  if (tag) {
14
31
  for (const name in tag) {
15
- tagMap.set(`${ns}:${name}`, tag[name]);
32
+ const impl = tag[name];
33
+ if (isTagCon(impl)) {
34
+ // NSP.TagCon
35
+ tagMap.set(`${ns}:${name}`, tagConToTagFn(impl));
36
+ }
37
+ else if (typeof impl === "function") {
38
+ // NSP.TagFn
39
+ tagMap.set(`${ns}:${name}`, impl);
40
+ }
41
+ else if (impl != null) {
42
+ throw new Error(`Invalid taglib implementation: <${ns}:${name}>`);
43
+ }
16
44
  }
17
45
  }
18
46
  }
package/esm/src/app.js CHANGED
@@ -1,11 +1,11 @@
1
- import { load, mount } from "./mount.js";
1
+ import { bundle } from "./bundle.js";
2
+ import { catchFn } from "./catch.js";
3
+ import { concat } from "./concat.js";
2
4
  import { FileLoader, JsLoader, JspLoader } from "./loaders.js";
5
+ import { load, mount } from "./mount.js";
3
6
  import { JSP } from "./parser/jsp.js";
4
- import { catchFn } from "./catch.js";
5
- import { bundle } from "./bundle.js";
7
+ import { Store } from "./store.js";
6
8
  import { addTagLib, prepareTag } from "./taglib.js";
7
- import { concat } from "./concat.js";
8
- import { StackStore } from "./stack-store.js";
9
9
  export class App {
10
10
  constructor(options) {
11
11
  this.loaders = [];
@@ -82,7 +82,7 @@ export class App {
82
82
  const map = (context[storeKey] ??= new Map());
83
83
  let value = map.get(key);
84
84
  if (value == null) {
85
- value = new StackStore();
85
+ value = new Store();
86
86
  map.set(key, value);
87
87
  }
88
88
  return value;
@@ -1,5 +1,5 @@
1
+ import { Store } from "../store.js";
1
2
  import { Scriptlet } from "./scriptlet.js";
2
- import { StackStore } from "../stack-store.js";
3
3
  import { Tag } from "./tag.js";
4
4
  /**
5
5
  * Parser for JSP document
@@ -40,7 +40,7 @@ const insideRE = `[^"']|${stringRE}`;
40
40
  const tagRegExp = new RegExp(`(</?${nameRE}:(?:${insideRE})*?>)|(<%(?:${insideRE})*?%>)`, "s");
41
41
  export const jspToJS = (app, src, option) => {
42
42
  const root = new Tag(app);
43
- const tree = new StackStore(root);
43
+ const tree = new Store(root);
44
44
  const array = src.split(tagRegExp);
45
45
  for (let i = 0; i < array.length; i++) {
46
46
  const i3 = i % 3;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The Store class was implemented using a stack {stack: P[]} at first.
3
+ * Now it was changed to a linked list to allow access to the parent.
4
+ */
5
+ export class Store {
6
+ constructor(value) {
7
+ this.item = { value };
8
+ }
9
+ open(value) {
10
+ this.item = { parent: this.item, value };
11
+ }
12
+ close() {
13
+ const item = this.item;
14
+ this.item = item.parent;
15
+ return item.value;
16
+ }
17
+ get() {
18
+ return this.item.value;
19
+ }
20
+ set(value) {
21
+ this.item.value = value;
22
+ }
23
+ find(test) {
24
+ let item = this.item;
25
+ while (item) {
26
+ if (test(item.value)) {
27
+ return item.value;
28
+ }
29
+ item = item.parent;
30
+ }
31
+ }
32
+ }
package/esm/src/taglib.js CHANGED
@@ -1,15 +1,43 @@
1
1
  import { toXML } from "to-xml";
2
+ const isTagCon = (v) => ("function" === typeof v?.prototype?.render);
3
+ const tagConToTagFn = (Tag) => {
4
+ return (tag) => {
5
+ return (context) => {
6
+ const result = new Tag(tag, context).render();
7
+ if (result)
8
+ return result;
9
+ };
10
+ };
11
+ };
2
12
  export function addTagLib(tagLibDef) {
3
13
  const { fnMap, tagMap } = this;
4
14
  const { ns, fn, tag } = tagLibDef;
5
15
  if (fn) {
6
16
  for (const name in fn) {
7
- fnMap.set(`${ns}:${name}`, fn[name]);
17
+ const impl = fn[name];
18
+ if (typeof impl === "function") {
19
+ // FnFn is called with App instance as this
20
+ fnMap.set(`${ns}:${name}`, impl.bind(this));
21
+ }
22
+ else if (impl != null) {
23
+ throw new Error(`Invalid taglib implementation: \${${ns}:${name}()}`);
24
+ }
8
25
  }
9
26
  }
10
27
  if (tag) {
11
28
  for (const name in tag) {
12
- tagMap.set(`${ns}:${name}`, tag[name]);
29
+ const impl = tag[name];
30
+ if (isTagCon(impl)) {
31
+ // NSP.TagCon
32
+ tagMap.set(`${ns}:${name}`, tagConToTagFn(impl));
33
+ }
34
+ else if (typeof impl === "function") {
35
+ // NSP.TagFn
36
+ tagMap.set(`${ns}:${name}`, impl);
37
+ }
38
+ else if (impl != null) {
39
+ throw new Error(`Invalid taglib implementation: <${ns}:${name}>`);
40
+ }
13
41
  }
14
42
  }
15
43
  }
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.2.1",
4
+ "version": "0.2.2",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
package/types/index.d.ts CHANGED
@@ -19,6 +19,8 @@ export declare namespace NSP {
19
19
 
20
20
  type TagFn<A, T = any> = (tag: TagDef<A, T>) => (NodeFn<T> | VoidFn<T>);
21
21
 
22
+ type TagCon<A, T = any> = { new(tag: NSP.TagDef<A>, context: T): TagClass };
23
+
22
24
  type LoaderFn = (path: string) => Promise<NodeFn<any> | undefined>;
23
25
 
24
26
  type Strings = string | Promise<string> | Strings[];
@@ -166,7 +168,11 @@ export declare namespace NSP {
166
168
  /**
167
169
  * tags
168
170
  */
169
- tag?: { [name: string]: TagFn<any> };
171
+ tag?: { [name: string]: TagFn<any> | TagCon<any> };
172
+ }
173
+
174
+ interface TagClass {
175
+ render(): string | Promise<string> | void | Promise<void>;
170
176
  }
171
177
 
172
178
  interface StackStore<P> {
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StackStore = void 0;
4
- class StackStore {
5
- constructor(value) {
6
- this.stack = [];
7
- if (arguments.length) {
8
- this.set(value);
9
- }
10
- }
11
- open(value) {
12
- this.stack.unshift(value);
13
- }
14
- close() {
15
- return this.stack.shift();
16
- }
17
- get() {
18
- return this.stack[0];
19
- }
20
- set(value) {
21
- this.stack[0] = value;
22
- }
23
- find(test) {
24
- for (const data of this.stack) {
25
- if (test(data)) {
26
- return data;
27
- }
28
- }
29
- }
30
- }
31
- exports.StackStore = StackStore;
@@ -1,27 +0,0 @@
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
- }