PageSourceSearch

https://pika.page/assets/hast-util-to-html-aa0f5db409d4bda68101e16473545bff6f133c05f1eb8cc0ee5dc43785c17c07.js

js pika.page collected 2026-09-25 18:56:46 UTC 19,995 bytes, 461 lines download raw bytes

1// [email protected] downloaded from https://ga.jspm.io/npm:[email protected]/index.js
2
3import{htmlVoidElements as t}from"html-void-elements";import{svg as e,find as n,html as a}from"property-information";import{zwitch as o}from"zwitch";import{stringifyEntities as s}from"stringify-entities";import{ccount as r}from"ccount";import{stringify as i}from"comma-separated-tokens";import{stringify as l}from"space-separated-tokens";import{whitespace as c}from"hast-util-whitespace";const m=/^>|^->|<!--|-->|--!>|<!-$/g;const u=[">"];const g=["<",">"];
4/**
5 * Serialize a comment.
6 *
7 * @param {Comment} node
8 *   Node to handle.
9 * @param {number | undefined} _1
10 *   Index of `node` in `parent.
11 * @param {Parents | undefined} _2
12 *   Parent of `node`.
13 * @param {State} state
14 *   Info passed around about the current state.
15 * @returns {string}
16 *   Serialized node.
17 */function comment(t,e,n,a){return a.settings.bogusComments?"<?"+s(t.value,Object.assign({},a.settings.characterReferences,{subset:u}))+">":"\x3c!--"+t.value.replace(m,encode)+"--\x3e"
18/**
19   * @param {string} $0
20   */;function encode(t){return s(t,Object.assign({},a.settings.characterReferences,{subset:g}))}}
21/**
22 * Serialize a doctype.
23 *
24 * @param {Doctype} _1
25 *   Node to handle.
26 * @param {number | undefined} _2
27 *   Index of `node` in `parent.
28 * @param {Parents | undefined} _3
29 *   Parent of `node`.
30 * @param {State} state
31 *   Info passed around about the current state.
32 * @returns {string}
33 *   Serialized node.
34 */function doctype(t,e,n,a){return"<!"+(a.settings.upperDoctype?"DOCTYPE":"doctype")+(a.settings.tightDoctype?"":" ")+"html>"}const f=siblings(1);const d=siblings(-1);
35/** @type {Array<RootContent>} */const h=[];
36/**
37 * Factory to check siblings in a direction.
38 *
39 * @param {number} increment
40 */function siblings(t){return sibling
41/**
42   * Find applicable siblings in a direction.
43   *
44   * @template {Parents} Parent
45   *   Parent type.
46   * @param {Parent | undefined} parent
47   *   Parent.
48   * @param {number | undefined} index
49   *   Index of child in `parent`.
50   * @param {boolean | undefined} [includeWhitespace=false]
51   *   Whether to include whitespace (default: `false`).
52   * @returns {Parent extends {children: Array<infer Child>} ? Child | undefined : never}
53   *   Child of parent.
54   */;function sibling(e,n,a){const o=e?e.children:h;let s=(n||0)+t;let r=o[s];if(!a)while(r&&c(r)){s+=t;r=o[s]}return r}}
55/**
56 * @callback OmitHandle
57 *   Check if a tag can be omitted.
58 * @param {Element} element
59 *   Element to check.
60 * @param {number | undefined} index
61 *   Index of element in parent.
62 * @param {Parents | undefined} parent
63 *   Parent of element.
64 * @returns {boolean}
65 *   Whether to omit a tag.
66 *
67 */const N={}.hasOwnProperty;
68/**
69 * Factory to check if a given node can have a tag omitted.
70 *
71 * @param {Record<string, OmitHandle>} handlers
72 *   Omission handlers, where each key is a tag name, and each value is the
73 *   corresponding handler.
74 * @returns {OmitHandle}
75 *   Whether to omit a tag of an element.
76 */function omission(t){return omit
77/**
78   * Check if a given node can have a tag omitted.
79   *
80   * @type {OmitHandle}
81   */;function omit(e,n,a){return N.call(t,e.tagName)&&t[e.tagName](e,n,a)}}const y=omission({body:body$1,caption:headOrColgroupOrCaption,colgroup:headOrColgroupOrCaption,dd:dd,dt:dt,head:headOrColgroupOrCaption,html:html$1,li:li,optgroup:optgroup,option:option,p:p,rp:rubyElement,rt:rubyElement,tbody:tbody$1,td:cells,tfoot:tfoot,th:cells,thead:thead,tr:tr});
82/**
83 * Macro for `</head>`, `</colgroup>`, and `</caption>`.
84 *
85 * @param {Element} _
86 *   Element.
87 * @param {number | undefined} index
88 *   Index of element in parent.
89 * @param {Parents | undefined} parent
90 *   Parent of element.
91 * @returns {boolean}
92 *   Whether the closing tag can be omitted.
93 */function headOrColgroupOrCaption(t,e,n){const a=f(n,e,true);return!a||a.type!=="comment"&&!(a.type==="text"&&c(a.value.charAt(0)))}
94/**
95 * Whether to omit `</html>`.
96 *
97 * @param {Element} _
98 *   Element.
99 * @param {number | undefined} index
100 *   Index of element in parent.
101 * @param {Parents | undefined} parent
102 *   Parent of element.
103 * @returns {boolean}
104 *   Whether the closing tag can be omitted.
105 */function html$1(t,e,n){const a=f(n,e);return!a||a.type!=="comment"}
106/**
107 * Whether to omit `</body>`.
108 *
109 * @param {Element} _
110 *   Element.
111 * @param {number | undefined} index
112 *   Index of element in parent.
113 * @param {Parents | undefined} parent
114 *   Parent of element.
115 * @returns {boolean}
116 *   Whether the closing tag can be omitted.
117 */function body$1(t,e,n){const a=f(n,e);return!a||a.type!=="comment"}
118/**
119 * Whether to omit `</p>`.
120 *
121 * @param {Element} _
122 *   Element.
123 * @param {number | undefined} index
124 *   Index of element in parent.
125 * @param {Parents | undefined} parent
126 *   Parent of element.
127 * @returns {boolean}
128 *   Whether the closing tag can be omitted.
129 */function p(t,e,n){const a=f(n,e);return a?a.type==="element"&&(a.tagName==="address"||a.tagName==="article"||a.tagName==="aside"||a.tagName==="blockquote"||a.tagName==="details"||a.tagName==="div"||a.tagName==="dl"||a.tagName==="fieldset"||a.tagName==="figcaption"||a.tagName==="figure"||a.tagName==="footer"||a.tagName==="form"||a.tagName==="h1"||a.tagName==="h2"||a.tagName==="h3"||a.tagName==="h4"||a.tagName==="h5"||a.tagName==="h6"||a.tagName==="header"||a.tagName==="hgroup"||a.tagName==="hr"||a.tagName==="main"||a.tagName==="menu"||a.tagName==="nav"||a.tagName==="ol"||a.tagName==="p"||a.tagName==="pre"||a.tagName==="section"||a.tagName==="table"||a.tagName==="ul"):!n||!(n.t
129ype==="element"&&(n.tagName==="a"||n.tagName==="audio"||n.tagName==="del"||n.tagName==="ins"||n.tagName==="map"||n.tagName==="noscript"||n.tagName==="video"))}
130/**
131 * Whether to omit `</li>`.
132 *
133 * @param {Element} _
134 *   Element.
135 * @param {number | undefined} index
136 *   Index of element in parent.
137 * @param {Parents | undefined} parent
138 *   Parent of element.
139 * @returns {boolean}
140 *   Whether the closing tag can be omitted.
141 */function li(t,e,n){const a=f(n,e);return!a||a.type==="element"&&a.tagName==="li"}
142/**
143 * Whether to omit `</dt>`.
144 *
145 * @param {Element} _
146 *   Element.
147 * @param {number | undefined} index
148 *   Index of element in parent.
149 * @param {Parents | undefined} parent
150 *   Parent of element.
151 * @returns {boolean}
152 *   Whether the closing tag can be omitted.
153 */function dt(t,e,n){const a=f(n,e);return Boolean(a&&a.type==="element"&&(a.tagName==="dt"||a.tagName==="dd"))}
154/**
155 * Whether to omit `</dd>`.
156 *
157 * @param {Element} _
158 *   Element.
159 * @param {number | undefined} index
160 *   Index of element in parent.
161 * @param {Parents | undefined} parent
162 *   Parent of element.
163 * @returns {boolean}
164 *   Whether the closing tag can be omitted.
165 */function dd(t,e,n){const a=f(n,e);return!a||a.type==="element"&&(a.tagName==="dt"||a.tagName==="dd")}
166/**
167 * Whether to omit `</rt>` or `</rp>`.
168 *
169 * @param {Element} _
170 *   Element.
171 * @param {number | undefined} index
172 *   Index of element in parent.
173 * @param {Parents | undefined} parent
174 *   Parent of element.
175 * @returns {boolean}
176 *   Whether the closing tag can be omitted.
177 */function rubyElement(t,e,n){const a=f(n,e);return!a||a.type==="element"&&(a.tagName==="rp"||a.tagName==="rt")}
178/**
179 * Whether to omit `</optgroup>`.
180 *
181 * @param {Element} _
182 *   Element.
183 * @param {number | undefined} index
184 *   Index of element in parent.
185 * @param {Parents | undefined} parent
186 *   Parent of element.
187 * @returns {boolean}
188 *   Whether the closing tag can be omitted.
189 */function optgroup(t,e,n){const a=f(n,e);return!a||a.type==="element"&&a.tagName==="optgroup"}
190/**
191 * Whether to omit `</option>`.
192 *
193 * @param {Element} _
194 *   Element.
195 * @param {number | undefined} index
196 *   Index of element in parent.
197 * @param {Parents | undefined} parent
198 *   Parent of element.
199 * @returns {boolean}
200 *   Whether the closing tag can be omitted.
201 */function option(t,e,n){const a=f(n,e);return!a||a.type==="element"&&(a.tagName==="option"||a.tagName==="optgroup")}
202/**
203 * Whether to omit `</thead>`.
204 *
205 * @param {Element} _
206 *   Element.
207 * @param {number | undefined} index
208 *   Index of element in parent.
209 * @param {Parents | undefined} parent
210 *   Parent of element.
211 * @returns {boolean}
212 *   Whether the closing tag can be omitted.
213 */function thead(t,e,n){const a=f(n,e);return Boolean(a&&a.type==="element"&&(a.tagName==="tbody"||a.tagName==="tfoot"))}
214/**
215 * Whether to omit `</tbody>`.
216 *
217 * @param {Element} _
218 *   Element.
219 * @param {number | undefined} index
220 *   Index of element in parent.
221 * @param {Parents | undefined} parent
222 *   Parent of element.
223 * @returns {boolean}
224 *   Whether the closing tag can be omitted.
225 */function tbody$1(t,e,n){const a=f(n,e);return!a||a.type==="element"&&(a.tagName==="tbody"||a.tagName==="tfoot")}
226/**
227 * Whether to omit `</tfoot>`.
228 *
229 * @param {Element} _
230 *   Element.
231 * @param {number | undefined} index
232 *   Index of element in parent.
233 * @param {Parents | undefined} parent
234 *   Parent of element.
235 * @returns {boolean}
236 *   Whether the closing tag can be omitted.
237 */function tfoot(t,e,n){return!f(n,e)}
238/**
239 * Whether to omit `</tr>`.
240 *
241 * @param {Element} _
242 *   Element.
243 * @param {number | undefined} index
244 *   Index of element in parent.
245 * @param {Parents | undefined} parent
246 *   Parent of element.
247 * @returns {boolean}
248 *   Whether the closing tag can be omitted.
249 */function tr(t,e,n){const a=f(n,e);return!a||a.type==="element"&&a.tagName==="tr"}
250/**
251 * Whether to omit `</td>` or `</th>`.
252 *
253 * @param {Element} _
254 *   Element.
255 * @param {number | undefined} index
256 *   Index of element in parent.
257 * @param {Parents | undefined} parent
258 *   Parent of element.
259 * @returns {boolean}
260 *   Whether the closing tag can be omitted.
261 */function cells(t,e,n){const a=f(n,e);return!a||a.type==="element"&&(a.tagName==="td"||a.tagName==="th")}const b=omission({body:body,colgroup:colgroup,head:head,html:html,tbody:tbody});
262/**
263 * Whether to omit `<html>`.
264 *
265 * @param {Element} node
266 *   Element.
267 * @returns {boolean}
268 *   Whether the opening tag can be omitted.
269 */function html(t){const e=f(t,-1);return!e||e.type!=="comment"}
270/**
271 * Whether to omit `<head>`.
272 *
273 * @param {Element} node
274 *   Element.
275 * @returns {boolean}
276 *   Whether the opening tag can be omitted.
277 */function head(t){
278/** @type {Set<string>} */
279const e=new Set;for(const n of t.children)if(n.type==="element"&&(n.tagName==="base"||n.tagName==="title")){if(e.has(n.tagName))return false;e.add(n.tagName)}const n=t.children[0];return!n||n.type==="element"}
280/**
281 * Whether to omit `<body>`.
282 *
283 * @param {Element} node
284 *   Element.
285 * @returns {boolean}
286 *   Whether the opening tag can be omitted.
287 */function body(t){const e=f(t,-1,true);return!e||e.type!=="comment"&&!(e.type==="text"&&c(e.value.charAt(0)))&&!(e.type==="element"&&(e.tagName==="meta"||e.tagName==="link"||e.tagName==="script"||e.tagName==="style"||e.tagName==="template"))}
288/**
289 * Whether to omit `<colgroup>`.
290 * The spec describes some logic for the opening tag, but it’s easier to
291 * implement in the closing tag, to the same effect, so we handle it there
292 * instead.
293 *
294 * @param {Element} node
295 *   Element.
296 * @param {number | undefined} index
297 *   Index of element in parent.
298 * @param {Parents | undefined} parent
299 *   Parent of element.
300 * @returns {boolean}
301 *   Whether the opening tag can be omitted.
302 */function colgroup(t,e,n){const a=d(n,e);const o=f(t,-1,true);return!(n&&a&&a.type==="element"&&a.tagName==="colgroup"&&y(a,n.children.indexOf(a),n))&&Boolean(o&&o.type==="element"&&o.tagName==="col")}
303/**
304 * Whether to omit `<tbody>`.
305 *
306 * @param {Element} node
307 *   Element.
308 * @param {number | undefined} index
309 *   Index of element in parent.
310 * @param {Parents | undefined} parent
311 *   Parent of element.
312 * @returns {boolean}
313 *   Whether the opening tag can be omitted.
314 */function tbody(t,e,n){const a=d(n,e);const o=f(t,-1);return(!n||!a||a.type!=="element"||a.tagName!=="thead"&&a.tagName!=="tbody"||!y(a,n.children.indexOf(a),n))&&Boolean(o&&o.type==="element"&&o.tagName==="tr")}
315/**
316 * Maps of subsets.
317 *
318 * Each value is a matrix of tuples.
319 * The value at `0` causes parse errors, the value at `1` is valid.
320 * Of both, the value at `0` is unsafe, and the value at `1` is safe.
321 *
322 * @type {Record<'double' | 'name' | 'single' | 'unquoted', Array<[Array<string>, Array<string>]>>}
323 */const v={name:[["\t\n\f\r &/=>".split(""),"\t\n\f\r \"&'/=>
323`".split("")],["\0\t\n\f\r \"&'/<=>".split(""),"\0\t\n\f\r \"&'/<=>`".split("")]],unquoted:[["\t\n\f\r &>".split(""),"\0\t\n\f\r \"&'<=>`".split("")],["\0\t\n\f\r \"&'<=>`".split(""),"\0\t\n\f\r \"&'<=>`".split("")]],single:[["&'".split(""),"\"&'`".split("")],["\0&'".split(""),"\0\"&'`".split("")]],double:[['"&'.split(""),"\"&'`".split("")],['\0"&'.split(""),"\0\"&'`".split("")]]};
324/**
325 * Serialize an element node.
326 *
327 * @param {Element} node
328 *   Node to handle.
329 * @param {number | undefined} index
330 *   Index of `node` in `parent.
331 * @param {Parents | undefined} parent
332 *   Parent of `node`.
333 * @param {State} state
334 *   Info passed around about the current state.
335 * @returns {string}
336 *   Serialized node.
337 */function element(t,n,a,o){const s=o.schema;const r=s.space!=="svg"&&o.settings.omitOptionalTags;let i=s.space==="svg"?o.settings.closeEmptyElements:o.settings.voids.includes(t.tagName.toLowerCase());
338/** @type {Array<string>} */const l=[];
339/** @type {string} */let c;s.space==="html"&&t.tagName==="svg"&&(o.schema=e);const m=serializeAttributes(o,t.properties);const u=o.all(s.space==="html"&&t.tagName==="template"?t.content:t);o.schema=s;u&&(i=false);if(m||!r||!b(t,n,a)){l.push("<",t.tagName,m?" "+m:"");if(i&&(s.space==="svg"||o.settings.closeSelfClosing)){c=m.charAt(m.length-1);(!o.settings.tightSelfClosing||c==="/"||c&&c!=='"'&&c!=="'")&&l.push(" ");l.push("/")}l.push(">")}l.push(u);i||r&&y(t,n,a)||l.push("</"+t.tagName+">");return l.join("")}
340/**
341 * @param {State} state
342 * @param {Properties | null | undefined} properties
343 * @returns {string}
344 */function serializeAttributes(t,e){
345/** @type {Array<string>} */
346const n=[];let a=-1;
347/** @type {string} */let o;if(e)for(o in e)if(e[o]!==null&&e[o]!==void 0){const a=serializeAttribute(t,o,e[o]);a&&n.push(a)}while(++a<n.length){const e=t.settings.tightAttributes?n[a].charAt(n[a].length-1):void 0;a!==n.length-1&&e!=='"'&&e!=="'"&&(n[a]+=" ")}return n.join("")}
348/**
349 * @param {State} state
350 * @param {string} key
351 * @param {Properties[keyof Properties]} value
352 * @returns {string}
353 */function serializeAttribute(t,e,a){const o=n(t.schema,e);const c=t.settings.allowParseErrors&&t.schema.space==="html"?0:1;const m=t.settings.allowDangerousCharacters?0:1;let u=t.quote;
354/** @type {string | undefined} */let g;!o.overloadedBoolean||a!==o.attribute&&a!==""?!o.boolean&&!o.overloadedBoolean||typeof a==="string"&&a!==o.attribute&&a!==""||(a=Boolean(a)):a=true;if(a===null||a===void 0||a===false||typeof a==="number"&&Number.isNaN(a))return"";const f=s(o.attribute,Object.assign({},t.settings.characterReferences,{subset:v.name[c][m]}));if(a===true)return f;a=Array.isArray(a)?(o.commaSeparated?i:l)(a,{padLeft:!t.settings.tightCommaSeparatedLists}):String(a);if(t.settings.collapseEmptyAttributes&&!a)return f;t.settings.preferUnquoted&&(g=s(a,Object.assign({},t.settings.characterReferences,{attribute:true,subset:v.unquoted[c][m]})));if(g!==a){t.settings.quoteSmart&&r(a,u)>r(a,t.alternative)&&(u=t.alternative);g=u+s(a,Object.assign({},t.settings.characterReferences,{subset:(u==="'"?v.single:v.double)[c][m],attribute:true}))+u}return f+(g?"="+g:g)}const w=["<","&"];
355/**
356 * Serialize a text node.
357 *
358 * @param {Raw | Text} node
359 *   Node to handle.
360 * @param {number | undefined} _
361 *   Index of `node` in `parent.
362 * @param {Parents | undefined} parent
363 *   Parent of `node`.
364 * @param {State} state
365 *   Info passed around about the current state.
366 * @returns {string}
367 *   Serialized node.
368 */function text(t,e,n,a){return!n||n.type!=="element"||n.tagName!=="script"&&n.tagName!=="style"?s(t.value,Object.assign({},a.settings.characterReferences,{subset:w})):t.value}
369/**
370 * Serialize a raw node.
371 *
372 * @param {Raw} node
373 *   Node to handle.
374 * @param {number | undefined} index
375 *   Index of `node` in `parent.
376 * @param {Parents | undefined} parent
377 *   Parent of `node`.
378 * @param {State} state
379 *   Info passed around about the current state.
380 * @returns {string}
381 *   Serialized node.
382 */function raw(t,e,n,a){return a.settings.allowDangerousHtml?t.value:text(t,e,n,a)}
383/**
384 * Serialize a root.
385 *
386 * @param {Root} node
387 *   Node to handle.
388 * @param {number | undefined} _1
389 *   Index of `node` in `parent.
390 * @param {Parents | undefined} _2
391 *   Parent of `node`.
392 * @param {State} state
393 *   Info passed around about the current state.
394 * @returns {string}
395 *   Serialized node.
396 */function root(t,e,n,a){return a.all(t)}
397/**
398 * @type {(node: Nodes, index: number | undefined, parent: Parents | undefined, state: State) => string}
399 */const C=o("type",{invalid:invalid,unknown:unknown,handlers:{comment:comment,doctype:doctype,element:element,raw:raw,root:root,text:text}});
400/**
401 * Fail when a non-node is found in the tree.
402 *
403 * @param {unknown} node
404 *   Unknown value.
405 * @returns {never}
406 *   Never.
407 */function invalid(t){throw new Error("Expected node, not `"+t+"`")}
408/**
409 * Fail when a node with an unknown type is found in the tree.
410 *
411 * @param {unknown} node_
412 *  Unknown node.
413 * @returns {never}
414 *   Never.
415 */function unknown(t){const e=/** @type {Nodes} */t;throw new Error("Cannot compile unknown node `"+e.type+"`")}
416/** @type {Options} */const O={};
417/** @type {CharacterReferences} */const E={};
418/** @type {Array<never>} */const A=[];
419/**
420 * Serialize hast as HTML.
421 *
422 * @param {Array<RootContent> | Nodes} tree
423 *   Tree to serialize.
424 * @param {Options | null | undefined} [options]
425 *   Configuration (optional).
426 * @returns {string}
427 *   Serialized HTML.
428 */function toHtml(n,o){const s=o||O;const r=s.quote||'"';const i=r==='"'?"'":'"';if(r!=='"'&&r!=="'")throw new Error("Invalid quote `"+r+"`, expected `'` or `\"`");
429/** @type {State} */const l={one:one,all:all,settings:{omitOptionalTags:s.omitOptionalTags||false,allowParseErrors:s.allowParseErrors||false,allowDangerousCharacters:s.allowDangerousCharacters||false,quoteSmart:s.quoteSmart||false,preferUnquoted:s.preferUnquoted||false,tightAttributes:s.tightAttributes||false,upperDoctype:s.upperDoctype||false,tightDoctype:s.tightDoctype||false,bogusComments:s.bogusComments||false,tightCommaSeparatedLists:s.tightCommaSeparatedLists||false,tightSelfClosing:s.tightSelfClosing||false,collapseEmptyAttributes:s.collapseEmptyAttributes||false,allowDangerousHtml:s.allowDangerousHtml||false,voids:s.voids||t,characterReferences:s.characterReferences||E,closeSelfClosing:s.closeSelfClosing||false,closeEmptyElements:s.closeEmptyElements||false},schema:s.space==="svg"?e:a,quote:r,alternative:i};return l.one(Array.isArray(n)?{type:"root",children:n}:n,void 0,void 0)}
430/**
431 * Serialize a node.
432 *
433 * @this {State}
434 *   Info passed around about the current state.
435 * @param {Nodes} node
436 *   Node to handle.
437 * @param {number | undefined} index
438 *   Index of `node` in `parent.
439 * @param {Parents | undefined} parent
440 *   Parent of `node`.
441 * @returns {string}
442 *   Serialized node.
443 */function one(t,e,n){return C(t,e,n,this)}
444/**
445 * Serialize all children of `parent`.
446 *
447 * @this {State}
448 *   Info passed around about the current state.
449 * @param {Parents | undefined} parent
450 *   Parent whose children to serialize.
451 * @returns {string}
452 */function all(t){
453/** @type {Array<string>} */
454const e=[];const n=t&&t.children||A;let a=-1;while(++a<n.length)e[a]=this.one(n[a],a,t);return e.join("")}
455/**
456 * @typedef {import('./lib/index.js').CharacterReferences} CharacterReferences
457 * @typedef {import('./lib/index.js').Options} Options
458 * @typedef {import('./lib/index.js').Quote} Quote
459 * @typedef {import('./lib/index.js').Space} Space
460 */export{toHtml};
461

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.