-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.js
More file actions
212 lines (189 loc) · 6.12 KB
/
Copy pathhtml.js
File metadata and controls
212 lines (189 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { isSignalRef } from "./signals.js";
import { attributeName, matchAttribute, normalizeAttributeConfig } from "./attributes.js";
const templateKind = Symbol.for("@async/framework.template");
const rawKind = Symbol.for("@async/framework.rawHtml");
const childrenKind = Symbol.for("@async/framework.children");
const signalRefKind = Symbol.for("@async/framework.signalRef");
export function html(strings, ...values) {
return {
[templateKind]: true,
strings,
values
};
}
export function isTemplateResult(value) {
return Boolean(value?.[templateKind]);
}
export function rawHtml(value) {
return {
[rawKind]: true,
html: String(value ?? "")
};
}
export function childrenFragment(source) {
if (isChildrenFragment(source)) {
return source;
}
let consumed = false;
const fragment = {
[childrenKind]: true,
consume(context = createRenderContext()) {
if (consumed) {
throw new Error("Async children fragments can only be consumed once.");
}
consumed = true;
const value = typeof source === "function"
? source.call(context.fragmentContext ?? undefined)
: source;
return renderTemplate(value, context);
}
};
return Object.freeze(fragment);
}
export function isChildrenFragment(value) {
return Boolean(value?.[childrenKind]);
}
export function renderTemplate(value, options = {}) {
if (isTemplateResult(value)) {
const context = createRenderContext(options);
let output = "";
for (let index = 0; index < value.strings.length; index += 1) {
output += value.strings[index];
if (index < value.values.length) {
output += renderValue(value.values[index], {
...context,
attribute: readAttributeContext(value.strings[index])
});
}
}
return output;
}
return renderValue(value, createRenderContext(options));
}
function renderValue(value, context = createRenderContext()) {
if (isChildrenFragment(value)) {
return value.consume(context);
}
if (value?.[rawKind]) {
return value.html;
}
if (isTemplateResult(value)) {
return renderTemplate(value, context);
}
if (context.attribute) {
return renderAttributeValue(value, context);
}
if (Array.isArray(value)) {
return value.map((item) => renderValue(item, context)).join("");
}
if (isSignalRef(value)) {
return escapeHtml(value.value);
}
if (value == null || value === false) {
return "";
}
return escapeHtml(value);
}
function renderAttributeValue(value, context) {
const signalName = matchAttribute(context.attribute.name, context.attributes, "signal");
const className = matchAttribute(context.attribute.name, context.attributes, "class");
const signalPath = signalPathFor(value, context);
if (context.attribute.name === "value" && signalPath) {
const currentValue = readSignalValue(value, context);
const signalValueAttribute = attributeName(context.attributes, "signal", "value");
return `${escapeHtml(currentValue)}${context.attribute.quote} ${signalValueAttribute}=${context.attribute.quote}${escapeHtml(signalPath)}`;
}
if (signalName != null || className != null) {
if (signalPath) {
return escapeHtml(signalPath);
}
if (isInlineBindingValue(value)) {
return escapeHtml(registerInlineBinding(value, context));
}
}
return renderValueAsAttributeLiteral(value, context);
}
function renderValueAsAttributeLiteral(value, context) {
if (Array.isArray(value)) {
return value.map((item) => renderValueAsAttributeLiteral(item, context)).join("");
}
if (isSignalRef(value)) {
return escapeHtml(value.value);
}
if (value == null || value === false) {
return "";
}
return escapeHtml(value);
}
function createRenderContext(options = {}) {
return {
...options,
attributes: normalizeAttributeConfig(options.attributes)
};
}
function readAttributeContext(source) {
const match = source.match(/(?:^|[\s<])([^\s"'=<>`]+)\s*=\s*(["'])$/);
if (!match) {
return null;
}
return {
name: match[1],
quote: match[2]
};
}
function signalPathFor(value, context) {
if (isSignalRef(value)) {
// Ref ids are registry-local. A ref from the active registry can use the
// compact named path; a foreign ref must stay an inline binding so reads,
// writes, and subscriptions continue to target its owning registry.
return context.signals === undefined || value[signalRefKind] === context.signals
? value.id
: null;
}
if (typeof value === "string" && context.signals?.has?.(value)) {
return value;
}
return null;
}
function readSignalValue(value, context) {
if (isSignalRef(value)) {
return value.value;
}
if (typeof value === "string" && context.signals?.has?.(value)) {
return context.signals.get(value);
}
return value;
}
function isInlineBindingValue(value) {
return Boolean(value && typeof value === "object");
}
function registerInlineBinding(value, context) {
if (typeof context.bind !== "function") {
// Same misconfiguration throws in component renders; silently emitting
// the value here produced "[object Object]" attribute garbage instead.
throw new TypeError("Inline template bindings require a Loader.");
}
return context.bind(value);
}
export function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
// Canonical script-JSON escaper for inline <script type="application/json">
// payloads (snapshots, stream patches): "<" must not open a closing tag.
export function escapeScriptJson(value) {
return JSON.stringify(value).replaceAll("<", "\\u003c");
}
// Component fragment comment-marker protocol. Built by component scopedHtml
// output and parsed by the loader's fragment-target resolution — one owner
// keeps the producer and consumer wire formats from drifting.
export function fragmentMarkerData(marker, edge) {
return `async:${marker}:${edge}`;
}
export function fragmentMarkerComments(marker, html) {
return `<!--${fragmentMarkerData(marker, "start")}-->${html}<!--${fragmentMarkerData(marker, "end")}-->`;
}