Skip to content

Commit 7e3663d

Browse files
committed
0.5.6: no escaping for text inside <script> tag
1 parent aa8e7b2 commit 7e3663d

12 files changed

+256
-11
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Sample code:
154154
_Requires Deno `1.35` or later._
155155
156156
```typescript
157-
import van from "https://deno.land/x/[email protected].5/src/van-plate.js"
157+
import van from "https://deno.land/x/[email protected].6/src/van-plate.js"
158158

159159
const {a, body, li, p, ul} = van.tags
160160

@@ -195,7 +195,7 @@ _Requires Deno `1.35` or later._
195195
196196
```typescript
197197
import { DOMParser } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts"
198-
import van from "https://deno.land/x/[email protected].5/src/mini-van.js"
198+
import van from "https://deno.land/x/[email protected].6/src/mini-van.js"
199199

200200
const document = new DOMParser().parseFromString("", "text/html")!
201201
const {tags, html} = van.vanWithDoc(document)
@@ -235,16 +235,16 @@ Preview via [CodeSandbox](https://codesandbox.io/p/sandbox/github/vanjs-org/vanj
235235
To get started on the client side, add the line below to your script:
236236
237237
```javascript
238-
import van from "https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.5.min.js"
238+
import van from "https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.6.min.js"
239239
```
240240
241241
To code without ES6 modules, add the following line to your HTML file instead:
242242
243243
```html
244-
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.5.nomodule.min.js"></script>
244+
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.6.nomodule.min.js"></script>
245245
```
246246
247-
Alternative, you can download the files ([`mini-van-0.5.5.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.5.min.js), [`mini-van-0.5.5.nomodule.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.5.nomodule.min.js)) and serve them locally.
247+
Alternative, you can download the files ([`mini-van-0.5.6.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.6.min.js), [`mini-van-0.5.6.nomodule.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.6.nomodule.min.js)) and serve them locally.
248248
249249
You can find all relevant **Mini-Van** files in this [Download Table](https://vanjs.org/minivan#download-table).
250250

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mini-van-plate",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"description": "A minimalist template engine for DOM generation and manipulation, working for both client-side and server-side rendering",
55
"files": [
66
"src/mini-van.js",

public/mini-van-0.5.6.d.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
export interface State<T> {
2+
val: T
3+
readonly oldVal: T
4+
}
5+
6+
// Defining readonly view of State<T> for covariance.
7+
// Basically we want StateView<string> to implement StateView<string | number>
8+
export type StateView<T> = Readonly<State<T>>
9+
10+
export type Primitive = string | number | boolean | bigint
11+
12+
export type PropValue = Primitive | ((e: any) => void) | null
13+
14+
export type Props = Record<string, PropValue | StateView<PropValue> | (() => PropValue)>
15+
16+
interface HasFirstChild {firstChild?: unknown}
17+
18+
type NodeType<ElementType extends HasFirstChild> =
19+
Omit<ElementType["firstChild"], "after" | "before" | "remove" | "replaceWith">
20+
21+
export type ValidChildDomValue<ElementType extends HasFirstChild, TextNodeType> =
22+
Primitive | ElementType | NodeType<ElementType> | TextNodeType | null | undefined
23+
24+
export type BindingFunc<ElementType extends HasFirstChild, TextNodeType> =
25+
| ((dom?: ElementType | TextNodeType) => ValidChildDomValue<ElementType, TextNodeType>)
26+
| ((dom?: ElementType) => ElementType)
27+
28+
export type ChildDom<ElementType extends HasFirstChild, TextNodeType> =
29+
| ValidChildDomValue<ElementType, TextNodeType>
30+
| StateView<Primitive | null | undefined>
31+
| BindingFunc<ElementType, TextNodeType>
32+
| readonly ChildDom<ElementType, TextNodeType>[]
33+
34+
type AddFunc<ElementType extends HasFirstChild, TextNodeType> =
35+
(dom: ElementType, ...children: readonly ChildDom<ElementType, TextNodeType>[]) => ElementType
36+
37+
export type TagFunc<ElementType extends HasFirstChild, TextNodeType, ResultType = ElementType> =
38+
(first?: Props | ChildDom<ElementType, TextNodeType>,
39+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => ResultType
40+
41+
type Tags<ElementType extends HasFirstChild, TextNodeType> =
42+
Readonly<Record<string, TagFunc<ElementType, TextNodeType>>>
43+
44+
// Tags type in browser context, which contains the signatures to tag functions that return
45+
// specialized DOM elements.
46+
type BrowserTags = Tags<Element, Text> & {
47+
[K in keyof HTMLElementTagNameMap]: TagFunc<Element, Text, HTMLElementTagNameMap[K]>
48+
}
49+
50+
declare function state<T>(): State<T>
51+
declare function state<T>(initVal: T): State<T>
52+
53+
export interface VanObj<ElementType extends HasFirstChild, TextNodeType> {
54+
readonly state: typeof state
55+
readonly derive: <T>(f: () => T) => State<T>
56+
readonly add: AddFunc<ElementType, TextNodeType>
57+
readonly tags: Tags<ElementType, TextNodeType> & ((namespaceURI: string) => Tags<ElementType, TextNodeType>)
58+
59+
// Mini-Van specific API
60+
html: (first?: Props | ChildDom<ElementType, TextNodeType>,
61+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => string
62+
}
63+
64+
export interface Van extends VanObj<Element, Text> {
65+
readonly vanWithDoc: <ElementType extends HasFirstChild, TextNodeType>(doc: {
66+
createElement(s: any): ElementType,
67+
createTextNode(s: any): TextNodeType,
68+
}) => VanObj<ElementType, TextNodeType>
69+
readonly tags: BrowserTags & ((namespaceURI: string) => Tags<Element, Text>)
70+
}
71+
72+
declare const van: Van
73+
74+
export default van

public/mini-van-0.5.6.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// <reference types="./mini-van.d.ts" />
2+
3+
// This file consistently uses `let` keyword instead of `const` for reducing the bundle size.
4+
5+
// Aliasing some builtin symbols to reduce the bundle size.
6+
let protoOf = Object.getPrototypeOf, _undefined, funcProto = protoOf(protoOf)
7+
8+
let stateProto = {get oldVal() { return this.val }}, objProto = protoOf(stateProto)
9+
10+
let state = initVal => ({__proto__: stateProto, val: initVal})
11+
12+
let plainValue = (k, v) => {
13+
let protoOfV = protoOf(v ?? 0)
14+
return protoOfV === stateProto ? v.val :
15+
protoOfV !== funcProto || k?.startsWith("on") ? v : v()
16+
}
17+
18+
let add = (dom, ...children) =>
19+
(dom.append(...children.flat(Infinity)
20+
.map(plainValue.bind(_undefined, _undefined))
21+
.filter(c => c != _undefined)),
22+
dom)
23+
24+
let vanWithDoc = doc => {
25+
let tag = (ns, name, ...args) => {
26+
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args]
27+
let dom = ns ? doc.createElementNS(ns, name) : doc.createElement(name)
28+
for (let [k, v] of Object.entries(props)) {
29+
let plainV = plainValue(k, v)
30+
// Disable setting attribute for function-valued properties (mostly event handlers),
31+
// as they're usually not useful for SSR (server-side rendering).
32+
protoOf(plainV) !== funcProto && dom.setAttribute(k, plainV)
33+
}
34+
return add(dom, ...children)
35+
}
36+
37+
let handler = ns => ({get: (_, name) => tag.bind(_undefined, ns, name)})
38+
let tags = new Proxy(ns => new Proxy(tag, handler(ns)), handler())
39+
40+
return {
41+
add, tags, state, derive: f => state(f()),
42+
html: (...args) => "<!DOCTYPE html>" + tags.html(...args).outerHTML,
43+
}
44+
}
45+
46+
export default {"vanWithDoc": vanWithDoc,
47+
...vanWithDoc(typeof window !== "undefined" ? window.document : null)}

public/mini-van-0.5.6.min.d.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
export interface State<T> {
2+
val: T
3+
readonly oldVal: T
4+
}
5+
6+
// Defining readonly view of State<T> for covariance.
7+
// Basically we want StateView<string> to implement StateView<string | number>
8+
export type StateView<T> = Readonly<State<T>>
9+
10+
export type Primitive = string | number | boolean | bigint
11+
12+
export type PropValue = Primitive | ((e: any) => void) | null
13+
14+
export type Props = Record<string, PropValue | StateView<PropValue> | (() => PropValue)>
15+
16+
interface HasFirstChild {firstChild?: unknown}
17+
18+
type NodeType<ElementType extends HasFirstChild> =
19+
Omit<ElementType["firstChild"], "after" | "before" | "remove" | "replaceWith">
20+
21+
export type ValidChildDomValue<ElementType extends HasFirstChild, TextNodeType> =
22+
Primitive | ElementType | NodeType<ElementType> | TextNodeType | null | undefined
23+
24+
export type BindingFunc<ElementType extends HasFirstChild, TextNodeType> =
25+
| ((dom?: ElementType | TextNodeType) => ValidChildDomValue<ElementType, TextNodeType>)
26+
| ((dom?: ElementType) => ElementType)
27+
28+
export type ChildDom<ElementType extends HasFirstChild, TextNodeType> =
29+
| ValidChildDomValue<ElementType, TextNodeType>
30+
| StateView<Primitive | null | undefined>
31+
| BindingFunc<ElementType, TextNodeType>
32+
| readonly ChildDom<ElementType, TextNodeType>[]
33+
34+
type AddFunc<ElementType extends HasFirstChild, TextNodeType> =
35+
(dom: ElementType, ...children: readonly ChildDom<ElementType, TextNodeType>[]) => ElementType
36+
37+
export type TagFunc<ElementType extends HasFirstChild, TextNodeType, ResultType = ElementType> =
38+
(first?: Props | ChildDom<ElementType, TextNodeType>,
39+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => ResultType
40+
41+
type Tags<ElementType extends HasFirstChild, TextNodeType> =
42+
Readonly<Record<string, TagFunc<ElementType, TextNodeType>>>
43+
44+
// Tags type in browser context, which contains the signatures to tag functions that return
45+
// specialized DOM elements.
46+
type BrowserTags = Tags<Element, Text> & {
47+
[K in keyof HTMLElementTagNameMap]: TagFunc<Element, Text, HTMLElementTagNameMap[K]>
48+
}
49+
50+
declare function state<T>(): State<T>
51+
declare function state<T>(initVal: T): State<T>
52+
53+
export interface VanObj<ElementType extends HasFirstChild, TextNodeType> {
54+
readonly state: typeof state
55+
readonly derive: <T>(f: () => T) => State<T>
56+
readonly add: AddFunc<ElementType, TextNodeType>
57+
readonly tags: Tags<ElementType, TextNodeType> & ((namespaceURI: string) => Tags<ElementType, TextNodeType>)
58+
59+
// Mini-Van specific API
60+
html: (first?: Props | ChildDom<ElementType, TextNodeType>,
61+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => string
62+
}
63+
64+
export interface Van extends VanObj<Element, Text> {
65+
readonly vanWithDoc: <ElementType extends HasFirstChild, TextNodeType>(doc: {
66+
createElement(s: any): ElementType,
67+
createTextNode(s: any): TextNodeType,
68+
}) => VanObj<ElementType, TextNodeType>
69+
readonly tags: BrowserTags & ((namespaceURI: string) => Tags<Element, Text>)
70+
}
71+
72+
declare const van: Van
73+
74+
export default van

public/mini-van-0.5.6.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mini-van-0.5.6.nomodule.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(() => {
2+
// mini-van.js
3+
var protoOf = Object.getPrototypeOf;
4+
var _undefined;
5+
var funcProto = protoOf(protoOf);
6+
var stateProto = { get oldVal() {
7+
return this.val;
8+
} };
9+
var objProto = protoOf(stateProto);
10+
var state = (initVal) => ({ __proto__: stateProto, val: initVal });
11+
var plainValue = (k, v) => {
12+
let protoOfV = protoOf(v ?? 0);
13+
return protoOfV === stateProto ? v.val : protoOfV !== funcProto || k?.startsWith("on") ? v : v();
14+
};
15+
var add = (dom, ...children) => (dom.append(...children.flat(Infinity).map(plainValue.bind(_undefined, _undefined)).filter((c) => c != _undefined)), dom);
16+
var vanWithDoc = (doc) => {
17+
let tag = (ns, name, ...args) => {
18+
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args];
19+
let dom = ns ? doc.createElementNS(ns, name) : doc.createElement(name);
20+
for (let [k, v] of Object.entries(props)) {
21+
let plainV = plainValue(k, v);
22+
protoOf(plainV) !== funcProto && dom.setAttribute(k, plainV);
23+
}
24+
return add(dom, ...children);
25+
};
26+
let handler = (ns) => ({ get: (_, name) => tag.bind(_undefined, ns, name) });
27+
let tags = new Proxy((ns) => new Proxy(tag, handler(ns)), handler());
28+
return {
29+
add,
30+
tags,
31+
state,
32+
derive: (f) => state(f()),
33+
html: (...args) => "<!DOCTYPE html>" + tags.html(...args).outerHTML
34+
};
35+
};
36+
var mini_van_default = {
37+
"vanWithDoc": vanWithDoc,
38+
...vanWithDoc(typeof window !== "undefined" ? window.document : null)
39+
};
40+
41+
// mini-van.forbundle.js
42+
window.van = mini_van_default;
43+
})();

public/mini-van-0.5.6.nomodule.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mini-van.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.5
1+
0.5.6

0 commit comments

Comments
 (0)