@grafctory/mime-xml 0.0.0-stage → 0.5.1
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 +201 -0
- package/README.md +56 -2
- package/dist/denoise.core.d.ts +12 -0
- package/dist/denoise.core.d.ts.map +1 -0
- package/dist/denoise.core.js +78 -0
- package/dist/denoise.core.js.map +1 -0
- package/dist/denoise.d.ts +45 -0
- package/dist/denoise.d.ts.map +1 -0
- package/dist/denoise.js +63 -0
- package/dist/denoise.js.map +1 -0
- package/dist/focus.core.d.ts +9 -0
- package/dist/focus.core.d.ts.map +1 -0
- package/dist/focus.core.js +305 -0
- package/dist/focus.core.js.map +1 -0
- package/dist/focus.d.ts +64 -0
- package/dist/focus.d.ts.map +1 -0
- package/dist/focus.js +88 -0
- package/dist/focus.js.map +1 -0
- package/dist/html.core.d.ts +9 -0
- package/dist/html.core.d.ts.map +1 -0
- package/dist/html.core.js +229 -0
- package/dist/html.core.js.map +1 -0
- package/dist/html.d.ts +76 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +104 -0
- package/dist/html.js.map +1 -0
- package/dist/index.core.d.ts +74 -0
- package/dist/index.core.d.ts.map +1 -0
- package/dist/index.core.js +117 -0
- package/dist/index.core.js.map +1 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +116 -0
- package/dist/index.js.map +1 -0
- package/dist/untag.core.d.ts +9 -0
- package/dist/untag.core.d.ts.map +1 -0
- package/dist/untag.core.js +216 -0
- package/dist/untag.core.js.map +1 -0
- package/dist/untag.d.ts +86 -0
- package/dist/untag.d.ts.map +1 -0
- package/dist/untag.js +102 -0
- package/dist/untag.js.map +1 -0
- package/dist/unwrap.d.ts +37 -0
- package/dist/unwrap.d.ts.map +1 -0
- package/dist/unwrap.js +69 -0
- package/dist/unwrap.js.map +1 -0
- package/dist/xml.core.d.ts +9 -0
- package/dist/xml.core.d.ts.map +1 -0
- package/dist/xml.core.js +86 -0
- package/dist/xml.core.js.map +1 -0
- package/dist/xml.d.ts +69 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +97 -0
- package/dist/xml.js.map +1 -0
- package/dist/xpath.core.d.ts +100 -0
- package/dist/xpath.core.d.ts.map +1 -0
- package/dist/xpath.core.js +279 -0
- package/dist/xpath.core.js.map +1 -0
- package/dist/xpath.d.ts +128 -0
- package/dist/xpath.d.ts.map +1 -0
- package/dist/xpath.js +33 -0
- package/dist/xpath.js.map +1 -0
- package/package.json +62 -4
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { parseItem } from "@grafctory/http";
|
|
17
|
+
import { log } from "@grafctory/tape";
|
|
18
|
+
import { isTag } from "domhandler";
|
|
19
|
+
import { DomUtils, parseDocument } from "htmlparser2";
|
|
20
|
+
import { isBase } from "./index.core.js";
|
|
21
|
+
/**
|
|
22
|
+
* The media types an HTML document is served under.
|
|
23
|
+
*
|
|
24
|
+
* Matches the `text/html` HTML is served under and the `application/xhtml+xml` its XML serialisation is served under,
|
|
25
|
+
* both read by the same forgiving parser.
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-8.3 RFC 9110 § 8.3 - Content-Type}
|
|
28
|
+
*/
|
|
29
|
+
const HTMLType = /^(?:text\/html|application\/xhtml\+xml)$/i;
|
|
30
|
+
/**
|
|
31
|
+
* The charset declaration opening an HTML document.
|
|
32
|
+
*
|
|
33
|
+
* Matches the `charset` parameter of a `meta` element, stated either on its own or inside the `content` field of a
|
|
34
|
+
* `Content-Type` declaration, so that a document stating its own encoding is read under it.
|
|
35
|
+
*
|
|
36
|
+
* @see {@link https://html.spec.whatwg.org/multipage/semantics.html#charset WHATWG HTML - Specifying the document
|
|
37
|
+
* character encoding}
|
|
38
|
+
*/
|
|
39
|
+
const HTMLCharset = /<meta[^>]+\bcharset\s*=\s*["']?\s*([-\w]+)/i;
|
|
40
|
+
/**
|
|
41
|
+
* The number of opening bytes a charset declaration is looked for in.
|
|
42
|
+
*
|
|
43
|
+
* States the limit the HTML encoding sniffing algorithm sets, so that a declaration buried further down is ignored as
|
|
44
|
+
* browsers ignore it.
|
|
45
|
+
*
|
|
46
|
+
* @see {@link https://html.spec.whatwg.org/multipage/parsing.html#prescan-a-byte-stream-to-determine-its-encoding
|
|
47
|
+
* WHATWG HTML - Prescan a byte stream to determine its encoding}
|
|
48
|
+
*/
|
|
49
|
+
const HTMLPrescan = 1024;
|
|
50
|
+
/**
|
|
51
|
+
* The elements opening a foreign content subtree.
|
|
52
|
+
*
|
|
53
|
+
* @see {@link https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign WHATWG HTML - Parsing tokens
|
|
54
|
+
* in foreign content}
|
|
55
|
+
*/
|
|
56
|
+
const ForeignRoots = new Set(["svg", "math"]);
|
|
57
|
+
/**
|
|
58
|
+
* The elements holding HTML content inside a foreign content subtree.
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://html.spec.whatwg.org/multipage/parsing.html#html-integration-point WHATWG HTML - HTML integration
|
|
61
|
+
* point}
|
|
62
|
+
*/
|
|
63
|
+
const ForeignHosts = new Set(["foreignObject", "desc", "title", "annotation-xml", "mi", "mo", "mn", "ms", "mtext"]);
|
|
64
|
+
/**
|
|
65
|
+
* The camelCase attribute names SVG and MathML define, keyed by the folded form HTML parsing produces.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://html.spec.whatwg.org/multipage/parsing.html#adjust-svg-attributes WHATWG HTML - Adjust SVG
|
|
68
|
+
* attributes}
|
|
69
|
+
* @see {@link https://html.spec.whatwg.org/multipage/parsing.html#adjust-mathml-attributes WHATWG HTML - Adjust MathML
|
|
70
|
+
* attributes}
|
|
71
|
+
*/
|
|
72
|
+
const ForeignAttributes = new Map([
|
|
73
|
+
["attributename", "attributeName"],
|
|
74
|
+
["attributetype", "attributeType"],
|
|
75
|
+
["basefrequency", "baseFrequency"],
|
|
76
|
+
["baseprofile", "baseProfile"],
|
|
77
|
+
["calcmode", "calcMode"],
|
|
78
|
+
["clippathunits", "clipPathUnits"],
|
|
79
|
+
["definitionurl", "definitionURL"],
|
|
80
|
+
["diffuseconstant", "diffuseConstant"],
|
|
81
|
+
["edgemode", "edgeMode"],
|
|
82
|
+
["filterunits", "filterUnits"],
|
|
83
|
+
["glyphref", "glyphRef"],
|
|
84
|
+
["gradienttransform", "gradientTransform"],
|
|
85
|
+
["gradientunits", "gradientUnits"],
|
|
86
|
+
["kernelmatrix", "kernelMatrix"],
|
|
87
|
+
["kernelunitlength", "kernelUnitLength"],
|
|
88
|
+
["keypoints", "keyPoints"],
|
|
89
|
+
["keysplines", "keySplines"],
|
|
90
|
+
["keytimes", "keyTimes"],
|
|
91
|
+
["lengthadjust", "lengthAdjust"],
|
|
92
|
+
["limitingconeangle", "limitingConeAngle"],
|
|
93
|
+
["markerheight", "markerHeight"],
|
|
94
|
+
["markerunits", "markerUnits"],
|
|
95
|
+
["markerwidth", "markerWidth"],
|
|
96
|
+
["maskcontentunits", "maskContentUnits"],
|
|
97
|
+
["maskunits", "maskUnits"],
|
|
98
|
+
["numoctaves", "numOctaves"],
|
|
99
|
+
["pathlength", "pathLength"],
|
|
100
|
+
["patterncontentunits", "patternContentUnits"],
|
|
101
|
+
["patterntransform", "patternTransform"],
|
|
102
|
+
["patternunits", "patternUnits"],
|
|
103
|
+
["pointsatx", "pointsAtX"],
|
|
104
|
+
["pointsaty", "pointsAtY"],
|
|
105
|
+
["pointsatz", "pointsAtZ"],
|
|
106
|
+
["preservealpha", "preserveAlpha"],
|
|
107
|
+
["preserveaspectratio", "preserveAspectRatio"],
|
|
108
|
+
["primitiveunits", "primitiveUnits"],
|
|
109
|
+
["refx", "refX"],
|
|
110
|
+
["refy", "refY"],
|
|
111
|
+
["repeatcount", "repeatCount"],
|
|
112
|
+
["repeatdur", "repeatDur"],
|
|
113
|
+
["requiredextensions", "requiredExtensions"],
|
|
114
|
+
["requiredfeatures", "requiredFeatures"],
|
|
115
|
+
["specularconstant", "specularConstant"],
|
|
116
|
+
["specularexponent", "specularExponent"],
|
|
117
|
+
["spreadmethod", "spreadMethod"],
|
|
118
|
+
["startoffset", "startOffset"],
|
|
119
|
+
["stddeviation", "stdDeviation"],
|
|
120
|
+
["stitchtiles", "stitchTiles"],
|
|
121
|
+
["surfacescale", "surfaceScale"],
|
|
122
|
+
["systemlanguage", "systemLanguage"],
|
|
123
|
+
["tablevalues", "tableValues"],
|
|
124
|
+
["targetx", "targetX"],
|
|
125
|
+
["targety", "targetY"],
|
|
126
|
+
["textlength", "textLength"],
|
|
127
|
+
["viewbox", "viewBox"],
|
|
128
|
+
["viewtarget", "viewTarget"],
|
|
129
|
+
["xchannelselector", "xChannelSelector"],
|
|
130
|
+
["ychannelselector", "yChannelSelector"],
|
|
131
|
+
["zoomandpan", "zoomAndPan"]
|
|
132
|
+
]);
|
|
133
|
+
const logger = log(import.meta.url);
|
|
134
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
135
|
+
/**
|
|
136
|
+
* Parses an HTML document.
|
|
137
|
+
*
|
|
138
|
+
* Helper backing the `html()` task, which states the parsing contract.
|
|
139
|
+
*/
|
|
140
|
+
export async function process(document, base) {
|
|
141
|
+
const text = (document instanceof Response ? await read(document) : document).trim();
|
|
142
|
+
// a document holding nothing but whitespace, a byte order mark included, holds no content
|
|
143
|
+
return text ? rebase(adjust(parseDocument(text)), locate(document, base)) : undefined;
|
|
144
|
+
async function read(response) {
|
|
145
|
+
const [type, parameters] = parseItem(response.headers.get("Content-Type"));
|
|
146
|
+
if (type && !HTMLType.test(type)) {
|
|
147
|
+
logger.warn `unexpected <${type}> content type`;
|
|
148
|
+
}
|
|
149
|
+
const bytes = await response.arrayBuffer();
|
|
150
|
+
return convert(bytes, parameters.get("charset") || declared(bytes) || "UTF-8");
|
|
151
|
+
}
|
|
152
|
+
function convert(bytes, charset) {
|
|
153
|
+
const decoder = decode(charset);
|
|
154
|
+
if (decoder === undefined) {
|
|
155
|
+
logger.warn `unknown <${charset}> charset`;
|
|
156
|
+
}
|
|
157
|
+
return (decoder ?? new TextDecoder()).decode(bytes);
|
|
158
|
+
}
|
|
159
|
+
function declared(bytes) {
|
|
160
|
+
// read as windows-1252, so that the opening bytes are scanned whatever the document is actually encoded in:
|
|
161
|
+
// the charsets HTML is served under are ASCII compatible, so the declaration survives the mismatch
|
|
162
|
+
const prescan = new TextDecoder("windows-1252").decode(bytes.slice(0, HTMLPrescan));
|
|
163
|
+
return HTMLCharset.exec(prescan)?.[1];
|
|
164
|
+
}
|
|
165
|
+
function decode(charset) {
|
|
166
|
+
try {
|
|
167
|
+
return new TextDecoder(charset);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function adjust(document) {
|
|
174
|
+
// the parser restores the element names SVG and MathML define, but not their attribute names: the folded form
|
|
175
|
+
// is renamed inside foreign subtrees, leaving the HTML content hosted by an integration point untouched
|
|
176
|
+
restore(document.children, false);
|
|
177
|
+
return document;
|
|
178
|
+
}
|
|
179
|
+
function restore(nodes, foreign) {
|
|
180
|
+
nodes.filter(isTag).forEach(element => {
|
|
181
|
+
const own = foreign || ForeignRoots.has(element.name);
|
|
182
|
+
if (own) {
|
|
183
|
+
element.attribs = Object.fromEntries(Object.entries(element.attribs)
|
|
184
|
+
.map(([name, value]) => [ForeignAttributes.get(name) ?? name, value]));
|
|
185
|
+
}
|
|
186
|
+
// an integration point states its own name in the foreign language, but holds HTML content
|
|
187
|
+
restore(element.children, own && !ForeignHosts.has(element.name));
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
function locate(document, base) {
|
|
191
|
+
// a stated base is taken as it stands, so the retrieval URL never stands in as the one to resolve it against
|
|
192
|
+
if (base !== undefined) {
|
|
193
|
+
if (!isBase(base)) {
|
|
194
|
+
throw new RangeError(`expected resolvable base URL <${base}>`);
|
|
195
|
+
}
|
|
196
|
+
return new URL(base);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
return document instanceof Response && document.url ? new URL(document.url) : undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function rebase(document, base) {
|
|
203
|
+
const target = resolve(document, base);
|
|
204
|
+
if (target === undefined) {
|
|
205
|
+
return document;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
document.children.filter(isTag).forEach(root => {
|
|
209
|
+
root.attribs["xml:base"] = new URL(root.attribs["xml:base"] ?? "", target).href;
|
|
210
|
+
});
|
|
211
|
+
return document;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function resolve(document, base) {
|
|
215
|
+
const href = DomUtils.findOne(element => element.name === "base" && element.attribs["href"] !== undefined, document.children, true)?.attribs["href"];
|
|
216
|
+
// a base the document states is resolved against the retrieval URL, as the standard prescribes; one that
|
|
217
|
+
// cannot be resolved on its own, a relative reference in a document given as text among them, is left out
|
|
218
|
+
return href === undefined ? base : absolute(href, base) ?? base;
|
|
219
|
+
}
|
|
220
|
+
function absolute(href, base) {
|
|
221
|
+
try {
|
|
222
|
+
return new URL(href, base);
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
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YAEJ,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAE5B,CAAC;QAAC,MAAM,CAAC;YAER,OAAO,SAAS,CAAC;QAElB,CAAC;IAEF,CAAC;AAEF,CAAC"}
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { IRI } from "@grafctory/core/resource";
|
|
2
|
+
import type { Task } from "@grafctory/flow";
|
|
3
|
+
import type { Document } from "domhandler";
|
|
4
|
+
/**
|
|
5
|
+
* Creates an HTML parser.
|
|
6
|
+
*
|
|
7
|
+
* The task converts a feed of HTML documents into a feed of parsed trees, one tree per document, so that downstream
|
|
8
|
+
* tasks work on document structure rather than on text.
|
|
9
|
+
*
|
|
10
|
+
* Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
|
|
11
|
+
* produce no tree, and neither do responses without a body.
|
|
12
|
+
*
|
|
13
|
+
* Trees have the same shape as the ones produced by {@link xml}, so the same path expressions work on both. Names are
|
|
14
|
+
* kept as written in the source, `xmlns` declarations are not resolved, and missing `html`, `head` or `body` elements
|
|
15
|
+
* are not added.
|
|
16
|
+
*
|
|
17
|
+
* Response bodies are decoded with the `charset` parameter of their content type. If no charset is stated, the `meta`
|
|
18
|
+
* charset declared in the first kilobyte of the document is used. If neither is stated, bodies are decoded as UTF-8
|
|
19
|
+
* rather than as the windows-1252 legacy default of HTML. A leading byte order mark is stripped, both from text and
|
|
20
|
+
* from bodies in a Unicode charset.
|
|
21
|
+
*
|
|
22
|
+
* A response with a content type other than `text/html` or `application/xhtml+xml` is logged as a warning and parsed
|
|
23
|
+
* anyway. A response with a charset the platform can't decode is also logged, and its body is decoded as UTF-8. Since
|
|
24
|
+
* parsing never fails, these warnings are the only sign that a source is mis-declared.
|
|
25
|
+
*
|
|
26
|
+
* Trees carry the base URL needed to resolve the references they contain, recorded as an `xml:base` attribute on each
|
|
27
|
+
* root element. A root element that already declares `xml:base` keeps it, resolved against the recorded base URL.
|
|
28
|
+
*
|
|
29
|
+
* The base URL is the one stated by the first `base` element of the document, resolved against the retrieval URL. The
|
|
30
|
+
* retrieval URL is the `base` argument, if given, or else the final URL of the response, after any redirects. No base
|
|
31
|
+
* URL is recorded if none of them yields an absolute URL, as for a document given as text with only a relative `base`
|
|
32
|
+
* element. Unlike the `base` argument, `base` elements are read leniently, since they come from the source.
|
|
33
|
+
*
|
|
34
|
+
* > [!NOTE]
|
|
35
|
+
* >
|
|
36
|
+
* > - **Incremental**: each tree is emitted as soon as its document is parsed, so endless sources are processed for as
|
|
37
|
+
* > long as the feed is consumed.
|
|
38
|
+
* > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
|
|
39
|
+
* > twice the size of the largest document, regardless of the length of the feed.
|
|
40
|
+
* > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
|
|
41
|
+
* > across nested feeds or runs.
|
|
42
|
+
*
|
|
43
|
+
* > [!WARNING]
|
|
44
|
+
* >
|
|
45
|
+
* > Parsing is forgiving and never fails. Emitted trees are always structurally sound, since unclosed elements are
|
|
46
|
+
* > closed at the end of the input, but malformed input may be misrepresented rather than rejected. Trees also reflect
|
|
47
|
+
* > the markup as written rather than as a browser would repair it: misnested elements stay misnested, and content a
|
|
48
|
+
* > browser would relocate stays in place. Expressions should target the tree the parser actually produces.
|
|
49
|
+
*
|
|
50
|
+
* > [!NOTE]
|
|
51
|
+
* >
|
|
52
|
+
* > Names are folded to lowercase, as HTML prescribes, except inside inline SVG and MathML, where the camelCase names
|
|
53
|
+
* > defined by those languages are restored: `clipPath` and `@viewBox` are selected as written. HTML content inside
|
|
54
|
+
* > `foreignObject` and MathML text elements is folded like the rest of the document.
|
|
55
|
+
*
|
|
56
|
+
* @param base The retrieval URL for resolving references, overriding the URL of responses and in turn overridden by
|
|
57
|
+
* `base` elements in the document; must be a hierarchical identifier, that is a scheme followed by a
|
|
58
|
+
* root-relative path
|
|
59
|
+
*
|
|
60
|
+
* @returns A task converting a feed of HTML documents, given as text or as responses, into a feed of parsed trees
|
|
61
|
+
*
|
|
62
|
+
* @throws {@link !RangeError RangeError} If `base` is not a hierarchical identifier, for instance a relative reference
|
|
63
|
+
* or an opaque identifier such as `urn:example:x`, which would leave references
|
|
64
|
+
* silently unresolved
|
|
65
|
+
*
|
|
66
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
|
|
67
|
+
* read
|
|
68
|
+
*
|
|
69
|
+
* @see {@link https://html.spec.whatwg.org/multipage/ WHATWG HTML Living Standard}
|
|
70
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-8.3 RFC 9110 § 8.3 - Content-Type}
|
|
71
|
+
* @see {@link https://www.w3.org/TR/xmlbase/ XML Base}
|
|
72
|
+
*
|
|
73
|
+
* @group Factories
|
|
74
|
+
*/
|
|
75
|
+
export declare function html(base?: IRI): Task<string | Response, Document>;
|
|
76
|
+
//# sourceMappingURL=html.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAO3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,wBAAgB,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,MAAM,GAAG,QAAQ,EAAE,QAAQ,CAAC,CAsBlE"}
|
package/dist/html.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { items } from "@grafctory/flow/feeds";
|
|
17
|
+
import { process } from "./html.core.js";
|
|
18
|
+
import { isBase } from "./index.core.js";
|
|
19
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
20
|
+
/**
|
|
21
|
+
* Creates an HTML parser.
|
|
22
|
+
*
|
|
23
|
+
* The task converts a feed of HTML documents into a feed of parsed trees, one tree per document, so that downstream
|
|
24
|
+
* tasks work on document structure rather than on text.
|
|
25
|
+
*
|
|
26
|
+
* Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
|
|
27
|
+
* produce no tree, and neither do responses without a body.
|
|
28
|
+
*
|
|
29
|
+
* Trees have the same shape as the ones produced by {@link xml}, so the same path expressions work on both. Names are
|
|
30
|
+
* kept as written in the source, `xmlns` declarations are not resolved, and missing `html`, `head` or `body` elements
|
|
31
|
+
* are not added.
|
|
32
|
+
*
|
|
33
|
+
* Response bodies are decoded with the `charset` parameter of their content type. If no charset is stated, the `meta`
|
|
34
|
+
* charset declared in the first kilobyte of the document is used. If neither is stated, bodies are decoded as UTF-8
|
|
35
|
+
* rather than as the windows-1252 legacy default of HTML. A leading byte order mark is stripped, both from text and
|
|
36
|
+
* from bodies in a Unicode charset.
|
|
37
|
+
*
|
|
38
|
+
* A response with a content type other than `text/html` or `application/xhtml+xml` is logged as a warning and parsed
|
|
39
|
+
* anyway. A response with a charset the platform can't decode is also logged, and its body is decoded as UTF-8. Since
|
|
40
|
+
* parsing never fails, these warnings are the only sign that a source is mis-declared.
|
|
41
|
+
*
|
|
42
|
+
* Trees carry the base URL needed to resolve the references they contain, recorded as an `xml:base` attribute on each
|
|
43
|
+
* root element. A root element that already declares `xml:base` keeps it, resolved against the recorded base URL.
|
|
44
|
+
*
|
|
45
|
+
* The base URL is the one stated by the first `base` element of the document, resolved against the retrieval URL. The
|
|
46
|
+
* retrieval URL is the `base` argument, if given, or else the final URL of the response, after any redirects. No base
|
|
47
|
+
* URL is recorded if none of them yields an absolute URL, as for a document given as text with only a relative `base`
|
|
48
|
+
* element. Unlike the `base` argument, `base` elements are read leniently, since they come from the source.
|
|
49
|
+
*
|
|
50
|
+
* > [!NOTE]
|
|
51
|
+
* >
|
|
52
|
+
* > - **Incremental**: each tree is emitted as soon as its document is parsed, so endless sources are processed for as
|
|
53
|
+
* > long as the feed is consumed.
|
|
54
|
+
* > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
|
|
55
|
+
* > twice the size of the largest document, regardless of the length of the feed.
|
|
56
|
+
* > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
|
|
57
|
+
* > across nested feeds or runs.
|
|
58
|
+
*
|
|
59
|
+
* > [!WARNING]
|
|
60
|
+
* >
|
|
61
|
+
* > Parsing is forgiving and never fails. Emitted trees are always structurally sound, since unclosed elements are
|
|
62
|
+
* > closed at the end of the input, but malformed input may be misrepresented rather than rejected. Trees also reflect
|
|
63
|
+
* > the markup as written rather than as a browser would repair it: misnested elements stay misnested, and content a
|
|
64
|
+
* > browser would relocate stays in place. Expressions should target the tree the parser actually produces.
|
|
65
|
+
*
|
|
66
|
+
* > [!NOTE]
|
|
67
|
+
* >
|
|
68
|
+
* > Names are folded to lowercase, as HTML prescribes, except inside inline SVG and MathML, where the camelCase names
|
|
69
|
+
* > defined by those languages are restored: `clipPath` and `@viewBox` are selected as written. HTML content inside
|
|
70
|
+
* > `foreignObject` and MathML text elements is folded like the rest of the document.
|
|
71
|
+
*
|
|
72
|
+
* @param base The retrieval URL for resolving references, overriding the URL of responses and in turn overridden by
|
|
73
|
+
* `base` elements in the document; must be a hierarchical identifier, that is a scheme followed by a
|
|
74
|
+
* root-relative path
|
|
75
|
+
*
|
|
76
|
+
* @returns A task converting a feed of HTML documents, given as text or as responses, into a feed of parsed trees
|
|
77
|
+
*
|
|
78
|
+
* @throws {@link !RangeError RangeError} If `base` is not a hierarchical identifier, for instance a relative reference
|
|
79
|
+
* or an opaque identifier such as `urn:example:x`, which would leave references
|
|
80
|
+
* silently unresolved
|
|
81
|
+
*
|
|
82
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
|
|
83
|
+
* read
|
|
84
|
+
*
|
|
85
|
+
* @see {@link https://html.spec.whatwg.org/multipage/ WHATWG HTML Living Standard}
|
|
86
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-8.3 RFC 9110 § 8.3 - Content-Type}
|
|
87
|
+
* @see {@link https://www.w3.org/TR/xmlbase/ XML Base}
|
|
88
|
+
*
|
|
89
|
+
* @group Factories
|
|
90
|
+
*/
|
|
91
|
+
export function html(base) {
|
|
92
|
+
if (base !== undefined && !isBase(base)) {
|
|
93
|
+
throw new RangeError(`expected resolvable base URL <${base}>`);
|
|
94
|
+
}
|
|
95
|
+
return documents => items((async function* () {
|
|
96
|
+
for await (const document of documents) {
|
|
97
|
+
const tree = await process(document, base);
|
|
98
|
+
if (tree !== undefined) { // a document holding no text contributes no value
|
|
99
|
+
yield tree;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
})());
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=html.js.map
|
package/dist/html.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGzC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,MAAM,UAAU,IAAI,CAAC,IAAU;IAE9B,IAAK,IAAI,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAG,CAAC;QAC3C,MAAM,IAAI,UAAU,CAAC,iCAAiC,IAAI,GAAG,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,SAAS,CAAC;QAEzC,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YAExC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;YAE3C,IAAK,IAAI,KAAK,SAAS,EAAG,CAAC,CAAC,kDAAkD;gBAE7E,MAAM,IAAI,CAAC;YAEZ,CAAC;QAEF,CAAC;IAEF,CAAC,CAAC,EAAE,CAAC,CAAC;AAEP,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type IRI } from "@grafctory/core/resource";
|
|
2
|
+
import type { AnyNode, Element, NodeWithChildren } from "domhandler";
|
|
3
|
+
/**
|
|
4
|
+
* The whitespace runs collapsed to a single space when character data is read.
|
|
5
|
+
*
|
|
6
|
+
* Matches the control characters markup is laid out with, line feeds and tabs among them, alongside the spaces a page
|
|
7
|
+
* is typeset with, the no-break space and the wider separators among them, so that neither the layout of the source
|
|
8
|
+
* nor the typesetting of the page weighs on the text. The zero-width space and the joiners stand for no space at all
|
|
9
|
+
* and are left as they are; the byte order mark is folded like the rest, as the standard counts it among the spaces.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-white-space ECMA-262 -
|
|
12
|
+
* White Space}
|
|
13
|
+
*/
|
|
14
|
+
export declare const Space: RegExp;
|
|
15
|
+
/**
|
|
16
|
+
* The elements carrying no text a reader is after.
|
|
17
|
+
*
|
|
18
|
+
* Lists the markup a page is framed by, navigation, headers, footers and sidebars among them, alongside the scripts,
|
|
19
|
+
* styles, controls and embedded objects a reader never reads, so that whatever they hold weighs neither on the text a
|
|
20
|
+
* page is scored on nor on the title it is taken to state.
|
|
21
|
+
*/
|
|
22
|
+
export declare const Ignored: Set<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a reference states a base URL relative references resolve against.
|
|
25
|
+
*
|
|
26
|
+
* Accepts the `"hierarchical"` identifiers RFC 3986 defines reference resolution for, that is a scheme followed by a
|
|
27
|
+
* root-relative path, so that a reference a tree carries resolves by the standard rules. A relative reference states
|
|
28
|
+
* no base of its own, and an opaque one, `urn:example:x` among them, states nothing to resolve a reference against.
|
|
29
|
+
*
|
|
30
|
+
* @param value The value to check
|
|
31
|
+
*
|
|
32
|
+
* @returns `true` if `value` is an IRI a relative reference resolves against; `false` otherwise
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3986#section-5.2.3 RFC 3986 § 5.2.3 - Merge Paths}
|
|
35
|
+
*/
|
|
36
|
+
export declare function isBase(value: unknown): value is IRI;
|
|
37
|
+
/**
|
|
38
|
+
* Finds the title of a page.
|
|
39
|
+
*
|
|
40
|
+
* @param node The root of the tree to search; only its descendants are considered, and the framing a reader is not
|
|
41
|
+
* after is left out, so that the caption of an embedded object is not mistaken for the title
|
|
42
|
+
*
|
|
43
|
+
* @returns The first title stated by the tree rooted at `node`; `undefined` if none is stated or the first one draws
|
|
44
|
+
* no text of its own, whatever framing it wraps, so that no empty heading is handed back
|
|
45
|
+
*/
|
|
46
|
+
export declare function title(node: AnyNode): undefined | Element;
|
|
47
|
+
/**
|
|
48
|
+
* Reads the name of an element as the tree carries it.
|
|
49
|
+
*
|
|
50
|
+
* @param element The element to name
|
|
51
|
+
*
|
|
52
|
+
* @returns The name of `element`, folded to lower case, so that names are matched case insensitively
|
|
53
|
+
*/
|
|
54
|
+
export declare function name(element: Element): string;
|
|
55
|
+
/**
|
|
56
|
+
* Reads the text of a node as the tree carries it.
|
|
57
|
+
*
|
|
58
|
+
* @param node The node to read; only its descendants are matched against `excluded`, so that the text of an
|
|
59
|
+
* excluded element is read where it is passed in directly
|
|
60
|
+
* @param excluded The lowercase names of the elements whose text is left out, however deeply they sit inside the
|
|
61
|
+
* content, so that the framing, the controls and the embedded objects of a page leave no text behind
|
|
62
|
+
*
|
|
63
|
+
* @returns The text held by the descendants of `node`, with whitespace left as the markup lays it out
|
|
64
|
+
*/
|
|
65
|
+
export declare function text(node: NodeWithChildren, excluded: ReadonlySet<string>): string;
|
|
66
|
+
/**
|
|
67
|
+
* Collapses the whitespace character data is laid out with.
|
|
68
|
+
*
|
|
69
|
+
* @param text The text to collapse
|
|
70
|
+
*
|
|
71
|
+
* @returns `text` with every run of whitespace replaced by a single space
|
|
72
|
+
*/
|
|
73
|
+
export declare function normalize(text: string): string;
|
|
74
|
+
//# sourceMappingURL=index.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.core.d.ts","sourceRoot":"","sources":["../src/index.core.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,KAAK,GAAG,EAAS,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAIrE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,KAAK,QAAmF,CAAC;AAEtG;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,aASlB,CAAC;AAKH;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAEnD;AAGD;;;;;;;;GAQG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,OAAO,CAcxD;AAED;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAE7C;AAED;;;;;;;;;GASG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,gBAAgB,EAAE,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,CAMlF;AAGD;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { isIRI } from "@grafctory/core/resource";
|
|
17
|
+
import { hasChildren, isTag, isText } from "domhandler";
|
|
18
|
+
/**
|
|
19
|
+
* The whitespace runs collapsed to a single space when character data is read.
|
|
20
|
+
*
|
|
21
|
+
* Matches the control characters markup is laid out with, line feeds and tabs among them, alongside the spaces a page
|
|
22
|
+
* is typeset with, the no-break space and the wider separators among them, so that neither the layout of the source
|
|
23
|
+
* nor the typesetting of the page weighs on the text. The zero-width space and the joiners stand for no space at all
|
|
24
|
+
* and are left as they are; the byte order mark is folded like the rest, as the standard counts it among the spaces.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-white-space ECMA-262 -
|
|
27
|
+
* White Space}
|
|
28
|
+
*/
|
|
29
|
+
export const Space = /[\x00-\x20\x7F\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+/g;
|
|
30
|
+
/**
|
|
31
|
+
* The elements carrying no text a reader is after.
|
|
32
|
+
*
|
|
33
|
+
* Lists the markup a page is framed by, navigation, headers, footers and sidebars among them, alongside the scripts,
|
|
34
|
+
* styles, controls and embedded objects a reader never reads, so that whatever they hold weighs neither on the text a
|
|
35
|
+
* page is scored on nor on the title it is taken to state.
|
|
36
|
+
*/
|
|
37
|
+
export const Ignored = new Set([
|
|
38
|
+
"style", "script", "noscript",
|
|
39
|
+
"nav", "header", "footer", "aside",
|
|
40
|
+
"menu", "menuitem", "toolbar",
|
|
41
|
+
"iframe", "embed", "object", "applet",
|
|
42
|
+
"form", "input", "button", "select", "textarea", "label", "fieldset", "legend",
|
|
43
|
+
"canvas", "svg", "audio", "video", "track", "source"
|
|
44
|
+
]);
|
|
45
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a reference states a base URL relative references resolve against.
|
|
48
|
+
*
|
|
49
|
+
* Accepts the `"hierarchical"` identifiers RFC 3986 defines reference resolution for, that is a scheme followed by a
|
|
50
|
+
* root-relative path, so that a reference a tree carries resolves by the standard rules. A relative reference states
|
|
51
|
+
* no base of its own, and an opaque one, `urn:example:x` among them, states nothing to resolve a reference against.
|
|
52
|
+
*
|
|
53
|
+
* @param value The value to check
|
|
54
|
+
*
|
|
55
|
+
* @returns `true` if `value` is an IRI a relative reference resolves against; `false` otherwise
|
|
56
|
+
*
|
|
57
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3986#section-5.2.3 RFC 3986 § 5.2.3 - Merge Paths}
|
|
58
|
+
*/
|
|
59
|
+
export function isBase(value) {
|
|
60
|
+
return isIRI(value, "hierarchical");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Finds the title of a page.
|
|
64
|
+
*
|
|
65
|
+
* @param node The root of the tree to search; only its descendants are considered, and the framing a reader is not
|
|
66
|
+
* after is left out, so that the caption of an embedded object is not mistaken for the title
|
|
67
|
+
*
|
|
68
|
+
* @returns The first title stated by the tree rooted at `node`; `undefined` if none is stated or the first one draws
|
|
69
|
+
* no text of its own, whatever framing it wraps, so that no empty heading is handed back
|
|
70
|
+
*/
|
|
71
|
+
export function title(node) {
|
|
72
|
+
const [heading] = stated(hasChildren(node) ? node.children : []);
|
|
73
|
+
return heading !== undefined && text(heading, Ignored).trim() !== "" ? heading : undefined;
|
|
74
|
+
function stated(nodes) {
|
|
75
|
+
return nodes.filter(isTag).flatMap(element => name(element) === "title" ? [element] // states its own
|
|
76
|
+
: Ignored.has(name(element)) ? [] // framing states captions a reader is not after
|
|
77
|
+
: stated(element.children));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Reads the name of an element as the tree carries it.
|
|
82
|
+
*
|
|
83
|
+
* @param element The element to name
|
|
84
|
+
*
|
|
85
|
+
* @returns The name of `element`, folded to lower case, so that names are matched case insensitively
|
|
86
|
+
*/
|
|
87
|
+
export function name(element) {
|
|
88
|
+
return element.name.toLowerCase();
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads the text of a node as the tree carries it.
|
|
92
|
+
*
|
|
93
|
+
* @param node The node to read; only its descendants are matched against `excluded`, so that the text of an
|
|
94
|
+
* excluded element is read where it is passed in directly
|
|
95
|
+
* @param excluded The lowercase names of the elements whose text is left out, however deeply they sit inside the
|
|
96
|
+
* content, so that the framing, the controls and the embedded objects of a page leave no text behind
|
|
97
|
+
*
|
|
98
|
+
* @returns The text held by the descendants of `node`, with whitespace left as the markup lays it out
|
|
99
|
+
*/
|
|
100
|
+
export function text(node, excluded) {
|
|
101
|
+
return node.children.map(child => isText(child) ? child.data
|
|
102
|
+
: isTag(child) && excluded.has(name(child)) ? ""
|
|
103
|
+
: hasChildren(child) ? text(child, excluded)
|
|
104
|
+
: "" // comments, doctypes and processing instructions carry no text
|
|
105
|
+
).join("");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Collapses the whitespace character data is laid out with.
|
|
109
|
+
*
|
|
110
|
+
* @param text The text to collapse
|
|
111
|
+
*
|
|
112
|
+
* @returns `text` with every run of whitespace replaced by a single space
|
|
113
|
+
*/
|
|
114
|
+
export function normalize(text) {
|
|
115
|
+
return text.replace(Space, " ");
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=index.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.core.js","sourceRoot":"","sources":["../src/index.core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAY,KAAK,EAAE,MAAM,0BAA0B,CAAC;AAE3D,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAGxD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,gFAAgF,CAAC;AAEtG;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC;IAE9B,OAAO,EAAE,QAAQ,EAAE,UAAU;IAC7B,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO;IAClC,MAAM,EAAE,UAAU,EAAE,SAAS;IAC7B,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ;IACrC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ;IAC9E,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ;CAEpD,CAAC,CAAC;AAGH,wHAAwH;AAExH;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CAAC,KAAc;IACpC,OA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