mancha 0.6.4 → 0.6.6

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/README.md CHANGED
@@ -1,80 +1,182 @@
1
1
  # mancha
2
2
 
3
- `mancha` is an HTML rendering library. It can work as a command-line tool, imported as a Javascript
4
- function, or as a `Gulp` plugin.
3
+ `mancha` is a simple HTML templating and rendering library for simple people. It works on the
4
+ browser or the server. It can be used as a command-line tool, imported as a Javascript module, or as
5
+ a plugin for [`Gulp`](https://gulpjs.com).
5
6
 
6
- ## Examples
7
-
8
- Here are some of the things you can use `mancha` for.
9
-
10
- ### Replace simple variables using `{{ value }}` format
11
-
12
- index.html:
13
-
14
- ```html
15
- <span>Hello {{ name }}</span>
16
- ```
17
-
18
- Command:
19
-
20
- ```bash
21
- npx mancha --input="./index.html" --vars='{"name": "World"}'
22
- ```
23
-
24
- Result:
25
-
26
- ```html
27
- <div>Hello World</div>
28
- ```
29
-
30
- ### Include files from a relative path using the `<include>` tag
31
-
32
- hello-name.html:
33
-
34
- ```html
35
- <span>Hello {{ name }}</span>
36
- ```
37
-
38
- index.html:
7
+ Here's a small sample of the things that you can do with `mancha`:
39
8
 
40
9
  ```html
41
- <div>
42
- <include src="./hello-name.html" data-name="World"></include>
43
- </div>
10
+ <!-- Use the bundled file from `unkpg`. -->
11
+ <script src="//unpkg.com/mancha" target="main" init></script>
12
+
13
+ <!-- Scoped variables using the `:data` attribute. -->
14
+ <main :data="{count: 0, name: 'Stranger'}">
15
+ <!-- Custom HTML tag element registration. -->
16
+ <template is="counter">
17
+ <div>
18
+ <slot></slot>
19
+ <button @click="count++">Counter: {{ count }}</button>
20
+ </div>
21
+ </template>
22
+
23
+ <!-- Custom HTML tag element usage. -->
24
+ <counter>Click me:</counter>
25
+
26
+ <!-- Reactive data binding. -->
27
+ <p>Enter your name: <input type="text" :bind="name" /></p>
28
+ <p>Hello, {{ name }}!</p>
29
+
30
+ <!-- Include HTML partials. -->
31
+ <include src="html/partial/footer.tpl.html"></include>
32
+ </main>
44
33
  ```
45
34
 
46
- Command:
47
-
48
- ```bash
49
- npx mancha --input="./index.html"
50
- ```
51
-
52
- Result:
53
-
54
- ```html
55
- <div>
56
- <span>Hello World</span>
57
- </div>
58
- ```
35
+ ## Why another front-end Javascript library?
36
+
37
+ There are plenty of other front-end Javascript libraries, many of them of great quality, including:
38
+
39
+ - [Google's Svelte](https://svelte.dev)
40
+ - [Meta's React](https://react.dev)
41
+ - [Vue.js](https://vuejs.org) and [petite-vue](https://github.com/vuejs/petite-vue)
42
+ - [Alpine.js](https://alpinejs.dev)
43
+
44
+ None of them have all the key features that make `mancha` unique:
45
+
46
+ | Feature | mancha | Svelte | React.js | Vue.js | petite-vue | Alpine.js |
47
+ | --------------------- | ------ | ------ | -------- | ------ | ---------- | --------- |
48
+ | Simple to learn | ✔️ | ❌ | ❌ | ❌ | ✔️ | ✔️ |
49
+ | < 10kb compressed | ✔️ | ❌ | ❌ | ❌ | ✔️ | ❌ |
50
+ | Custom web components | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | ❌ |
51
+ | Client-side rendering | ✔️ | ❌ | ❌ | ✔️ | ✔️ | ✔️ |
52
+ | Server-side rendering | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | ❌ |
53
+
54
+ `mancha` is great for:
55
+
56
+ - **prototyping**, just plop a script tag in your HTML and off you go
57
+ - **testing**, individual components can be rendered and tested outside the browser
58
+ - **progressive enhancement**, from simple templating and basic reactivity to a full-blown app
59
+
60
+ A core benefit of using `mancha` is that it allows you to compartmentalize the complexity of
61
+ front-end development. Whether you decide to break up your app into reusable partial sections via
62
+ `<include>` or create custom web components, you can write HTML as if your mother was watching.
63
+
64
+ `mancha` implements its own reactivity engine, so the bundled browser module contains no external
65
+ dependencies.
66
+
67
+ ## Preprocessing
68
+
69
+ As part of the rendering lifecycle, `mancha` first preprocesses the HTML. The two main stages of
70
+ preprocessing consist of:
71
+
72
+ - Resolution of `<include>` tags
73
+
74
+ ```html
75
+ <!-- ./button.tpl.html -->
76
+ <button>Click Me</button>
77
+
78
+ <!-- ./index.html -->
79
+ <div>
80
+ <include src="button.tpl.html"></include>
81
+ </div>
82
+
83
+ <!-- Result after rendering `index.html`. -->
84
+ <div>
85
+ <button>Click Me</button>
86
+ </div>
87
+ ```
88
+
89
+ - Registration and resolution of all custom web components
90
+
91
+ ```html
92
+ <!-- Use <template is="my-component-name"> to register a component. -->
93
+ <template is="my-red-button">
94
+ <button style="background-color: red;">
95
+ <slot></slot>
96
+ </button>
97
+ </template>
98
+
99
+ <!-- Any node traversed after registration can use the component. -->
100
+ <my-red-button @click="console.log('clicked')">
101
+ <!-- The contents within will replace the `<slot></slot>` tag. -->
102
+ Click Me
103
+ </my-red-button>
104
+ ```
105
+
106
+ ## Rendering
107
+
108
+ Once the HTML has been preprocessed, it is rendered by traversing every node in the DOM and applying
109
+ a series of plugins. Each plugin is only applied if specific conditions are met such as the HTML
110
+ element tag or attributes match a specific criteria. Here's the list of attributes handled:
111
+
112
+ - `:data` provides scoped variables to all subnodes
113
+ ```html
114
+ <div :data="{name: 'Stranger'}"></div>
115
+ ```
116
+ - `:for` clones the node and repeats it
117
+ ```html
118
+ <div :for="item in ['a', 'b', 'c']">{{ item }}</div>
119
+ ```
120
+ - `$text` sets the `textContent` value of a node
121
+ ```html
122
+ <div :data="{foo: 'bar'}" $text="foo"></div>
123
+ ```
124
+ - `$html` sets the `innerHTML` value of a node
125
+ ```html
126
+ <div $html="<span>Hello World</span>"></div>
127
+ ```
128
+ - `:show` toggles `$elem.style.display` to `none`
129
+ ```html
130
+ <div :data="{foo: false}" :show="foo"></div>
131
+ ```
132
+ - `@watch` executes an expression anytime its dependencies change
133
+ ```html
134
+ <div :data="{foo: 'bar'}" @watch="console.log('foo changed:', foo)"></div>
135
+ ```
136
+ - `:bind` binds (two-way) a variable to the `value` or `checked` property of the element.
137
+ ```html
138
+ <div :data="{name: 'Stranger'}">
139
+ <input type="text" :bind="name" />
140
+ </div>
141
+ ```
142
+ - `${prop}` binds (one-way) the node property `prop` with the given expression
143
+ ```html
144
+ <div :data="{foo: false}">
145
+ <input type="submit" $disabled="foo" />
146
+ </div>
147
+ ```
148
+ - `:{attr}` binds (one-way) the node attribute `attr` with the given expression
149
+ ```html
150
+ <div :data="{foo: 'bar'}">
151
+ <input type="text" :placeholder="foo" />
152
+ </div>
153
+ ```
154
+ - `@{event}` adds an event listener for `event` to the node
155
+ ```html
156
+ <button @click="console.log('clicked')"></button>
157
+ ```
158
+ - `{{ value }}` replaces `value` in text nodes
159
+ ```html
160
+ <button :data="{label: 'Click Me'}">{{ label }}</button>
161
+ ```
59
162
 
60
163
  ## Usage
61
164
 
62
165
  ### Client Side Rendering (CSR)
63
166
 
64
- To use `mancha` on the client (browser), use the `mancha.js` bundled file available via `unpkg`.
167
+ To use `mancha` on the client (browser), use the `mancha` bundled file available via `unpkg`.
65
168
 
66
169
  ```html
67
- <body>
170
+ <body :data="{ name: 'John' }">
68
171
  <span>Hello, {{ name }}!</span>
69
172
  </body>
70
173
 
71
- <script src="//unpkg.com/mancha" data-name="John" target="body" init></script>
174
+ <script src="//unpkg.com/mancha" target="body" defer init></script>
72
175
  ```
73
176
 
74
177
  Script tag attributes:
75
178
 
76
179
  - `init`: whether to automatically render upon script load
77
- - `data-name`: dataset atribute, where `data-{{key}}` will be replaced with the attribute's value
78
180
  - `target`: comma-separated document elements to render e.g. "body" or "head,body" (defaults to "body")
79
181
 
80
182
  For a more complete example, see [examples/browser](./examples/browser).
@@ -109,13 +211,20 @@ the HTML code on demand for each incoming request:
109
211
 
110
212
  ```js
111
213
  import express from "express";
112
- import { renderLocalPath } from "mancha";
214
+ import { Renderer } from "mancha";
113
215
  import vars from "./vars.json";
114
216
 
115
217
  const app = express();
116
218
 
117
219
  app.get("/", async (req, res) => {
118
- const html = await renderLocalPath("src/index.html", vars);
220
+ const name = req.query.name || "Stranger";
221
+ // Instantiate a new renderer.
222
+ const renderer = new Renderer({ name, ...vars });
223
+ // Preprocess input HTML from a local file path.
224
+ const fragment = await renderer.preprocessLocal("src/index.html");
225
+ // Render and serialize output HTML.
226
+ const html = renderer.serializeHTML(await renderer.renderNode(fragment));
227
+ // Send it to the client.
119
228
  res.set("Content-Type", "text/html");
120
229
  res.send(html);
121
230
  });
@@ -123,50 +232,56 @@ app.get("/", async (req, res) => {
123
232
  app.listen(process.env.PORT || 8080);
124
233
  ```
125
234
 
126
- For a more complete example, see [examples/rendered](./examples/rendered).
235
+ For a more complete example, see [examples/express](./examples/express).
127
236
 
128
237
  ### Web Worker Runtime Server Side Rendering (SSR)
129
238
 
130
239
  For servers hosted as worker runtimes, such as `Cloudflare Workers`, you will need to import a
131
- stripped down version of `mancha` that does not have the ability to read local files. Any HTML files
132
- will need to be separately hosted by a static server, although you can also generate strings
133
- containing HTML on demand.
240
+ stripped down version of `mancha` that does not have the ability to read local files or evaluate
241
+ expressions. The contents of any `{{ value }}` must be an existing variable in the renderer
242
+ instance, since string evaluation is not permitted in some runtimes.
134
243
 
135
244
  ```js
136
- import { renderRemotePath } from "mancha/dist/core"
137
-
138
- const VARS = {...};
139
- const HTML_ROOT = "https://example.com/html";
245
+ import { Renderer } from "mancha/dist/worker";
246
+ import htmlIndex from "./index.html";
247
+ import vars from "./vars.json";
140
248
 
141
- self.addEventListener('fetch', async event => {
142
- const content = await renderRemotePath(`${HTML_ROOT}/index.html`, VARS);
143
- event.respondWith(new Response(content, { headers: {"Content-Type": "text/html"} }))
249
+ self.addEventListener("fetch", async (event) => {
250
+ // Instantiate a new renderer.
251
+ const renderer = new Renderer({ ...vars });
252
+ // Preprocess input HTML from a string.
253
+ const fragment = await renderer.preprocessString(htmlIndex);
254
+ // Render and serialize output HTML.
255
+ const html = renderer.serializeHTML(await renderer.renderNode(fragment));
256
+ // Send it to the client.
257
+ event.respondWith(new Response(content, { headers: { "Content-Type": "text/html" } }));
144
258
  });
145
259
  ```
146
260
 
261
+ To meet the size requirements of popular worker runtimes, the worker version of `mancha` uses
262
+ `htmlparser2` instead of `jsdom` for the underlying HTML and DOM manipulation. This keeps the
263
+ footprint of `mancha` under 100kb.
264
+
147
265
  For a more complete example, see [examples/wrangler](./examples/wrangler).
148
266
 
149
- ## Compile Time `gulpfile` Scripts
267
+ ## Compile Time `Gulp` Plugin
150
268
 
151
- To use `mancha` in your `gulpfile`, you can do the following:
269
+ To use `mancha` as a `Gulp` plugin in `gulpfile.js`, you can do the following:
152
270
 
153
271
  ```js
272
+ import GulpClient from "gulp";
154
273
  import { mancha } from "mancha/dist/gulp";
155
- gulp.src(...).pipe(mancha({"myvar": "myval"})).pipe(...)
156
- ```
157
-
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,
160
- if we passed `{"name": "World"}` as the argument:
161
-
162
- Source:
163
-
164
- ```html
165
- <div>Hello {{ name }}</div>
166
- ```
167
-
168
- Result:
274
+ import vars from "./vars.json";
169
275
 
170
- ```html
171
- <div>Hello World</div>
276
+ GulpClient.task("build", function () {
277
+ return (
278
+ GulpClient
279
+ // Inlcude all HTML files, but exclude all partials (ending in .tpl.html).
280
+ .src(["src/**/*.html", "!src/**/*.tpl/html"])
281
+ // Render the HTML content using `mancha`.
282
+ .pipe(mancha(vars))
283
+ // Pipe the output to the destination folder.
284
+ .pipe(GulpClient.dest("public"))
285
+ );
286
+ });
172
287
  ```
package/dist/browser.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { dirname, IRenderer } from "./core.js";
2
2
  class Renderer extends IRenderer {
3
3
  dirpath = dirname(self.location.href);
4
- parseHTML(content, params = { root: false }) {
5
- if (params.root) {
4
+ parseHTML(content, params = { rootDocument: false }) {
5
+ if (params.rootDocument) {
6
6
  return new DOMParser().parseFromString(content, "text/html");
7
7
  }
8
8
  else {
@@ -15,7 +15,7 @@ class Renderer extends IRenderer {
15
15
  return new XMLSerializer().serializeToString(root).replace(/\s?xmlns="[^"]+"/gm, "");
16
16
  }
17
17
  preprocessLocal(fpath, params) {
18
- // In the browser, "local" paths (i.e., relative) can still be fetched.
18
+ // In the browser, "local" paths (i.e., relative paths) can still be fetched.
19
19
  return this.preprocessRemote(fpath, params);
20
20
  }
21
21
  }
package/dist/cli.js CHANGED
File without changes
package/dist/core.d.ts CHANGED
@@ -1,10 +1,30 @@
1
1
  import { ReactiveProxyStore } from "./reactive.js";
2
2
  import { ParserParams, RenderParams } from "./interfaces.js";
3
3
  export type EvalListener = (result: any, dependencies: string[]) => any;
4
- export declare function traverse(root: Node | DocumentFragment | Document, skip?: Set<Node>): Generator<ChildNode>;
4
+ /**
5
+ * Returns the directory name from a given file path.
6
+ * @param fpath - The file path.
7
+ * @returns The directory name.
8
+ */
5
9
  export declare function dirname(fpath: string): string;
10
+ /**
11
+ * Checks if a given file path is a relative path.
12
+ *
13
+ * @param fpath - The file path to check.
14
+ * @returns A boolean indicating whether the file path is relative or not.
15
+ */
6
16
  export declare function isRelativePath(fpath: string): boolean;
17
+ /**
18
+ * Creates an evaluation function based on the provided code and arguments.
19
+ * @param code The code to be evaluated.
20
+ * @param args The arguments to be passed to the evaluation function. Default is an empty array.
21
+ * @returns The evaluation function.
22
+ */
7
23
  export declare function makeEvalFunction(code: string, args?: string[]): Function;
24
+ /**
25
+ * Represents an abstract class for rendering and manipulating HTML content.
26
+ * Extends the `ReactiveProxyStore` class.
27
+ */
8
28
  export declare abstract class IRenderer extends ReactiveProxyStore {
9
29
  protected debugging: boolean;
10
30
  protected readonly dirpath: string;
@@ -15,22 +35,115 @@ export declare abstract class IRenderer extends ReactiveProxyStore {
15
35
  readonly _customElements: Map<string, Node>;
16
36
  abstract parseHTML(content: string, params?: ParserParams): DocumentFragment;
17
37
  abstract serializeHTML(root: DocumentFragment | Node): string;
38
+ /**
39
+ * Sets the debugging flag for the current instance.
40
+ *
41
+ * @param flag - The flag indicating whether debugging is enabled or disabled.
42
+ * @returns The current instance of the class.
43
+ */
18
44
  debug(flag: boolean): this;
45
+ /**
46
+ * Fetches the remote file at the specified path and returns its content as a string.
47
+ * @param fpath - The path of the remote file to fetch.
48
+ * @param params - Optional parameters for the fetch operation.
49
+ * @returns A promise that resolves to the content of the remote file as a string.
50
+ */
19
51
  fetchRemote(fpath: string, params?: RenderParams): Promise<string>;
52
+ /**
53
+ * Fetches a local path and returns its content as a string.
54
+ *
55
+ * @param fpath - The file path of the resource.
56
+ * @param params - Optional render parameters.
57
+ * @returns A promise that resolves to the fetched resource as a string.
58
+ */
20
59
  fetchLocal(fpath: string, params?: RenderParams): Promise<string>;
60
+ /**
61
+ * Preprocesses a string content with optional rendering and parsing parameters.
62
+ *
63
+ * @param content - The string content to preprocess.
64
+ * @param params - Optional rendering and parsing parameters.
65
+ * @returns A promise that resolves to a DocumentFragment representing the preprocessed content.
66
+ */
21
67
  preprocessString(content: string, params?: RenderParams & ParserParams): Promise<DocumentFragment>;
22
- preprocessLocal(fpath: string, params?: RenderParams & ParserParams): Promise<DocumentFragment>;
68
+ /**
69
+ * Preprocesses a remote file by fetching its content and applying preprocessing steps.
70
+ * @param fpath - The path to the remote file.
71
+ * @param params - Optional parameters for rendering and parsing.
72
+ * @returns A Promise that resolves to a DocumentFragment representing the preprocessed content.
73
+ */
23
74
  preprocessRemote(fpath: string, params?: RenderParams & ParserParams): Promise<DocumentFragment>;
75
+ /**
76
+ * Preprocesses a local file by fetching its content and applying preprocessing steps.
77
+ * @param fpath - The path to the local file.
78
+ * @param params - Optional parameters for rendering and parsing.
79
+ * @returns A promise that resolves to the preprocessed document fragment.
80
+ */
81
+ preprocessLocal(fpath: string, params?: RenderParams & ParserParams): Promise<DocumentFragment>;
82
+ /**
83
+ * Creates a deep copy of the current renderer instance.
84
+ * @returns A new instance of the renderer with the same state as the original.
85
+ */
24
86
  clone(): IRenderer;
87
+ /**
88
+ * Logs the provided arguments if debugging is enabled.
89
+ * @param args - The arguments to be logged.
90
+ */
25
91
  log(...args: any[]): void;
92
+ /**
93
+ * Retrieves or creates a cached expression function based on the provided expression.
94
+ * @param expr - The expression to retrieve or create a cached function for.
95
+ * @returns The cached expression function.
96
+ */
26
97
  private cachedExpressionFunction;
98
+ /**
99
+ * Evaluates an expression and returns the result along with its dependencies.
100
+ * If the expression is already stored, it returns the stored value directly.
101
+ * Otherwise, it performs the expression evaluation using the cached expression function.
102
+ * @param expr - The expression to evaluate.
103
+ * @param args - Optional arguments to be passed to the expression function.
104
+ * @returns A promise that resolves to the result and the dependencies of the expression.
105
+ */
27
106
  eval(expr: string, args?: {
28
107
  [key: string]: any;
29
108
  }): Promise<[any, string[]]>;
109
+ /**
110
+ * This function is intended for internal use only.
111
+ *
112
+ * Executes the given expression and invokes the provided callback whenever the any of the
113
+ * dependencies change.
114
+ *
115
+ * @param expr - The expression to watch for changes.
116
+ * @param args - The arguments to be passed to the expression during evaluation.
117
+ * @param callback - The callback function to be invoked when the dependencies change.
118
+ * @returns A promise that resolves when the initial evaluation is complete.
119
+ */
30
120
  watchExpr(expr: string, args: {
31
121
  [key: string]: any;
32
122
  }, callback: EvalListener): Promise<void>;
123
+ /**
124
+ * Preprocesses a node by applying all the registered preprocessing plugins.
125
+ *
126
+ * @template T - The type of the input node.
127
+ * @param {T} root - The root node to preprocess.
128
+ * @param {RenderParams} [params] - Optional parameters for preprocessing.
129
+ * @returns {Promise<T>} - A promise that resolves to the preprocessed node.
130
+ */
33
131
  preprocessNode<T extends Document | DocumentFragment | Node>(root: T, params?: RenderParams): Promise<T>;
132
+ /**
133
+ * Renders the node by applies all the registered rendering plugins.
134
+ *
135
+ * @template T - The type of the root node (Document, DocumentFragment, or Node).
136
+ * @param {T} root - The root node to render.
137
+ * @param {RenderParams} [params] - Optional parameters for rendering.
138
+ * @returns {Promise<T>} - A promise that resolves to the fully rendered root node.
139
+ */
34
140
  renderNode<T extends Document | DocumentFragment | Node>(root: T, params?: RenderParams): Promise<T>;
141
+ /**
142
+ * Mounts the Mancha application to a root element in the DOM.
143
+ *
144
+ * @param root - The root element to mount the application to.
145
+ * @param params - Optional parameters for rendering the application.
146
+ * @returns A promise that resolves when the mounting process is complete.
147
+ */
35
148
  mount(root: Document | DocumentFragment | Node, params?: RenderParams): Promise<void>;
36
149
  }