what-core 0.14.0 → 0.15.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 +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
// What Framework - the markup rules every renderer applies.
|
|
2
|
+
//
|
|
3
|
+
// One copy of each rule a value has to pass before it is written as HTML or
|
|
4
|
+
// set as an attribute: which tag and attribute names can be emitted verbatim,
|
|
5
|
+
// which attributes hold URLs and which URL schemes are refused, which props
|
|
6
|
+
// are never applied, how a prop name maps to its attribute, which attributes
|
|
7
|
+
// are enumerated, which elements are void, and how the text of <script> and
|
|
8
|
+
// <style> is kept from ending its element. what-compiler applies them to
|
|
9
|
+
// static markup when a module is compiled, what-server to every value it
|
|
10
|
+
// renders (compiled or h()), and the client to every attribute it sets, so a
|
|
11
|
+
// rule cannot hold on one path and lapse on another
|
|
12
|
+
// (packages/server/test/markup-rules-parity.test.js).
|
|
13
|
+
//
|
|
14
|
+
// Import-free, because what-compiler loads it into the build.
|
|
15
|
+
|
|
16
|
+
// Names are emitted verbatim (values are escaped, names cannot be), so anything
|
|
17
|
+
// outside these patterns could inject markup:
|
|
18
|
+
// h('div', { 'x" onload="alert(1)': '' }) -> <div x" onload="alert(1)>
|
|
19
|
+
// They allow data-*, aria-*, xlink:href, SVG names such as stroke-width and
|
|
20
|
+
// clipPath, custom elements, and the internal '__suspense' marker tag.
|
|
21
|
+
export const SAFE_ATTR_NAME = /^[a-zA-Z_:][a-zA-Z0-9:._-]*$/;
|
|
22
|
+
export const SAFE_TAG_NAME = /^[a-zA-Z_][a-zA-Z0-9._:-]*$/;
|
|
23
|
+
|
|
24
|
+
// Attributes whose value is a URL the browser follows, fetches or executes:
|
|
25
|
+
// xlink:href because SVG <a> executes it, ping and object[data] because they
|
|
26
|
+
// are fetched, and the SVG animation values (values, to, from, by), which
|
|
27
|
+
// <animate> or <set> with attributeName="href" sets as the href after the
|
|
28
|
+
// element's own attributes were checked. Compared in lower case, so the
|
|
29
|
+
// camelCase props (formAction, xlinkHref) are covered too.
|
|
30
|
+
export const URL_ATTRS = /* @__PURE__ */ new Set(['href', 'src', 'action', 'formaction', 'data', 'ping', 'xlink:href', 'xlinkhref', 'values', 'to', 'from', 'by']);
|
|
31
|
+
|
|
32
|
+
export function isUrlAttr(name) {
|
|
33
|
+
return URL_ATTRS.has(name.toLowerCase());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A URL with a javascript:, vbscript: or data: scheme, after whitespace and
|
|
37
|
+
// control characters (a superset of what a browser ignores) are removed, or
|
|
38
|
+
// a `;`-separated list (an animation's `values`) with one among its items. A
|
|
39
|
+
// value with no usable toString (Object.create(null), { toString: null }, both
|
|
40
|
+
// reachable from JSON) is refused rather than allowed to throw mid-render.
|
|
41
|
+
export function isUnsafeUrl(value) {
|
|
42
|
+
if (value == null) return false;
|
|
43
|
+
let normalized;
|
|
44
|
+
try {
|
|
45
|
+
normalized = String(value).replace(/[\p{Cc}\s]+/gu, '').toLowerCase();
|
|
46
|
+
} catch {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return normalized.split(';').some((url) => url.startsWith('javascript:') || url.startsWith('vbscript:') || url.startsWith('data:'));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Props whose value the browser parses as markup, so escaping is no defense:
|
|
53
|
+
// srcdoc is entity-decoded and parsed as a whole document, and outerHTML
|
|
54
|
+
// replaces the element with its value parsed as markup. Refused outright, in
|
|
55
|
+
// any letter case, on every path. innerHTML and dangerouslySetInnerHTML are the
|
|
56
|
+
// other two; they are accepted only as { __html: trustedString }.
|
|
57
|
+
export const REFUSED_ATTRS = /* @__PURE__ */ new Set(['srcdoc', 'outerhtml']);
|
|
58
|
+
|
|
59
|
+
export function isRefusedAttr(name) {
|
|
60
|
+
return REFUSED_ATTRS.has(name.toLowerCase());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const INNER_HTML_PROPS = /* @__PURE__ */ new Set(['innerHTML', 'dangerouslySetInnerHTML']);
|
|
64
|
+
|
|
65
|
+
// Prop names whose HTML attribute is not just the prop name in another case.
|
|
66
|
+
// The client reaches the right attribute through the element's reflected DOM
|
|
67
|
+
// property (`label.htmlFor = x` writes `for`); markup has no properties, so
|
|
68
|
+
// static templates and server markup rename them: an unrenamed `htmlFor="x"`
|
|
69
|
+
// parses as a meaningless `htmlfor` attribute, and hydration never corrects a
|
|
70
|
+
// static attribute. Names that differ from their attribute only by case
|
|
71
|
+
// (tabIndex, readOnly) need no entry: the parser lowercases attribute names.
|
|
72
|
+
export const ATTRIBUTE_ALIASES = /* @__PURE__ */ new Map([
|
|
73
|
+
['className', 'class'],
|
|
74
|
+
['htmlFor', 'for'],
|
|
75
|
+
['httpEquiv', 'http-equiv'],
|
|
76
|
+
['acceptCharset', 'accept-charset'],
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
export function attributeName(prop) {
|
|
80
|
+
return ATTRIBUTE_ALIASES.get(prop) || prop;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The props whose value lives in markup under another attribute: a field's
|
|
84
|
+
// default is its content attribute (an <input>'s defaultValue is its `value`
|
|
85
|
+
// attribute, what a form reset restores), so markup writes it there. A client
|
|
86
|
+
// writes these props as properties, which reflect to the same attributes.
|
|
87
|
+
const DEFAULT_ATTRIBUTES = /* @__PURE__ */ new Map([
|
|
88
|
+
['defaultValue', 'value'],
|
|
89
|
+
['defaultChecked', 'checked'],
|
|
90
|
+
['defaultSelected', 'selected'],
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
// SVG's XLink and XML attributes in React's spelling (`xlinkHref`, `xmlLang`):
|
|
94
|
+
// markup writes them under their qualified names (`xlink:href`, `xml:lang`),
|
|
95
|
+
// which the HTML parser places in their namespaces inside <svg>. Written as
|
|
96
|
+
// they are, they parse as `xlinkhref`, which no browser reads. `xmlns` and
|
|
97
|
+
// `xmlnsXlink` are declarations, not one of these.
|
|
98
|
+
const XML_PROP = /^(xlink|xml)([A-Z])/;
|
|
99
|
+
|
|
100
|
+
/** The attribute `prop` is written as in markup (server HTML, a compiled template). */
|
|
101
|
+
export function markupName(prop) {
|
|
102
|
+
return DEFAULT_ATTRIBUTES.get(prop) || attributeName(prop).replace(XML_PROP, (_, prefix, first) => prefix + ':' + first.toLowerCase());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// --- Form fields ---
|
|
106
|
+
//
|
|
107
|
+
// The props of a form field that hold its state: `value` (an <input> whose
|
|
108
|
+
// value is its own, a <textarea>, a <select>) and `checked` control what it
|
|
109
|
+
// shows; `defaultValue` and `defaultChecked` are its default, what a form
|
|
110
|
+
// reset restores. A server shows the value (else the default) in the one
|
|
111
|
+
// place markup has for both; a client writes the value as a property and the
|
|
112
|
+
// default as the attribute (what-core's dom.js, what-server's html.js).
|
|
113
|
+
export const FIELD_PROPS = /* @__PURE__ */ new Set(['value', 'defaultValue', 'checked', 'defaultChecked']);
|
|
114
|
+
export const FIELD_TAGS = /* @__PURE__ */ new Set(['input', 'textarea', 'select']);
|
|
115
|
+
|
|
116
|
+
// <input> types whose `value` is the attribute it submits or shows on a
|
|
117
|
+
// button, not a value the user edits: for them `value` is an attribute.
|
|
118
|
+
export const ATTRIBUTE_VALUE_TYPES = /* @__PURE__ */ new Set(['checkbox', 'radio', 'hidden', 'submit', 'reset', 'button', 'image']);
|
|
119
|
+
|
|
120
|
+
/** A string with ASCII whitespace stripped from both ends and collapsed to single spaces. */
|
|
121
|
+
export function stripAndCollapse(text) {
|
|
122
|
+
return text.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/g, '').replace(/[\t\n\f\r ]+/g, ' ');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The options a <select> value selects, as one boolean per option value in
|
|
127
|
+
* `values`: an array selects every option whose value it holds, any other
|
|
128
|
+
* value the first option with its string, a value no option has none. The
|
|
129
|
+
* client writes it (`select.value =` picks the first match, in a multiple
|
|
130
|
+
* select too) and a server marks it.
|
|
131
|
+
*/
|
|
132
|
+
export function selectedOptions(values, value) {
|
|
133
|
+
if (Array.isArray(value)) {
|
|
134
|
+
const wanted = value.map(String);
|
|
135
|
+
return values.map((v) => wanted.includes(v));
|
|
136
|
+
}
|
|
137
|
+
const at = values.indexOf(String(value));
|
|
138
|
+
return values.map((_, i) => i === at);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ARIA attributes and `role` take enumerated string values, never HTML boolean
|
|
142
|
+
// syntax: an absent aria-expanded means "unsupported" and aria-expanded="false"
|
|
143
|
+
// means "collapsed". data-* attributes are strings too, so `[data-open="false"]`
|
|
144
|
+
// keeps matching. HTML's own "true"/"false" attributes are enumerated as well:
|
|
145
|
+
// draggable="" is the auto state (a <div> is not draggable), and an absent
|
|
146
|
+
// spellcheck or contenteditable inherits from the parent, so `false` has to be
|
|
147
|
+
// written out. Everything else follows HTML boolean semantics: true writes the
|
|
148
|
+
// bare name, false omits it.
|
|
149
|
+
export function isAriaAttr(name) {
|
|
150
|
+
return name === 'role' || name.startsWith('aria-');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function isEnumeratedAttr(name) {
|
|
154
|
+
return isAriaAttr(name) || name.startsWith('data-') || /^(draggable|spellcheck|contenteditable)$/i.test(name);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
158
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
159
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
160
|
+
]);
|
|
161
|
+
|
|
162
|
+
// --- Namespaces ---
|
|
163
|
+
//
|
|
164
|
+
// The parser builds <svg> and <math> content in the SVG and MathML namespaces,
|
|
165
|
+
// except where it switches back to HTML (its integration points): the
|
|
166
|
+
// children of SVG <foreignObject>, <desc> and <title>; of the MathML token
|
|
167
|
+
// elements <mi>, <mo>, <mn>, <ms> and <mtext>, apart from <mglyph> and
|
|
168
|
+
// <malignmark>, which stay MathML; and of MathML <annotation-xml> whose
|
|
169
|
+
// encoding is text/html or application/xhtml+xml.
|
|
170
|
+
export const SVG_HTML_INTEGRATION_POINTS = /* @__PURE__ */ new Set(['foreignObject', 'desc', 'title']);
|
|
171
|
+
export const MATHML_TEXT_INTEGRATION_POINTS = /* @__PURE__ */ new Set(['mi', 'mo', 'mn', 'ms', 'mtext']);
|
|
172
|
+
export const MATHML_IN_TEXT_POINTS = /* @__PURE__ */ new Set(['mglyph', 'malignmark']);
|
|
173
|
+
export const HTML_ENCODINGS = /* @__PURE__ */ new Set(['text/html', 'application/xhtml+xml']);
|
|
174
|
+
|
|
175
|
+
// The namespace the children of `tag` are parsed in, given the namespace `tag`
|
|
176
|
+
// itself is in: null for HTML, 'svg', 'math', or 'foreign' for SVG or MathML
|
|
177
|
+
// content whose exact namespace a renderer does not know, which never switches
|
|
178
|
+
// back to HTML. `encoding` is the element's encoding attribute (it decides
|
|
179
|
+
// <annotation-xml>).
|
|
180
|
+
export function childNamespace(ns, tag, encoding) {
|
|
181
|
+
if (ns === null) {
|
|
182
|
+
if (tag === 'svg') return 'svg';
|
|
183
|
+
return tag === 'math' || MATHML_IN_TEXT_POINTS.has(tag) ? 'math' : null;
|
|
184
|
+
}
|
|
185
|
+
if (ns === 'svg') return SVG_HTML_INTEGRATION_POINTS.has(tag) ? null : 'svg';
|
|
186
|
+
if (ns === 'math') {
|
|
187
|
+
if (MATHML_TEXT_INTEGRATION_POINTS.has(tag)) return null;
|
|
188
|
+
return tag === 'annotation-xml' && typeof encoding === 'string' && HTML_ENCODINGS.has(encoding.toLowerCase()) ? null : 'math';
|
|
189
|
+
}
|
|
190
|
+
return ns;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Elements whose content the HTML parser reads as raw text: nothing in it is
|
|
194
|
+
// decoded, so HTML-escaping corrupts it (`a && b` is not JavaScript),
|
|
195
|
+
// and only the closing sequence ends it. Its text is written as it is, apart
|
|
196
|
+
// from the sequences encodeRawText replaces. In SVG or MathML they are
|
|
197
|
+
// ordinary elements whose content is parsed as markup, so there their text is
|
|
198
|
+
// escaped (rawTextContent).
|
|
199
|
+
export const RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(['script', 'style']);
|
|
200
|
+
|
|
201
|
+
// Elements whose content the HTML parser reads as text that it decodes:
|
|
202
|
+
// <textarea> and <title> in HTML. Nothing in it is markup, a comment included,
|
|
203
|
+
// so a server writes their children as escaped text with no hydration
|
|
204
|
+
// markers, and the client hydrates that text as one text node.
|
|
205
|
+
export const TEXT_ONLY_ELEMENTS = /* @__PURE__ */ new Set(['textarea', 'title']);
|
|
206
|
+
|
|
207
|
+
// The HTML parser drops a newline that comes right after one of these start
|
|
208
|
+
// tags, so a server writes one there: the parser drops that one, and content
|
|
209
|
+
// that begins with a newline keeps its own.
|
|
210
|
+
export const LEADING_NEWLINE_ELEMENTS = /* @__PURE__ */ new Set(['pre', 'textarea', 'listing']);
|
|
211
|
+
|
|
212
|
+
// The sequences that could end a raw-text element or change how the parser
|
|
213
|
+
// reads it: `</` (which starts every closing tag, in any letter case) and, in
|
|
214
|
+
// a script, `<!--`, which can switch the parser into a state where the real
|
|
215
|
+
// closing tag no longer closes the element.
|
|
216
|
+
export function rawTextHazard(tag, text) {
|
|
217
|
+
return text.includes('</') || (tag === 'script' && text.includes('<!--'));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Raw text, encoded so it cannot end its element. `<\/` means `</` inside a
|
|
221
|
+
// CSS or JavaScript string and in JSON; `<!--` means `<!--` inside a
|
|
222
|
+
// JavaScript or JSON string. Text without either sequence is unchanged, so
|
|
223
|
+
// static text that passed rawTextHazard encodes to itself.
|
|
224
|
+
export function encodeRawText(tag, text) {
|
|
225
|
+
if (!rawTextHazard(tag, text)) return text;
|
|
226
|
+
const out = text.replace(/<\//g, '<\\/');
|
|
227
|
+
return tag === 'script' ? out.replace(/<!--/g, '\\u003C!--') : out;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// The content of a <script> or <style> whose text is `text`, written in
|
|
231
|
+
// namespace `ns`. In HTML it is raw text (encodeRawText); in SVG or MathML it
|
|
232
|
+
// is parsed as markup, so `&` and `<` are escaped, and nothing else, so text
|
|
233
|
+
// that holds neither is the same bytes in every namespace.
|
|
234
|
+
export function rawTextContent(tag, text, ns) {
|
|
235
|
+
if (ns === null) return encodeRawText(tag, text);
|
|
236
|
+
return text.replace(/&/g, '&').replace(/</g, '<');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Text a compiler can write into a raw-text element as it is, whatever
|
|
240
|
+
// namespace the element ends up in: no `&` and no `<`.
|
|
241
|
+
export function isNamespaceNeutralText(text) {
|
|
242
|
+
return !/[&<]/.test(text);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// The five-character HTML escape every server renderer and what-compiler use
|
|
246
|
+
// for text and attribute values. A test() for any character that needs
|
|
247
|
+
// escaping, an early return for the common clean string (test() is cheaper
|
|
248
|
+
// than search(), which saves and restores the expression's lastIndex), then a
|
|
249
|
+
// search for the first one and one pass from there: the same output as five
|
|
250
|
+
// chained replace() calls without scanning the string five times.
|
|
251
|
+
const NEEDS_ESCAPE = /[&<>"']/;
|
|
252
|
+
|
|
253
|
+
export function escapeHtml(str) {
|
|
254
|
+
if (!NEEDS_ESCAPE.test(str)) return str;
|
|
255
|
+
const first = str.search(NEEDS_ESCAPE);
|
|
256
|
+
let out = '';
|
|
257
|
+
let last = 0;
|
|
258
|
+
for (let i = first; i < str.length; i++) {
|
|
259
|
+
let replacement;
|
|
260
|
+
switch (str.charCodeAt(i)) {
|
|
261
|
+
case 38: replacement = '&'; break;
|
|
262
|
+
case 60: replacement = '<'; break;
|
|
263
|
+
case 62: replacement = '>'; break;
|
|
264
|
+
case 34: replacement = '"'; break;
|
|
265
|
+
case 39: replacement = '''; break;
|
|
266
|
+
default: continue;
|
|
267
|
+
}
|
|
268
|
+
if (last < i) out += str.slice(last, i);
|
|
269
|
+
out += replacement;
|
|
270
|
+
last = i + 1;
|
|
271
|
+
}
|
|
272
|
+
return last < str.length ? out + str.slice(last) : out;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// A style object as CSS text, as what-server writes a style prop and
|
|
276
|
+
// what-framework/dom's style() returns it: camelCase properties in
|
|
277
|
+
// kebab-case, custom properties (--x) as they are. A property whose value is
|
|
278
|
+
// null, undefined, a boolean or empty is left out, as the client sets none
|
|
279
|
+
// for it (React leaves out the same values). A declaration the client's
|
|
280
|
+
// CSSOM would refuse (a name that is not a property or a custom property, a
|
|
281
|
+
// value that ends the declaration or opens a block) is left out, as the
|
|
282
|
+
// client leaves it out: the server must not write
|
|
283
|
+
// `color: red; background: url(//tracker)` from one value.
|
|
284
|
+
const CSS_PROPERTY = /^(?:--[\w-]+|-?[a-zA-Z][\w-]*)$/;
|
|
285
|
+
|
|
286
|
+
// Control characters other than CSS whitespace: tab, LF, FF, CR.
|
|
287
|
+
const isControl = (code) => (code < 0x20 && code !== 0x09 && code !== 0x0a && code !== 0x0c && code !== 0x0d) || code === 0x7f;
|
|
288
|
+
|
|
289
|
+
// Whether `text` is a value in a conservative CSS grammar, which a CSS parser
|
|
290
|
+
// reads as one declaration's value whatever it does with it:
|
|
291
|
+
// - no control characters;
|
|
292
|
+
// - outside strings, no `\` (an escape makes the next character, a quote or
|
|
293
|
+
// a parenthesis, part of an identifier), no comment (`/*`, which can hide a
|
|
294
|
+
// quote or a parenthesis from a reader that knows no comments) and no braces;
|
|
295
|
+
// - strings close on their own line (`\` escapes the next character in them);
|
|
296
|
+
// - parentheses and brackets nest and match, and a `;` is allowed only inside
|
|
297
|
+
// them (`url(data:image/png;base64,…)`);
|
|
298
|
+
// - an unquoted `url(` holds one url token: a CSS parser reads it to the
|
|
299
|
+
// first `)` whatever it holds, and one with a quote, a `(` or inner
|
|
300
|
+
// whitespace is a bad url, which also ends at the first `)`, so the `;`
|
|
301
|
+
// after that `)` would end the declaration (`url(a(b);x:y)`).
|
|
302
|
+
// A value outside the grammar is dropped, even one a browser would accept
|
|
303
|
+
// (`red /* note */`): style objects never need one.
|
|
304
|
+
export function oneDeclaration(text) {
|
|
305
|
+
const open = [];
|
|
306
|
+
let quote = '';
|
|
307
|
+
for (let i = 0; i < text.length; i++) {
|
|
308
|
+
const c = text[i];
|
|
309
|
+
if (isControl(text.charCodeAt(i))) return false;
|
|
310
|
+
if (quote !== '') {
|
|
311
|
+
if (c === '\\') i++;
|
|
312
|
+
else if (c === quote) quote = '';
|
|
313
|
+
else if (c === '\n' || c === '\r' || c === '\f') return false;
|
|
314
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
315
|
+
else if (c === '(' && isUrlFunction(text, i)) {
|
|
316
|
+
const end = urlTokenEnd(text, i + 1);
|
|
317
|
+
if (end === -1) return false;
|
|
318
|
+
if (end > i) i = end;
|
|
319
|
+
else open.push(')');
|
|
320
|
+
} else if (c === '(' || c === '[') open.push(c === '(' ? ')' : ']');
|
|
321
|
+
else if (c === ')' || c === ']') {
|
|
322
|
+
if (open.pop() !== c) return false;
|
|
323
|
+
} else if (c === '\\' || c === '{' || c === '}' || (c === '/' && text[i + 1] === '*') || (c === ';' && open.length === 0)) return false;
|
|
324
|
+
}
|
|
325
|
+
return open.length === 0 && quote === '';
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Whether the `(` at `at` opens `url(`: the identifier before it is `url`, in
|
|
329
|
+
// any case.
|
|
330
|
+
function isUrlFunction(text, at) {
|
|
331
|
+
return at >= 3 && text.slice(at - 3, at).toLowerCase() === 'url' && (at === 3 || !/[\w\u0080-\uffff-]/.test(text[at - 4]));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Where the url token that starts at `from` (after `url(`) ends: the index of
|
|
335
|
+
// its `)`; `from - 1` when the argument is a string instead (a quote after
|
|
336
|
+
// any whitespace), read as one; -1 when it is a bad url or never closes.
|
|
337
|
+
function urlTokenEnd(text, from) {
|
|
338
|
+
let i = from;
|
|
339
|
+
while (i < text.length && /[ \t\n\r\f]/.test(text[i])) i++;
|
|
340
|
+
if (text[i] === '"' || text[i] === "'") return from - 1;
|
|
341
|
+
for (; i < text.length; i++) {
|
|
342
|
+
const c = text[i];
|
|
343
|
+
if (c === ')') return i;
|
|
344
|
+
if (c === '"' || c === "'" || c === '(' || c === '\\') return -1;
|
|
345
|
+
if (/[ \t\n\r\f]/.test(c)) {
|
|
346
|
+
while (i < text.length && /[ \t\n\r\f]/.test(text[i])) i++;
|
|
347
|
+
return text[i] === ')' ? i : -1;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return -1;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// A property name as it is written (kebab-case; a custom property as it is),
|
|
354
|
+
// or null for a name that is no property: decided once per name. Bounded,
|
|
355
|
+
// because a style object's keys can come from data.
|
|
356
|
+
export const _cssNames = new Map();
|
|
357
|
+
export const CSS_NAME_CACHE_SIZE = 1024;
|
|
358
|
+
|
|
359
|
+
function cssName(property) {
|
|
360
|
+
let name = _cssNames.get(property);
|
|
361
|
+
if (name === undefined) {
|
|
362
|
+
name = !CSS_PROPERTY.test(property) ? null
|
|
363
|
+
: property.startsWith('--') ? property : property.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
364
|
+
if (_cssNames.size < CSS_NAME_CACHE_SIZE) _cssNames.set(property, name);
|
|
365
|
+
}
|
|
366
|
+
return name;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Values that are one declaration's value by their characters alone: letters,
|
|
370
|
+
// digits, `_`, `#`, `%`, `.`, `,`, `-`, `+` and spaces, none of which
|
|
371
|
+
// oneDeclaration gives a meaning to. Every other value is read by it.
|
|
372
|
+
export const PLAIN_CSS_VALUE = /^[\w#%.,+ -]*$/;
|
|
373
|
+
|
|
374
|
+
export function styleText(style) {
|
|
375
|
+
let css = '';
|
|
376
|
+
const properties = Object.keys(style);
|
|
377
|
+
for (let i = 0; i < properties.length; i++) {
|
|
378
|
+
const property = properties[i];
|
|
379
|
+
const value = style[property];
|
|
380
|
+
if (value == null || value === '' || typeof value === 'boolean') continue;
|
|
381
|
+
const text = String(value);
|
|
382
|
+
const name = cssName(property);
|
|
383
|
+
if (name === null || !(PLAIN_CSS_VALUE.test(text) || oneDeclaration(text))) continue;
|
|
384
|
+
css += (css ? ';' : '') + name + ':' + text;
|
|
385
|
+
}
|
|
386
|
+
return css;
|
|
387
|
+
}
|