mancha 0.4.0 → 0.5.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.
@@ -0,0 +1,3 @@
1
+ {
2
+ "recommendations": ["github.copilot"]
3
+ }
package/README.md CHANGED
@@ -7,12 +7,12 @@ function, or as a `Gulp` plugin.
7
7
 
8
8
  Here are some of the things you can use `mancha` for.
9
9
 
10
- ### Replace simple variables using `{{value}}` format
10
+ ### Replace simple variables using `{{ value }}` format
11
11
 
12
12
  index.html:
13
13
 
14
14
  ```html
15
- <span>Hello {{name}}</span>
15
+ <span>Hello {{ name }}</span>
16
16
  ```
17
17
 
18
18
  Command:
@@ -29,17 +29,17 @@ Result:
29
29
 
30
30
  ### Include files from a relative path using the `<include>` tag
31
31
 
32
- hello-world.html:
32
+ hello-name.html:
33
33
 
34
34
  ```html
35
- <span>Hello World</span>
35
+ <span>Hello {{ name }}</span>
36
36
  ```
37
37
 
38
38
  index.html:
39
39
 
40
40
  ```html
41
41
  <div>
42
- <include src="./hello-world.html"></include>
42
+ <include src="./hello-name.html" data-name="World"></include>
43
43
  </div>
44
44
  ```
45
45
 
@@ -65,16 +65,16 @@ To use `mancha` on the client (browser), use the `mancha.js` bundled file availa
65
65
 
66
66
  ```html
67
67
  <body>
68
- <span>Hello, {{name}}!</span>
68
+ <span>Hello, {{ name }}!</span>
69
69
  </body>
70
70
 
71
- <script src="//unpkg.com/mancha" data-vars='{"name": "John"}' target="body" init></script>
71
+ <script src="//unpkg.com/mancha" data-name="John" target="body" init></script>
72
72
  ```
73
73
 
74
74
  Script tag attributes:
75
75
 
76
76
  - `init`: whether to automatically render upon script load
77
- - `data-vars`: JSON string with key-value pairs, where `{{key}}` will be replaced with `{{value}}`
77
+ - `data-name`: dataset atribute, where `data-{{key}}` will be replaced with the attribute's value
78
78
  - `target`: comma-separated document elements to render e.g. "body" or "head,body" (defaults to "body")
79
79
 
80
80
  For a more complete example, see [examples/browser](./examples/browser).
@@ -133,7 +133,7 @@ will need to be separately hosted by a static server, although you can also gene
133
133
  containing HTML on demand.
134
134
 
135
135
  ```js
136
- import { renderRemotePath } from "mancha/dist/web"
136
+ import { renderRemotePath } from "mancha/dist/core"
137
137
 
138
138
  const VARS = {...};
139
139
  const HTML_ROOT = "https://example.com/html";
@@ -156,13 +156,13 @@ gulp.src(...).pipe(mancha({"myvar": "myval"})).pipe(...)
156
156
  ```
157
157
 
158
158
  The first argument consists of a dictionary of `<key, value>` pairs of literal string replacements.
159
- `key` will become `{{key}}` before replacing it with `value` in the processed files. For example,
159
+ `key` will become `{{ key }}` before replacing it with `value` in the processed files. For example,
160
160
  if we passed `{"name": "World"}` as the argument:
161
161
 
162
162
  Source:
163
163
 
164
164
  ```html
165
- <div>Hello {{name}}</div>
165
+ <div>Hello {{ name }}</div>
166
166
  ```
167
167
 
168
168
  Result:
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Helper function used to escape HTML attribute values.
3
+ * See: https://stackoverflow.com/a/9756789
4
+ */
5
+ export declare function encodeHtmlAttrib(value?: string): string;
6
+ /** Inverse the operation of [encodeHtmlAttrib] */
7
+ export declare function decodeHtmlAttrib(value?: string): string;
8
+ /**
9
+ * Converts from an attribute name to camelCase, e.g. `foo-bar` becomes `fooBar`.
10
+ * @param name attribute name
11
+ * @returns camel-cased attribute name
12
+ */
13
+ export declare function attributeNameToCamelCase(name: string): string;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.attributeNameToCamelCase = exports.decodeHtmlAttrib = exports.encodeHtmlAttrib = void 0;
4
+ /**
5
+ * Helper function used to escape HTML attribute values.
6
+ * See: https://stackoverflow.com/a/9756789
7
+ */
8
+ function encodeHtmlAttrib(value) {
9
+ var _a, _b, _c, _d, _e, _f, _g;
10
+ return ((_g = (_f = (_e = (_d = (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.replace(/&/g, "&amp;")) === null || _a === void 0 ? void 0 : _a.replace(/'/g, "&apos;")) === null || _b === void 0 ? void 0 : _b.replace(/"/g, "&quot;")) === null || _c === void 0 ? void 0 : _c.replace(/</g, "&lt;")) === null || _d === void 0 ? void 0 : _d.replace(/>/g, "&gt;")) === null || _e === void 0 ? void 0 : _e.replace(/\r\n/g, "&#13;")) === null || _f === void 0 ? void 0 : _f.replace(/[\r\n]/g, "&#13;")) !== null && _g !== void 0 ? _g : "");
11
+ }
12
+ exports.encodeHtmlAttrib = encodeHtmlAttrib;
13
+ /** Inverse the operation of [encodeHtmlAttrib] */
14
+ function decodeHtmlAttrib(value) {
15
+ var _a, _b, _c, _d, _e, _f;
16
+ return ((_f = (_e = (_d = (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.replace(/&amp;/g, "&")) === null || _a === void 0 ? void 0 : _a.replace(/&apos;/g, "'")) === null || _b === void 0 ? void 0 : _b.replace(/&quot;/g, '"')) === null || _c === void 0 ? void 0 : _c.replace(/&lt;/g, "<")) === null || _d === void 0 ? void 0 : _d.replace(/&gt;/g, ">")) === null || _e === void 0 ? void 0 : _e.replace(/&#13;/g, "\n")) !== null && _f !== void 0 ? _f : "");
17
+ }
18
+ exports.decodeHtmlAttrib = decodeHtmlAttrib;
19
+ /**
20
+ * Converts from an attribute name to camelCase, e.g. `foo-bar` becomes `fooBar`.
21
+ * @param name attribute name
22
+ * @returns camel-cased attribute name
23
+ */
24
+ function attributeNameToCamelCase(name) {
25
+ return name.replace(/-./g, (c) => c[1].toUpperCase());
26
+ }
27
+ exports.attributeNameToCamelCase = attributeNameToCamelCase;
package/dist/browser.d.ts CHANGED
@@ -1,2 +1,9 @@
1
- import * as Mancha from "./web.js";
1
+ import { IRenderer, ParserParams, RendererParams } from "./core";
2
+ declare class RendererImpl extends IRenderer {
3
+ protected readonly fsroot: string;
4
+ parseHTML(content: string, params?: ParserParams): DocumentFragment;
5
+ serializeHTML(root: Node | DocumentFragment): string;
6
+ renderLocalPath(fpath: string, params?: RendererParams & ParserParams): Promise<DocumentFragment>;
7
+ }
8
+ declare const Mancha: RendererImpl;
2
9
  export default Mancha;
package/dist/browser.js CHANGED
@@ -10,20 +10,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  var _a, _b, _c, _d;
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- const Mancha = require("./web.js");
13
+ const core_1 = require("./core");
14
+ class RendererImpl extends core_1.IRenderer {
15
+ constructor() {
16
+ super(...arguments);
17
+ this.fsroot = (0, core_1.folderPath)(self.location.href);
18
+ }
19
+ parseHTML(content, params = { isRoot: false }) {
20
+ if (params.isRoot) {
21
+ return new DOMParser().parseFromString(content, "text/html");
22
+ }
23
+ else {
24
+ const range = document.createRange();
25
+ range.selectNodeContents(document.body);
26
+ return range.createContextualFragment(content);
27
+ }
28
+ }
29
+ serializeHTML(root) {
30
+ return new XMLSerializer().serializeToString(root).replace(/\s?xmlns="[^"]+"/gm, "");
31
+ }
32
+ renderLocalPath(fpath, params) {
33
+ throw new Error("Not implemented.");
34
+ }
35
+ }
36
+ const Mancha = new RendererImpl();
14
37
  self["Mancha"] = Mancha;
15
- if ((_b = (_a = self.document) === null || _a === void 0 ? void 0 : _a.currentScript) === null || _b === void 0 ? void 0 : _b.hasAttribute("init")) {
16
- const vars = JSON.parse(self.document.currentScript.dataset["vars"] || "{}");
17
- const attributes = Array.from(((_c = self.document.currentScript) === null || _c === void 0 ? void 0 : _c.attributes) || []).reduce((dict, attr) => Object.assign(dict, { [attr.name]: attr.value }), {});
18
- const targets = ((_d = attributes["target"]) === null || _d === void 0 ? void 0 : _d.split(",")) || ["body"];
38
+ const currentScript = (_a = self.document) === null || _a === void 0 ? void 0 : _a.currentScript;
39
+ if ((_c = (_b = self.document) === null || _b === void 0 ? void 0 : _b.currentScript) === null || _c === void 0 ? void 0 : _c.hasAttribute("init")) {
40
+ Mancha.update(Object.assign({}, currentScript === null || currentScript === void 0 ? void 0 : currentScript.dataset));
41
+ const debug = currentScript === null || currentScript === void 0 ? void 0 : currentScript.hasAttribute("debug");
42
+ const cachePolicy = currentScript === null || currentScript === void 0 ? void 0 : currentScript.getAttribute("cache");
43
+ const targets = ((_d = currentScript === null || currentScript === void 0 ? void 0 : currentScript.getAttribute("target")) === null || _d === void 0 ? void 0 : _d.split(",")) || ["body"];
19
44
  const renderings = targets.map((target) => __awaiter(void 0, void 0, void 0, function* () {
20
- const node = self.document[target];
21
- node.innerHTML = yield Mancha.renderContent(node.innerHTML, vars);
45
+ const fragment = self.document.querySelector(target);
46
+ yield Mancha.mount(fragment, { cache: cachePolicy, debug });
22
47
  }));
23
48
  Promise.all(renderings).then(() => {
24
- if (attributes["onrender"]) {
25
- eval(attributes["onrender"]);
26
- }
27
49
  dispatchEvent(new Event("mancha-render", { bubbles: true }));
28
50
  });
29
51
  }
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  const yargs_1 = require("yargs");
5
5
  const helpers_1 = require("yargs/helpers");
6
- const Mancha = require("./index.js");
6
+ const index_1 = require("./index");
7
7
  const fs = require("fs/promises");
8
8
  const args = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
9
9
  .describe("input", "Input HMTL file to render")
@@ -11,11 +11,11 @@ const args = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
11
11
  .describe("vars", "JSON-formatted variables")
12
12
  .demand(["input"])
13
13
  .parse();
14
- Mancha.renderLocalPath(args["input"], JSON.parse(args.vars || "{}")).then((result) => {
14
+ index_1.Mancha.renderLocalPath(args["input"], JSON.parse(args.vars || "{}")).then((result) => {
15
15
  if (!args.output || args.output === "-") {
16
16
  console.log(result + "\n");
17
17
  }
18
18
  else {
19
- return fs.writeFile(args.output, result);
19
+ return fs.writeFile(args.output, index_1.Mancha.serializeHTML(result));
20
20
  }
21
21
  });
package/dist/core.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ /// <reference types="node" />
2
+ import { ReactiveProxy, ReactiveProxyStore } from "./reactive";
3
+ export declare function traverse(root: Node | DocumentFragment | Document, skip?: Set<Node>): Generator<ChildNode>;
4
+ export declare function folderPath(fpath: string): string;
5
+ export declare function resolvePath(fpath: string): string;
6
+ export declare function extractTextNodeKeys(content: string): [string, string, string[]][];
7
+ export declare function safeEval(code: string, context: any, args?: {
8
+ [key: string]: any;
9
+ }): Promise<any>;
10
+ export interface ParserParams {
11
+ isRoot?: boolean;
12
+ encoding?: BufferEncoding;
13
+ }
14
+ export interface RendererParams {
15
+ fsroot?: string;
16
+ maxdepth?: number;
17
+ cache?: RequestCache | null;
18
+ debug?: boolean;
19
+ }
20
+ export declare abstract class IRenderer extends ReactiveProxyStore {
21
+ protected readonly fsroot: string;
22
+ protected readonly skipNodes: Set<Node>;
23
+ abstract parseHTML(content: string, params?: ParserParams): DocumentFragment;
24
+ abstract serializeHTML(root: DocumentFragment | Node): string;
25
+ abstract renderLocalPath(fpath: string, params?: RendererParams & ParserParams): Promise<DocumentFragment>;
26
+ clone(): IRenderer;
27
+ log(params?: RendererParams, ...args: any[]): void;
28
+ eval(expr: string, args?: {
29
+ [key: string]: any;
30
+ }, params?: RendererParams): Promise<any>;
31
+ resolveIncludes(root: Document | DocumentFragment | Node, params?: RendererParams): Promise<IRenderer>;
32
+ resolveTextNode(node: ChildNode, params?: RendererParams): ReactiveProxy<any>[];
33
+ resolveDataAttribute(node: ChildNode, params?: RendererParams): Promise<void>;
34
+ resolveWatchAttribute(node: ChildNode, params?: RendererParams): Promise<void>;
35
+ resolvePropAttributes(node: ChildNode, params?: RendererParams): Promise<void>;
36
+ resolveAttrAttributes(node: ChildNode, params?: RendererParams): Promise<void>;
37
+ resolveEventAttributes(node: ChildNode, params?: RendererParams): Promise<void>;
38
+ resolveForAttribute(node: ChildNode, params?: RendererParams): Promise<void>;
39
+ resolveBindAttribute(node: ChildNode, params?: RendererParams): Promise<void>;
40
+ resolveShowAttribute(node: ChildNode, params?: RendererParams): Promise<void>;
41
+ mount(root: Document | DocumentFragment | Node, params?: RendererParams): Promise<IRenderer>;
42
+ renderString(content: string, params?: RendererParams & ParserParams): Promise<DocumentFragment>;
43
+ renderRemotePath(fpath: string, params?: RendererParams & ParserParams): Promise<DocumentFragment>;
44
+ }