@link-foundation/ui-dom 0.0.0-stage → 0.0.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.
- package/LICENSE +24 -0
- package/README.md +295 -2
- package/package.json +54 -4
- package/src/diagnostics.js +58 -0
- package/src/events.js +174 -0
- package/src/frame-fit.js +205 -0
- package/src/html.d.ts +4 -0
- package/src/html.js +245 -0
- package/src/index.d.ts +267 -0
- package/src/index.js +47 -0
- package/src/kinds/basic.js +189 -0
- package/src/kinds.js +151 -0
- package/src/model.js +84 -0
- package/src/patch.js +62 -0
- package/src/render.js +121 -0
- package/src/scheduler.js +50 -0
- package/src/styles.js +190 -0
- package/src/themes.js +147 -0
- package/src/tree.js +335 -0
package/src/frame-fit.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
function classifyBlocks(current, root, levels, boundaryAttribute) {
|
|
2
|
+
if (levels.panel && levels.block) {
|
|
3
|
+
const panel = levels.panel.join(', ');
|
|
4
|
+
for (const [element, level] of current) {
|
|
5
|
+
if (level !== 'panel') {
|
|
6
|
+
continue;
|
|
7
|
+
}
|
|
8
|
+
let ancestor = element;
|
|
9
|
+
while (
|
|
10
|
+
ancestor &&
|
|
11
|
+
ancestor !== root &&
|
|
12
|
+
!(boundaryAttribute && ancestor.hasAttribute(boundaryAttribute))
|
|
13
|
+
) {
|
|
14
|
+
ancestor = ancestor.parentElement;
|
|
15
|
+
if (ancestor?.matches(panel)) {
|
|
16
|
+
current.set(element, 'block');
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function matchingHosts(root, levels, limits, boundaryAttribute) {
|
|
25
|
+
const current = new Map();
|
|
26
|
+
for (const [level, selectors] of Object.entries(levels)) {
|
|
27
|
+
const selector = selectors.join(', ');
|
|
28
|
+
if (root.matches?.(selector)) {
|
|
29
|
+
current.set(root, level);
|
|
30
|
+
}
|
|
31
|
+
for (const element of root.querySelectorAll(selector)) {
|
|
32
|
+
current.set(element, level);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
classifyBlocks(current, root, levels, boundaryAttribute);
|
|
36
|
+
for (const [element, level] of current) {
|
|
37
|
+
if (!limits.get(level).some((value) => value > 0)) {
|
|
38
|
+
current.delete(element);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return current;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function resizeRect(entry, view) {
|
|
45
|
+
const box = entry.borderBoxSize?.[0];
|
|
46
|
+
if (!box) {
|
|
47
|
+
return entry.target.getBoundingClientRect();
|
|
48
|
+
}
|
|
49
|
+
const vertical = view
|
|
50
|
+
.getComputedStyle(entry.target)
|
|
51
|
+
.writingMode.startsWith('vertical');
|
|
52
|
+
return {
|
|
53
|
+
width: vertical ? box.blockSize : box.inlineSize,
|
|
54
|
+
height: vertical ? box.inlineSize : box.blockSize,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Opt-in threshold fitting. CSS handles continuous geometry; this helper only
|
|
59
|
+
* marks hosts outside configured limits. It never computes paths or pixels.
|
|
60
|
+
*
|
|
61
|
+
* @param {Element|Document} root
|
|
62
|
+
* @param {{levels: Record<string, string[]>, scope?: Element, prefix?: string, boundaryAttribute?: string,
|
|
63
|
+
* ResizeObserver?: typeof ResizeObserver, MutationObserver?: typeof MutationObserver}} options
|
|
64
|
+
*/
|
|
65
|
+
export function observeFrameFit(root, options) {
|
|
66
|
+
const { levels, prefix = 'frame' } = options;
|
|
67
|
+
const scope = options.scope ?? root.documentElement ?? root;
|
|
68
|
+
const view = scope.ownerDocument.defaultView;
|
|
69
|
+
const Resize = options.ResizeObserver ?? view.ResizeObserver;
|
|
70
|
+
const Mutation = options.MutationObserver ?? view.MutationObserver;
|
|
71
|
+
const attribute = `data-${prefix}-fit`;
|
|
72
|
+
const tracked = new Map();
|
|
73
|
+
let observer = null;
|
|
74
|
+
let disposed = false;
|
|
75
|
+
let limits = new Map();
|
|
76
|
+
const outside = (rect, limit) =>
|
|
77
|
+
(limit[0] > 0 && rect.width < limit[0]) ||
|
|
78
|
+
(limit[1] > 0 && rect.width > limit[1]) ||
|
|
79
|
+
(limit[2] > 0 && rect.height < limit[2]) ||
|
|
80
|
+
(limit[3] > 0 && rect.height > limit[3]);
|
|
81
|
+
const mark = (element, rect) => {
|
|
82
|
+
const item = tracked.get(element);
|
|
83
|
+
if (!item) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const value = outside(rect, limits.get(item.level)) ? 'outside' : null;
|
|
87
|
+
if (element.getAttribute(attribute) === value) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (value === null) {
|
|
91
|
+
element.removeAttribute(attribute);
|
|
92
|
+
} else {
|
|
93
|
+
element.setAttribute(attribute, value);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const restore = (element, item) => {
|
|
97
|
+
if (element.getAttribute(attribute) === item.original) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (item.original === null) {
|
|
101
|
+
element.removeAttribute(attribute);
|
|
102
|
+
} else {
|
|
103
|
+
element.setAttribute(attribute, item.original);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const refresh = () => {
|
|
107
|
+
if (disposed) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const style = view.getComputedStyle(scope);
|
|
111
|
+
limits = new Map(
|
|
112
|
+
Object.keys(levels).map((level) => [
|
|
113
|
+
level,
|
|
114
|
+
['min-width', 'max-width', 'min-height', 'max-height'].map((key) => {
|
|
115
|
+
const value = Number(
|
|
116
|
+
style.getPropertyValue(`--${prefix}-${level}-${key}`).trim()
|
|
117
|
+
);
|
|
118
|
+
return Number.isFinite(value) && value >= 0 && value <= 1600
|
|
119
|
+
? value
|
|
120
|
+
: 0;
|
|
121
|
+
}),
|
|
122
|
+
])
|
|
123
|
+
);
|
|
124
|
+
const active = [...limits.values()].some((values) =>
|
|
125
|
+
values.some((value) => value > 0)
|
|
126
|
+
);
|
|
127
|
+
if (!active) {
|
|
128
|
+
observer?.disconnect();
|
|
129
|
+
observer = null;
|
|
130
|
+
for (const [element, item] of tracked) {
|
|
131
|
+
restore(element, item);
|
|
132
|
+
}
|
|
133
|
+
tracked.clear();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!Resize) {
|
|
137
|
+
throw new TypeError('Threshold fitting requires ResizeObserver');
|
|
138
|
+
}
|
|
139
|
+
if (!observer) {
|
|
140
|
+
observer = new Resize((entries) => {
|
|
141
|
+
for (const entry of entries) {
|
|
142
|
+
const rect = resizeRect(entry, view);
|
|
143
|
+
mark(entry.target, rect);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
const current = matchingHosts(
|
|
148
|
+
root,
|
|
149
|
+
levels,
|
|
150
|
+
limits,
|
|
151
|
+
options.boundaryAttribute
|
|
152
|
+
);
|
|
153
|
+
for (const [element, item] of tracked) {
|
|
154
|
+
if (!current.has(element)) {
|
|
155
|
+
observer.unobserve(element);
|
|
156
|
+
restore(element, item);
|
|
157
|
+
tracked.delete(element);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const [element, level] of current) {
|
|
161
|
+
if (!tracked.has(element)) {
|
|
162
|
+
tracked.set(element, {
|
|
163
|
+
level,
|
|
164
|
+
original: element.getAttribute(attribute),
|
|
165
|
+
});
|
|
166
|
+
observer.observe(element, { box: 'border-box' });
|
|
167
|
+
} else {
|
|
168
|
+
tracked.get(element).level = level;
|
|
169
|
+
}
|
|
170
|
+
mark(element, element.getBoundingClientRect());
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
// Watch numeric controls on the scope and dynamically inserted/retagged hosts.
|
|
174
|
+
const mutations = new Mutation((records) => {
|
|
175
|
+
if (
|
|
176
|
+
records.some(
|
|
177
|
+
(record) =>
|
|
178
|
+
record.type === 'childList' || record.attributeName !== attribute
|
|
179
|
+
)
|
|
180
|
+
) {
|
|
181
|
+
refresh();
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
refresh();
|
|
185
|
+
mutations.observe(root, { subtree: true, childList: true, attributes: true });
|
|
186
|
+
if (scope !== root && !root.contains(scope)) {
|
|
187
|
+
mutations.observe(scope, { attributes: true });
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
refresh,
|
|
191
|
+
dispose() {
|
|
192
|
+
if (disposed) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
disposed = true;
|
|
196
|
+
observer?.disconnect();
|
|
197
|
+
observer = null;
|
|
198
|
+
mutations.disconnect();
|
|
199
|
+
for (const [element, item] of tracked) {
|
|
200
|
+
restore(element, item);
|
|
201
|
+
}
|
|
202
|
+
tracked.clear();
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
package/src/html.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The package self-reference resolves to index.d.ts in TypeScript and Deno
|
|
2
|
+
// alike (a relative './index.js' resolves to the JavaScript file in Deno).
|
|
3
|
+
export { renderToHTML, escapeHtml } from '@link-foundation/ui-dom';
|
|
4
|
+
export type { HtmlOptions } from '@link-foundation/ui-dom';
|
package/src/html.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `renderToHTML(source, options)`: the static HTML of a document, with the
|
|
3
|
+
* same custom element tags and `data-link-id` attributes as `render`, for
|
|
4
|
+
* server rendering and snapshots. It needs no DOM: the kinds build into a
|
|
5
|
+
* minimal static document, serialized with every text and attribute value
|
|
6
|
+
* escaped. No `on*` attribute and no `<script>` is ever written.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { parse } from '@link-foundation/ui-core';
|
|
10
|
+
import { createStore } from '@link-foundation/ui-store/memory';
|
|
11
|
+
import { createTree } from './tree.js';
|
|
12
|
+
import { applyThemes } from './themes.js';
|
|
13
|
+
|
|
14
|
+
const VOID = new Set(['img', 'br', 'hr', 'input', 'meta', 'link', 'source']);
|
|
15
|
+
|
|
16
|
+
const ESCAPES = {
|
|
17
|
+
'&': '&',
|
|
18
|
+
'<': '<',
|
|
19
|
+
'>': '>',
|
|
20
|
+
'"': '"',
|
|
21
|
+
"'": ''',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Escape `& < > " '` (text content and attribute values alike). */
|
|
25
|
+
export function escapeHtml(text) {
|
|
26
|
+
let out = '';
|
|
27
|
+
for (const c of String(text)) {
|
|
28
|
+
out += Object.hasOwn(ESCAPES, c) ? ESCAPES[c] : c;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class StaticNode {
|
|
34
|
+
constructor(document) {
|
|
35
|
+
this.ownerDocument = document;
|
|
36
|
+
this.parentNode = null;
|
|
37
|
+
this.childNodes = [];
|
|
38
|
+
}
|
|
39
|
+
get firstChild() {
|
|
40
|
+
return this.childNodes[0] ?? null;
|
|
41
|
+
}
|
|
42
|
+
get children() {
|
|
43
|
+
return this.childNodes.filter((n) => n.nodeType === 1);
|
|
44
|
+
}
|
|
45
|
+
appendChild(node) {
|
|
46
|
+
return this.insertBefore(node, null);
|
|
47
|
+
}
|
|
48
|
+
insertBefore(node, ref) {
|
|
49
|
+
node.parentNode?.removeChild(node);
|
|
50
|
+
const i = ref === null ? -1 : this.childNodes.indexOf(ref);
|
|
51
|
+
this.childNodes.splice(i < 0 ? this.childNodes.length : i, 0, node);
|
|
52
|
+
node.parentNode = this;
|
|
53
|
+
return node;
|
|
54
|
+
}
|
|
55
|
+
removeChild(node) {
|
|
56
|
+
const i = this.childNodes.indexOf(node);
|
|
57
|
+
if (i >= 0) {
|
|
58
|
+
this.childNodes.splice(i, 1);
|
|
59
|
+
node.parentNode = null;
|
|
60
|
+
}
|
|
61
|
+
return node;
|
|
62
|
+
}
|
|
63
|
+
remove() {
|
|
64
|
+
this.parentNode?.removeChild(this);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class StaticText extends StaticNode {
|
|
69
|
+
constructor(document, data) {
|
|
70
|
+
super(document);
|
|
71
|
+
this.nodeType = 3;
|
|
72
|
+
this.data = String(data);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `element.style` with the `name: value;` serialization of the DOM. */
|
|
77
|
+
class StaticStyle {
|
|
78
|
+
constructor(element) {
|
|
79
|
+
this.element = element;
|
|
80
|
+
this.values = new Map();
|
|
81
|
+
}
|
|
82
|
+
setProperty(name, value) {
|
|
83
|
+
this.values.set(name, String(value));
|
|
84
|
+
this.sync();
|
|
85
|
+
}
|
|
86
|
+
removeProperty(name) {
|
|
87
|
+
this.values.delete(name);
|
|
88
|
+
this.sync();
|
|
89
|
+
}
|
|
90
|
+
getPropertyValue(name) {
|
|
91
|
+
return this.values.get(name) ?? '';
|
|
92
|
+
}
|
|
93
|
+
get cssText() {
|
|
94
|
+
return [...this.values].map(([n, v]) => `${n}: ${v};`).join(' ');
|
|
95
|
+
}
|
|
96
|
+
sync() {
|
|
97
|
+
if (this.values.size === 0) {
|
|
98
|
+
this.element.removeAttribute('style');
|
|
99
|
+
} else {
|
|
100
|
+
this.element.setAttribute('style', this.cssText);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
class StaticElement extends StaticNode {
|
|
106
|
+
constructor(document, tag) {
|
|
107
|
+
super(document);
|
|
108
|
+
this.nodeType = 1;
|
|
109
|
+
this.localName = String(tag).toLowerCase();
|
|
110
|
+
this.attributes = new Map();
|
|
111
|
+
this.style = new StaticStyle(this);
|
|
112
|
+
}
|
|
113
|
+
setAttribute(name, value) {
|
|
114
|
+
this.attributes.set(String(name).toLowerCase(), String(value));
|
|
115
|
+
}
|
|
116
|
+
getAttribute(name) {
|
|
117
|
+
return this.attributes.get(String(name).toLowerCase()) ?? null;
|
|
118
|
+
}
|
|
119
|
+
hasAttribute(name) {
|
|
120
|
+
return this.attributes.has(String(name).toLowerCase());
|
|
121
|
+
}
|
|
122
|
+
removeAttribute(name) {
|
|
123
|
+
this.attributes.delete(String(name).toLowerCase());
|
|
124
|
+
}
|
|
125
|
+
toggleAttribute(name, force) {
|
|
126
|
+
const on = force ?? !this.hasAttribute(name);
|
|
127
|
+
if (on && !this.hasAttribute(name)) {
|
|
128
|
+
this.setAttribute(name, '');
|
|
129
|
+
} else if (!on) {
|
|
130
|
+
this.removeAttribute(name);
|
|
131
|
+
}
|
|
132
|
+
return on;
|
|
133
|
+
}
|
|
134
|
+
get textContent() {
|
|
135
|
+
return this.childNodes
|
|
136
|
+
.map((n) => (n.nodeType === 3 ? n.data : n.textContent))
|
|
137
|
+
.join('');
|
|
138
|
+
}
|
|
139
|
+
/** The attribute selectors used by static control templates. */
|
|
140
|
+
querySelector(selector) {
|
|
141
|
+
const attribute = /^\[([^=\]]+)(?:="([^"]*)")?\]$/.exec(selector);
|
|
142
|
+
for (const child of this.children) {
|
|
143
|
+
const matches = attribute
|
|
144
|
+
? child.hasAttribute(attribute[1]) &&
|
|
145
|
+
(attribute[2] === undefined ||
|
|
146
|
+
child.getAttribute(attribute[1]) === attribute[2])
|
|
147
|
+
: child.localName === selector;
|
|
148
|
+
if (matches) {
|
|
149
|
+
return child;
|
|
150
|
+
}
|
|
151
|
+
const descendant = child.querySelector(selector);
|
|
152
|
+
if (descendant) {
|
|
153
|
+
return descendant;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
// Static output: events never happen.
|
|
159
|
+
addEventListener() {}
|
|
160
|
+
removeEventListener() {}
|
|
161
|
+
dispatchEvent() {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The minimal document the kinds build into (no window, no scripts). */
|
|
167
|
+
export function createStaticDocument() {
|
|
168
|
+
const document = {
|
|
169
|
+
defaultView: null,
|
|
170
|
+
title: '',
|
|
171
|
+
createElement: (tag) => new StaticElement(document, tag),
|
|
172
|
+
createTextNode: (data) => new StaticText(document, data),
|
|
173
|
+
};
|
|
174
|
+
return document;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Serialize a static node. */
|
|
178
|
+
export function serialize(node) {
|
|
179
|
+
if (node.nodeType === 3) {
|
|
180
|
+
return escapeHtml(node.data);
|
|
181
|
+
}
|
|
182
|
+
if (node.localName === 'script') {
|
|
183
|
+
return '';
|
|
184
|
+
}
|
|
185
|
+
let attributes = '';
|
|
186
|
+
for (const [name, value] of node.attributes) {
|
|
187
|
+
if (!name.startsWith('on')) {
|
|
188
|
+
attributes += ` ${name}="${escapeHtml(value)}"`;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const open = `<${node.localName}${attributes}>`;
|
|
192
|
+
if (VOID.has(node.localName)) {
|
|
193
|
+
return open;
|
|
194
|
+
}
|
|
195
|
+
return `${open}${node.childNodes.map(serialize).join('')}</${node.localName}>`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const isStore = (source) =>
|
|
199
|
+
typeof source?.subscribe === 'function' &&
|
|
200
|
+
typeof source?.roots === 'function';
|
|
201
|
+
|
|
202
|
+
/** A store of the source: LiNo text, a UiDocument, nodes, or a store. */
|
|
203
|
+
function storeOf(source, report) {
|
|
204
|
+
if (isStore(source)) {
|
|
205
|
+
return source;
|
|
206
|
+
}
|
|
207
|
+
if (typeof source === 'string') {
|
|
208
|
+
const parsed = parse(source);
|
|
209
|
+
parsed.diagnostics.forEach(report);
|
|
210
|
+
return createStore({ document: parsed });
|
|
211
|
+
}
|
|
212
|
+
if (Array.isArray(source)) {
|
|
213
|
+
return createStore({ document: { roots: source } });
|
|
214
|
+
}
|
|
215
|
+
if (Array.isArray(source?.roots)) {
|
|
216
|
+
return createStore({ document: source });
|
|
217
|
+
}
|
|
218
|
+
return createStore({ document: { roots: [source] } });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {string|import('@link-foundation/ui-store').Store|
|
|
223
|
+
* import('@link-foundation/ui-core').UiDocument|
|
|
224
|
+
* import('@link-foundation/ui-core').UiNode|
|
|
225
|
+
* import('@link-foundation/ui-core').UiNode[]} source
|
|
226
|
+
* @param {{kinds?: Map<string, object>|Record<string, object>,
|
|
227
|
+
* onDiagnostic?: (diagnostic: object) => void, prefix?: string}} [options]
|
|
228
|
+
* @returns {string}
|
|
229
|
+
*/
|
|
230
|
+
export function renderToHTML(source, options = {}) {
|
|
231
|
+
const report = (d) => options.onDiagnostic?.(d);
|
|
232
|
+
const document = createStaticDocument();
|
|
233
|
+
const container = document.createElement('template');
|
|
234
|
+
const store = storeOf(source, report);
|
|
235
|
+
const tree = createTree({
|
|
236
|
+
document,
|
|
237
|
+
container,
|
|
238
|
+
options,
|
|
239
|
+
interactive: false,
|
|
240
|
+
report,
|
|
241
|
+
});
|
|
242
|
+
tree.mount(store);
|
|
243
|
+
applyThemes(tree, store, options);
|
|
244
|
+
return container.childNodes.map(serialize).join('');
|
|
245
|
+
}
|