typeclaw 0.48.3 → 0.48.5
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/package.json +2 -2
- package/scripts/dump-system-prompt.ts +146 -11
- package/src/agent/index.ts +6 -1
- package/src/agent/live-subagents.ts +15 -0
- package/src/agent/plugin-tools.ts +7 -0
- package/src/agent/session-origin.ts +104 -16
- package/src/agent/subagent-completion-reminder.ts +1 -1
- package/src/agent/subagent-drain.ts +11 -1
- package/src/agent/subagents.ts +52 -7
- package/src/agent/tool-file-safety.ts +14 -4
- package/src/agent/tools/runtime-notice.ts +29 -0
- package/src/agent/tools/spawn-subagent.ts +88 -7
- package/src/agent/tools/subagent-cancel.ts +1 -0
- package/src/agent/tools/subagent-output.ts +7 -2
- package/src/agent/tools/webfetch/adaptive-html.ts +319 -0
- package/src/agent/tools/webfetch/html-lexical.ts +841 -0
- package/src/agent/tools/webfetch/inline-style-visibility.ts +281 -0
- package/src/agent/tools/webfetch/strategies/readability.ts +306 -5
- package/src/agent/tools/webfetch/tool.ts +45 -2
- package/src/agent/tools/webfetch/types.ts +7 -0
- package/src/agent/turn-runner.ts +7 -3
- package/src/bundled-plugins/github-cli-auth/gh-command.ts +187 -8
- package/src/bundled-plugins/github-cli-auth/git-command.ts +497 -652
- package/src/bundled-plugins/memory/load-memory.ts +23 -27
- package/src/bundled-plugins/reviewer/reviewer.ts +32 -5
- package/src/bundled-plugins/reviewer/skills/code-review.ts +5 -3
- package/src/bundled-plugins/reviewer/skills/doc-review.ts +1 -1
- package/src/bundled-plugins/reviewer/skills/plan-review.ts +1 -1
- package/src/bundled-plugins/security/index.ts +1 -0
- package/src/bundled-plugins/security/policies/private-surface-read.ts +74 -18
- package/src/channels/router.ts +32 -47
- package/src/cli/hostd.ts +1 -1
- package/src/hostd/kakao-renewal-manager.ts +23 -11
- package/src/plugin/index.ts +1 -0
- package/src/plugin/types.ts +9 -10
- package/src/run/channel-session-factory.ts +3 -1
- package/src/run/index.ts +29 -29
- package/src/run/subagent-coalescing.ts +24 -0
- package/src/secrets/kakao-renewal.ts +123 -20
- package/src/server/index.ts +4 -1
- package/src/skills/typeclaw-channel-github/SKILL.md +6 -6
- package/src/skills/typeclaw-channel-instagram/SKILL.md +1 -1
- package/src/skills/typeclaw-channel-kakaotalk/SKILL.md +1 -1
- package/src/skills/typeclaw-channel-line/SKILL.md +1 -1
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
import { classifyRawInlineStyleVisibility } from './inline-style-visibility'
|
|
2
|
+
|
|
3
|
+
const VOID_ELEMENTS = new Set([
|
|
4
|
+
'area',
|
|
5
|
+
'base',
|
|
6
|
+
'br',
|
|
7
|
+
'col',
|
|
8
|
+
'embed',
|
|
9
|
+
'hr',
|
|
10
|
+
'img',
|
|
11
|
+
'input',
|
|
12
|
+
'link',
|
|
13
|
+
'meta',
|
|
14
|
+
'param',
|
|
15
|
+
'source',
|
|
16
|
+
'track',
|
|
17
|
+
'wbr',
|
|
18
|
+
])
|
|
19
|
+
const OPTIONAL_SAME_TAG_CLOSE = new Set([
|
|
20
|
+
'li',
|
|
21
|
+
'dt',
|
|
22
|
+
'dd',
|
|
23
|
+
'p',
|
|
24
|
+
'rt',
|
|
25
|
+
'rp',
|
|
26
|
+
'optgroup',
|
|
27
|
+
'option',
|
|
28
|
+
'thead',
|
|
29
|
+
'tbody',
|
|
30
|
+
'tfoot',
|
|
31
|
+
'tr',
|
|
32
|
+
'th',
|
|
33
|
+
'td',
|
|
34
|
+
'colgroup',
|
|
35
|
+
])
|
|
36
|
+
const RAW_TEXT_ELEMENTS = new Set(['script', 'style', 'textarea', 'title', 'xmp', 'iframe', 'noembed', 'noframes'])
|
|
37
|
+
// Inert template content and noscript fallback markup are not public document metadata sources.
|
|
38
|
+
const METADATA_SUPPRESSING_ELEMENTS = new Set(['template', 'noscript'])
|
|
39
|
+
// HTML integration points inside SVG/MathML are deliberately still suppressed: faithfully
|
|
40
|
+
// switching tokenizer namespaces is unnecessary risk for metadata-only extraction.
|
|
41
|
+
const FOREIGN_ROOT_ELEMENTS = new Set(['svg', 'math'])
|
|
42
|
+
const MAX_DOCTYPE_CHARS = 4_096
|
|
43
|
+
const MAX_XML_DECLARATION_CHARS = 1_024
|
|
44
|
+
const MAX_BOGUS_COMMENT_CHARS = 64 * 1024
|
|
45
|
+
|
|
46
|
+
type HtmlStartTag = {
|
|
47
|
+
name: string
|
|
48
|
+
end: number
|
|
49
|
+
typeAttribute: string | null
|
|
50
|
+
metadataSuppressing: boolean
|
|
51
|
+
selfClosingSyntax: boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type HtmlRawElement = {
|
|
55
|
+
name: string
|
|
56
|
+
typeAttribute: string | null
|
|
57
|
+
bodyStart: number
|
|
58
|
+
bodyEnd: number
|
|
59
|
+
metadataSuppressed: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type HtmlLexicalWalkOptions = {
|
|
63
|
+
maxTags?: number
|
|
64
|
+
maxDepth?: number
|
|
65
|
+
maxAttributesPerTag?: number
|
|
66
|
+
maxStartTagChars?: number
|
|
67
|
+
allowIncompleteTail?: boolean
|
|
68
|
+
onStartTag?: (tag: { name: string; end: number }) => void
|
|
69
|
+
onRawElement?: (element: HtmlRawElement) => void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type HtmlLexicalWalkResult = { valid: true } | { valid: false; failure: 'limit' | 'malformed' }
|
|
73
|
+
|
|
74
|
+
type ParseResult<T> =
|
|
75
|
+
| { kind: 'ok'; value: T }
|
|
76
|
+
| { kind: 'text' }
|
|
77
|
+
| { kind: 'incomplete' }
|
|
78
|
+
| { kind: 'invalid' }
|
|
79
|
+
| { kind: 'limit' }
|
|
80
|
+
|
|
81
|
+
type StackEntry = {
|
|
82
|
+
name: string
|
|
83
|
+
metadataSuppressing: boolean
|
|
84
|
+
foreignRoot: boolean
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function walkHtmlLexically(html: string, options: HtmlLexicalWalkOptions = {}): HtmlLexicalWalkResult {
|
|
88
|
+
const stack: StackEntry[] = []
|
|
89
|
+
let metadataSuppressionDepth = 0
|
|
90
|
+
let foreignNamespaceDepth = 0
|
|
91
|
+
let tagCount = 0
|
|
92
|
+
let cursor = 0
|
|
93
|
+
let xmlDeclarationAllowed = true
|
|
94
|
+
|
|
95
|
+
const recordTag = (): boolean => {
|
|
96
|
+
tagCount++
|
|
97
|
+
return options.maxTags === undefined || tagCount <= options.maxTags
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const popStack = (): StackEntry | undefined => {
|
|
101
|
+
const closed = stack.pop()
|
|
102
|
+
if (closed?.metadataSuppressing) metadataSuppressionDepth--
|
|
103
|
+
if (closed?.foreignRoot) foreignNamespaceDepth--
|
|
104
|
+
return closed
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const popThrough = (index: number): void => {
|
|
108
|
+
while (stack.length > index) popStack()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const closeOptionalElementsForStart = (name: string): void => {
|
|
112
|
+
const current = stack.at(-1)?.name
|
|
113
|
+
if ((name === 'dt' || name === 'dd') && (current === 'dt' || current === 'dd')) {
|
|
114
|
+
popStack()
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
if (name === 'tr') {
|
|
118
|
+
const row = findOpenTableEntry(stack, new Set(['tr']), new Set(['table', 'thead', 'tbody', 'tfoot']))
|
|
119
|
+
if (row >= 0) popThrough(row)
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
if (name === 'thead' || name === 'tbody' || name === 'tfoot') {
|
|
123
|
+
const priorTableContent = findOpenTableEntry(stack, new Set(['thead', 'tbody', 'tfoot']), new Set(['table']))
|
|
124
|
+
if (priorTableContent >= 0) popThrough(priorTableContent)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
if (name === 'td' || name === 'th') {
|
|
128
|
+
const cell = findOpenTableEntry(stack, new Set(['td', 'th']), new Set(['tr', 'table']))
|
|
129
|
+
if (cell >= 0) popThrough(cell)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
if (OPTIONAL_SAME_TAG_CLOSE.has(name) && current === name) popStack()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const closeTableEndTag = (name: string): boolean => {
|
|
136
|
+
const target = findTableEndTarget(stack, name)
|
|
137
|
+
if (target < 0) return false
|
|
138
|
+
popThrough(target)
|
|
139
|
+
return true
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
while (cursor < html.length) {
|
|
143
|
+
const open = html.indexOf('<', cursor)
|
|
144
|
+
if (open < 0) break
|
|
145
|
+
if (xmlDeclarationAllowed && !isIgnorablePrologText(html.slice(cursor, open))) xmlDeclarationAllowed = false
|
|
146
|
+
|
|
147
|
+
if (html.startsWith('<!--', open)) {
|
|
148
|
+
const comment = parseComment(html, open)
|
|
149
|
+
if (comment.kind === 'ok') {
|
|
150
|
+
xmlDeclarationAllowed = false
|
|
151
|
+
cursor = comment.value
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
if (comment.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
155
|
+
return malformed()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (html[open + 1] === '!') {
|
|
159
|
+
const declaration = parseDoctype(html, open)
|
|
160
|
+
if (declaration.kind === 'ok') {
|
|
161
|
+
xmlDeclarationAllowed = false
|
|
162
|
+
cursor = declaration.value
|
|
163
|
+
continue
|
|
164
|
+
}
|
|
165
|
+
if (declaration.kind === 'invalid') {
|
|
166
|
+
const comment = parseBogusComment(html, open)
|
|
167
|
+
if (comment.kind === 'ok') {
|
|
168
|
+
xmlDeclarationAllowed = false
|
|
169
|
+
cursor = comment.value
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
if (comment.kind === 'limit') return limitExceeded()
|
|
173
|
+
if (comment.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
174
|
+
}
|
|
175
|
+
if (declaration.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
176
|
+
return malformed()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (html[open + 1] === '?') {
|
|
180
|
+
if (xmlDeclarationAllowed && html.startsWith('<?xml', open)) {
|
|
181
|
+
const declaration = parseXmlDeclaration(html, open)
|
|
182
|
+
if (declaration.kind === 'ok') {
|
|
183
|
+
xmlDeclarationAllowed = false
|
|
184
|
+
cursor = declaration.value
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const comment = parseBogusComment(html, open)
|
|
189
|
+
if (comment.kind === 'ok') {
|
|
190
|
+
xmlDeclarationAllowed = false
|
|
191
|
+
cursor = comment.value
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
if (comment.kind === 'limit') return limitExceeded()
|
|
195
|
+
if (comment.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
196
|
+
return malformed()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (html[open + 1] === '/') {
|
|
200
|
+
xmlDeclarationAllowed = false
|
|
201
|
+
const closingTag = parseClosingTag(html, open)
|
|
202
|
+
if (closingTag.kind === 'text') {
|
|
203
|
+
cursor = open + 1
|
|
204
|
+
continue
|
|
205
|
+
}
|
|
206
|
+
if (closingTag.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
207
|
+
if (closingTag.kind !== 'ok') return malformed()
|
|
208
|
+
if (!recordTag()) return limitExceeded()
|
|
209
|
+
|
|
210
|
+
if (!closeTableEndTag(closingTag.value.name)) {
|
|
211
|
+
const target = findLastOpenElement(stack, closingTag.value.name)
|
|
212
|
+
if (target >= 0 && stack.slice(target + 1).every(({ name }) => OPTIONAL_SAME_TAG_CLOSE.has(name))) {
|
|
213
|
+
popThrough(target)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
cursor = closingTag.value.end + 1
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
xmlDeclarationAllowed = false
|
|
221
|
+
const openingTag = parseOpeningTag(html, open, options)
|
|
222
|
+
if (openingTag.kind === 'text') {
|
|
223
|
+
cursor = open + 1
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
if (openingTag.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
227
|
+
if (openingTag.kind === 'limit') return limitExceeded()
|
|
228
|
+
if (openingTag.kind !== 'ok') return malformed()
|
|
229
|
+
if (!recordTag()) return limitExceeded()
|
|
230
|
+
|
|
231
|
+
const tag = openingTag.value
|
|
232
|
+
options.onStartTag?.({ name: tag.name, end: tag.end })
|
|
233
|
+
if (!VOID_ELEMENTS.has(tag.name)) {
|
|
234
|
+
closeOptionalElementsForStart(tag.name)
|
|
235
|
+
const inForeignContent = foreignNamespaceDepth > 0
|
|
236
|
+
const metadataSuppressed = metadataSuppressionDepth > 0 || foreignNamespaceDepth > 0
|
|
237
|
+
const entry = {
|
|
238
|
+
name: tag.name,
|
|
239
|
+
metadataSuppressing: METADATA_SUPPRESSING_ELEMENTS.has(tag.name) || tag.metadataSuppressing,
|
|
240
|
+
foreignRoot: FOREIGN_ROOT_ELEMENTS.has(tag.name),
|
|
241
|
+
}
|
|
242
|
+
stack.push(entry)
|
|
243
|
+
if (entry.metadataSuppressing) metadataSuppressionDepth++
|
|
244
|
+
if (entry.foreignRoot) foreignNamespaceDepth++
|
|
245
|
+
if (options.maxDepth !== undefined && stack.length > options.maxDepth) return limitExceeded()
|
|
246
|
+
|
|
247
|
+
cursor = tag.end + 1
|
|
248
|
+
if ((inForeignContent || entry.foreignRoot) && tag.selfClosingSyntax) {
|
|
249
|
+
popStack()
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
if (tag.name === 'plaintext' && !inForeignContent) return { valid: true }
|
|
253
|
+
if (!RAW_TEXT_ELEMENTS.has(tag.name) || (inForeignContent && tag.name !== 'style')) continue
|
|
254
|
+
|
|
255
|
+
const closeStart =
|
|
256
|
+
tag.name === 'script' ? findScriptClosingTagStart(html, cursor) : findRawClosingTagStart(html, tag.name, cursor)
|
|
257
|
+
if (closeStart < 0) break
|
|
258
|
+
const closingTag = parseClosingTag(html, closeStart)
|
|
259
|
+
if (closingTag.kind === 'incomplete' && options.allowIncompleteTail) break
|
|
260
|
+
if (closingTag.kind !== 'ok') return malformed()
|
|
261
|
+
|
|
262
|
+
options.onRawElement?.({
|
|
263
|
+
name: tag.name,
|
|
264
|
+
typeAttribute: tag.typeAttribute,
|
|
265
|
+
bodyStart: cursor,
|
|
266
|
+
bodyEnd: closeStart,
|
|
267
|
+
metadataSuppressed: metadataSuppressed || entry.metadataSuppressing,
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
if (!recordTag()) return limitExceeded()
|
|
271
|
+
if (stack.at(-1)?.name === tag.name) popStack()
|
|
272
|
+
cursor = closingTag.value.end + 1
|
|
273
|
+
continue
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
cursor = tag.end + 1
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return { valid: true }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function parseComment(html: string, open: number): ParseResult<number> {
|
|
283
|
+
let cursor = open + 4
|
|
284
|
+
if (html[cursor] === '>' || html.startsWith('->', cursor)) return { kind: 'invalid' }
|
|
285
|
+
|
|
286
|
+
while (cursor < html.length) {
|
|
287
|
+
if (html.startsWith('-->', cursor)) return { kind: 'ok', value: cursor + 3 }
|
|
288
|
+
if (html.startsWith('<!--', cursor) || html.startsWith('--', cursor)) return { kind: 'invalid' }
|
|
289
|
+
cursor++
|
|
290
|
+
}
|
|
291
|
+
return { kind: 'incomplete' }
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function parseBogusComment(html: string, open: number): ParseResult<number> {
|
|
295
|
+
const end = html.indexOf('>', open + 2)
|
|
296
|
+
if (end < 0) return html.length - open > MAX_BOGUS_COMMENT_CHARS ? { kind: 'limit' } : { kind: 'incomplete' }
|
|
297
|
+
if (end - open > MAX_BOGUS_COMMENT_CHARS) return { kind: 'limit' }
|
|
298
|
+
return { kind: 'ok', value: end + 1 }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function parseDoctype(html: string, open: number): ParseResult<number> {
|
|
302
|
+
const keywordEnd = open + 9
|
|
303
|
+
if (!startsWithAsciiInsensitive(html, '<!doctype', open) || !isHtmlWhitespace(html[keywordEnd])) {
|
|
304
|
+
return { kind: 'invalid' }
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const terminator = findQuoteAwareTerminator(html, keywordEnd, '>', MAX_DOCTYPE_CHARS)
|
|
308
|
+
if (terminator.kind !== 'ok') return terminator
|
|
309
|
+
|
|
310
|
+
let cursor = skipHtmlWhitespace(html, keywordEnd)
|
|
311
|
+
if (!startsWithAsciiInsensitive(html, 'html', cursor) || !isDoctypeNameBoundary(html[cursor + 4])) {
|
|
312
|
+
return { kind: 'invalid' }
|
|
313
|
+
}
|
|
314
|
+
cursor += 4
|
|
315
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
316
|
+
if (cursor === terminator.value) return { kind: 'ok', value: terminator.value + 1 }
|
|
317
|
+
|
|
318
|
+
const externalIdStart = cursor
|
|
319
|
+
while (isAsciiLetter(html[cursor])) cursor++
|
|
320
|
+
const externalIdKind = asciiFoldSlice(html, externalIdStart, cursor)
|
|
321
|
+
if ((externalIdKind !== 'public' && externalIdKind !== 'system') || !isHtmlWhitespace(html[cursor])) {
|
|
322
|
+
return { kind: 'invalid' }
|
|
323
|
+
}
|
|
324
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
325
|
+
|
|
326
|
+
const firstIdentifier = consumeQuotedValue(html, cursor, terminator.value)
|
|
327
|
+
if (firstIdentifier.kind !== 'ok') return { kind: 'invalid' }
|
|
328
|
+
cursor = skipHtmlWhitespace(html, firstIdentifier.value)
|
|
329
|
+
|
|
330
|
+
if (externalIdKind === 'public' && cursor < terminator.value) {
|
|
331
|
+
const systemIdentifier = consumeQuotedValue(html, cursor, terminator.value)
|
|
332
|
+
if (systemIdentifier.kind !== 'ok') return { kind: 'invalid' }
|
|
333
|
+
cursor = skipHtmlWhitespace(html, systemIdentifier.value)
|
|
334
|
+
}
|
|
335
|
+
if (cursor !== terminator.value) return { kind: 'invalid' }
|
|
336
|
+
return { kind: 'ok', value: terminator.value + 1 }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function parseXmlDeclaration(html: string, open: number): ParseResult<number> {
|
|
340
|
+
if (!html.startsWith('<?xml', open) || !isHtmlWhitespace(html[open + 5])) return { kind: 'invalid' }
|
|
341
|
+
|
|
342
|
+
const terminator = findQuoteAwareTerminator(html, open + 5, '?>', MAX_XML_DECLARATION_CHARS)
|
|
343
|
+
if (terminator.kind !== 'ok') return terminator
|
|
344
|
+
|
|
345
|
+
const attributes: Array<{ name: string; value: string }> = []
|
|
346
|
+
let cursor = open + 5
|
|
347
|
+
while (cursor < terminator.value) {
|
|
348
|
+
if (!isHtmlWhitespace(html[cursor])) return { kind: 'invalid' }
|
|
349
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
350
|
+
if (cursor === terminator.value) break
|
|
351
|
+
|
|
352
|
+
const nameStart = cursor
|
|
353
|
+
while (isXmlNameCharacter(html[cursor])) cursor++
|
|
354
|
+
if (cursor === nameStart) return { kind: 'invalid' }
|
|
355
|
+
const name = html.slice(nameStart, cursor)
|
|
356
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
357
|
+
if (html[cursor] !== '=') return { kind: 'invalid' }
|
|
358
|
+
cursor = skipHtmlWhitespace(html, cursor + 1)
|
|
359
|
+
|
|
360
|
+
const valueStart = cursor
|
|
361
|
+
const quoted = consumeQuotedValue(html, cursor, terminator.value)
|
|
362
|
+
if (quoted.kind !== 'ok') return { kind: 'invalid' }
|
|
363
|
+
const quote = html[valueStart]
|
|
364
|
+
attributes.push({ name, value: html.slice(valueStart + 1, quoted.value - 1) })
|
|
365
|
+
if (quote !== '"' && quote !== "'") return { kind: 'invalid' }
|
|
366
|
+
cursor = quoted.value
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (attributes.length < 1 || attributes.length > 3) return { kind: 'invalid' }
|
|
370
|
+
if (attributes[0]?.name !== 'version' || attributes[0].value !== '1.0') return { kind: 'invalid' }
|
|
371
|
+
if (attributes[1]?.name === 'encoding' && !/^[A-Za-z][A-Za-z0-9._-]*$/u.test(attributes[1].value)) {
|
|
372
|
+
return { kind: 'invalid' }
|
|
373
|
+
}
|
|
374
|
+
const standalone = attributes.find(({ name }) => name === 'standalone')
|
|
375
|
+
if (standalone !== undefined && standalone.value !== 'yes' && standalone.value !== 'no') {
|
|
376
|
+
return { kind: 'invalid' }
|
|
377
|
+
}
|
|
378
|
+
const expectedNames = [
|
|
379
|
+
'version',
|
|
380
|
+
...(attributes.some(({ name }) => name === 'encoding') ? ['encoding'] : []),
|
|
381
|
+
...(standalone === undefined ? [] : ['standalone']),
|
|
382
|
+
]
|
|
383
|
+
if (attributes.some(({ name }, index) => name !== expectedNames[index])) return { kind: 'invalid' }
|
|
384
|
+
return { kind: 'ok', value: terminator.value + 2 }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function findQuoteAwareTerminator(
|
|
388
|
+
html: string,
|
|
389
|
+
from: number,
|
|
390
|
+
terminator: '>' | '?>',
|
|
391
|
+
maxChars: number,
|
|
392
|
+
): ParseResult<number> {
|
|
393
|
+
const limit = Math.min(html.length, from + maxChars)
|
|
394
|
+
let quote: '"' | "'" | null = null
|
|
395
|
+
let cursor = from
|
|
396
|
+
|
|
397
|
+
while (cursor < limit) {
|
|
398
|
+
const char = html[cursor]
|
|
399
|
+
if (char === '<') return { kind: 'invalid' }
|
|
400
|
+
if (quote !== null) {
|
|
401
|
+
if (char === quote) quote = null
|
|
402
|
+
cursor++
|
|
403
|
+
continue
|
|
404
|
+
}
|
|
405
|
+
if (char === '"' || char === "'") {
|
|
406
|
+
quote = char
|
|
407
|
+
cursor++
|
|
408
|
+
continue
|
|
409
|
+
}
|
|
410
|
+
if (html.startsWith(terminator, cursor)) return { kind: 'ok', value: cursor }
|
|
411
|
+
cursor++
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return limit < html.length ? { kind: 'invalid' } : { kind: 'incomplete' }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function consumeQuotedValue(html: string, from: number, end: number): ParseResult<number> {
|
|
418
|
+
const quote = html[from]
|
|
419
|
+
if (quote !== '"' && quote !== "'") return { kind: 'invalid' }
|
|
420
|
+
|
|
421
|
+
let cursor = from + 1
|
|
422
|
+
while (cursor < end) {
|
|
423
|
+
if (html[cursor] === '<') return { kind: 'invalid' }
|
|
424
|
+
if (html[cursor] === quote) return { kind: 'ok', value: cursor + 1 }
|
|
425
|
+
cursor++
|
|
426
|
+
}
|
|
427
|
+
return { kind: 'invalid' }
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function parseOpeningTag(
|
|
431
|
+
html: string,
|
|
432
|
+
open: number,
|
|
433
|
+
limits: Pick<HtmlLexicalWalkOptions, 'maxAttributesPerTag' | 'maxStartTagChars'>,
|
|
434
|
+
): ParseResult<HtmlStartTag> {
|
|
435
|
+
const nameStart = open + 1
|
|
436
|
+
if (!isAsciiLetter(html[nameStart])) return { kind: 'text' }
|
|
437
|
+
|
|
438
|
+
const nameEnd = consumeTagName(html, nameStart)
|
|
439
|
+
if (!isTagBoundary(html[nameEnd])) return { kind: 'invalid' }
|
|
440
|
+
const name = asciiFoldSlice(html, nameStart, nameEnd)
|
|
441
|
+
let typeAttribute: string | null = null
|
|
442
|
+
let typeAttributeSeen = false
|
|
443
|
+
let ariaHiddenAttributeSeen = false
|
|
444
|
+
let styleAttributeSeen = false
|
|
445
|
+
let openAttributeSeen = false
|
|
446
|
+
let metadataSuppressing = false
|
|
447
|
+
let cursor = nameEnd
|
|
448
|
+
let attributeCount = 0
|
|
449
|
+
const exceedsStartTagLimit = (position: number): boolean =>
|
|
450
|
+
limits.maxStartTagChars !== undefined && position - open > limits.maxStartTagChars
|
|
451
|
+
|
|
452
|
+
while (cursor < html.length) {
|
|
453
|
+
if (exceedsStartTagLimit(cursor)) return { kind: 'limit' }
|
|
454
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
455
|
+
if (exceedsStartTagLimit(cursor)) return { kind: 'limit' }
|
|
456
|
+
const char = html[cursor]
|
|
457
|
+
if (char === '>')
|
|
458
|
+
return {
|
|
459
|
+
kind: 'ok',
|
|
460
|
+
value: {
|
|
461
|
+
name,
|
|
462
|
+
end: cursor,
|
|
463
|
+
typeAttribute,
|
|
464
|
+
metadataSuppressing: metadataSuppressing || (isClosedNativeContainer(name) && !openAttributeSeen),
|
|
465
|
+
selfClosingSyntax: false,
|
|
466
|
+
},
|
|
467
|
+
}
|
|
468
|
+
if (char === undefined) return { kind: 'incomplete' }
|
|
469
|
+
if (char === '/') {
|
|
470
|
+
if (html[cursor + 1] === '>') {
|
|
471
|
+
return {
|
|
472
|
+
kind: 'ok',
|
|
473
|
+
value: {
|
|
474
|
+
name,
|
|
475
|
+
end: cursor + 1,
|
|
476
|
+
typeAttribute,
|
|
477
|
+
metadataSuppressing: metadataSuppressing || (isClosedNativeContainer(name) && !openAttributeSeen),
|
|
478
|
+
selfClosingSyntax: true,
|
|
479
|
+
},
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { kind: 'invalid' }
|
|
483
|
+
}
|
|
484
|
+
if (char === '<' || char === '=' || char === '"' || char === "'") return { kind: 'invalid' }
|
|
485
|
+
|
|
486
|
+
const attributeStart = cursor
|
|
487
|
+
while (cursor < html.length && !isAttributeNameBoundary(html[cursor])) {
|
|
488
|
+
cursor++
|
|
489
|
+
if (exceedsStartTagLimit(cursor)) return { kind: 'limit' }
|
|
490
|
+
}
|
|
491
|
+
if (cursor === attributeStart) return { kind: 'invalid' }
|
|
492
|
+
attributeCount++
|
|
493
|
+
if (limits.maxAttributesPerTag !== undefined && attributeCount > limits.maxAttributesPerTag) {
|
|
494
|
+
return { kind: 'limit' }
|
|
495
|
+
}
|
|
496
|
+
const attributeName = asciiFoldSlice(html, attributeStart, cursor)
|
|
497
|
+
cursor = skipHtmlWhitespace(html, cursor)
|
|
498
|
+
|
|
499
|
+
let attributeValue: string | null = null
|
|
500
|
+
let attributeValueUncertain = false
|
|
501
|
+
if (html[cursor] === '=') {
|
|
502
|
+
cursor = skipHtmlWhitespace(html, cursor + 1)
|
|
503
|
+
const quote = html[cursor]
|
|
504
|
+
if (quote === '"' || quote === "'") {
|
|
505
|
+
const valueStart = ++cursor
|
|
506
|
+
while (cursor < html.length && html[cursor] !== quote) {
|
|
507
|
+
cursor++
|
|
508
|
+
if (exceedsStartTagLimit(cursor)) return { kind: 'limit' }
|
|
509
|
+
}
|
|
510
|
+
if (cursor >= html.length) return { kind: 'incomplete' }
|
|
511
|
+
const rawAttributeValue = html.slice(valueStart, cursor)
|
|
512
|
+
attributeValue = decodeRelevantAttributeValue(rawAttributeValue)
|
|
513
|
+
attributeValueUncertain = attributeValue === null && rawAttributeValue.includes('&')
|
|
514
|
+
cursor++
|
|
515
|
+
} else {
|
|
516
|
+
const valueStart = cursor
|
|
517
|
+
while (cursor < html.length && !isUnquotedAttributeBoundary(html[cursor])) {
|
|
518
|
+
if (isInvalidUnquotedAttributeCharacter(html[cursor])) return { kind: 'invalid' }
|
|
519
|
+
cursor++
|
|
520
|
+
if (exceedsStartTagLimit(cursor)) return { kind: 'limit' }
|
|
521
|
+
}
|
|
522
|
+
if (cursor === valueStart) return { kind: 'invalid' }
|
|
523
|
+
const rawAttributeValue = html.slice(valueStart, cursor)
|
|
524
|
+
attributeValue = decodeRelevantAttributeValue(rawAttributeValue)
|
|
525
|
+
attributeValueUncertain = attributeValue === null && rawAttributeValue.includes('&')
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if (attributeName === 'type' && !typeAttributeSeen) {
|
|
530
|
+
typeAttributeSeen = true
|
|
531
|
+
typeAttribute = attributeValue === null ? null : asciiFold(trimHtmlWhitespace(attributeValue))
|
|
532
|
+
}
|
|
533
|
+
if (attributeName === 'open') openAttributeSeen = true
|
|
534
|
+
if (attributeName === 'hidden' || attributeName === 'inert') metadataSuppressing = true
|
|
535
|
+
if (attributeName === 'aria-hidden' && !ariaHiddenAttributeSeen) {
|
|
536
|
+
ariaHiddenAttributeSeen = true
|
|
537
|
+
if (asciiFold(attributeValue?.trim() ?? '') === 'true') metadataSuppressing = true
|
|
538
|
+
}
|
|
539
|
+
if (attributeName === 'style' && !styleAttributeSeen) {
|
|
540
|
+
styleAttributeSeen = true
|
|
541
|
+
if (
|
|
542
|
+
attributeValueUncertain ||
|
|
543
|
+
(attributeValue !== null && classifyRawInlineStyleVisibility(attributeValue) !== 'visible')
|
|
544
|
+
) {
|
|
545
|
+
metadataSuppressing = true
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return { kind: 'incomplete' }
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function isClosedNativeContainer(name: string): boolean {
|
|
554
|
+
return name === 'details' || name === 'dialog'
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function findLastOpenElement(stack: StackEntry[], name: string): number {
|
|
558
|
+
for (let index = stack.length - 1; index >= 0; index--) {
|
|
559
|
+
if (stack[index]?.name === name) return index
|
|
560
|
+
}
|
|
561
|
+
return -1
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function parseClosingTag(html: string, open: number): ParseResult<{ name: string; end: number }> {
|
|
565
|
+
const nameStart = open + 2
|
|
566
|
+
if (!isAsciiLetter(html[nameStart])) return { kind: 'invalid' }
|
|
567
|
+
|
|
568
|
+
const nameEnd = consumeTagName(html, nameStart)
|
|
569
|
+
if (!isTagBoundary(html[nameEnd])) return { kind: 'invalid' }
|
|
570
|
+
const end = skipHtmlWhitespace(html, nameEnd)
|
|
571
|
+
if (html[end] === undefined) return { kind: 'incomplete' }
|
|
572
|
+
if (html[end] !== '>') return { kind: 'invalid' }
|
|
573
|
+
return { kind: 'ok', value: { name: asciiFoldSlice(html, nameStart, nameEnd), end } }
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function findRawClosingTagStart(html: string, name: string, from: number): number {
|
|
577
|
+
const prefix = `</${name}`
|
|
578
|
+
let cursor = from
|
|
579
|
+
while (cursor < html.length) {
|
|
580
|
+
const index = html.indexOf('<', cursor)
|
|
581
|
+
if (index < 0) return -1
|
|
582
|
+
if (startsWithAsciiInsensitive(html, prefix, index) && isTagBoundary(html[index + prefix.length])) return index
|
|
583
|
+
cursor = index + 1
|
|
584
|
+
}
|
|
585
|
+
return -1
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function findScriptClosingTagStart(html: string, from: number): number {
|
|
589
|
+
type ScriptState = 'data' | 'escaped' | 'double-escaped'
|
|
590
|
+
|
|
591
|
+
let state: ScriptState = 'data'
|
|
592
|
+
let cursor = from
|
|
593
|
+
while (cursor < html.length) {
|
|
594
|
+
if (state === 'data') {
|
|
595
|
+
if (html.startsWith('<!--', cursor)) {
|
|
596
|
+
state = 'escaped'
|
|
597
|
+
cursor += 4
|
|
598
|
+
continue
|
|
599
|
+
}
|
|
600
|
+
if (isScriptEndTagAt(html, cursor)) return cursor
|
|
601
|
+
} else if (state === 'escaped') {
|
|
602
|
+
if (html.startsWith('-->', cursor)) {
|
|
603
|
+
state = 'data'
|
|
604
|
+
cursor += 3
|
|
605
|
+
continue
|
|
606
|
+
}
|
|
607
|
+
if (isScriptStartTagAt(html, cursor)) {
|
|
608
|
+
state = 'double-escaped'
|
|
609
|
+
cursor += 7
|
|
610
|
+
continue
|
|
611
|
+
}
|
|
612
|
+
if (isScriptEndTagAt(html, cursor)) return cursor
|
|
613
|
+
} else if (isScriptEndTagAt(html, cursor)) {
|
|
614
|
+
state = 'escaped'
|
|
615
|
+
cursor += 8
|
|
616
|
+
continue
|
|
617
|
+
}
|
|
618
|
+
cursor++
|
|
619
|
+
}
|
|
620
|
+
return -1
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function isScriptStartTagAt(html: string, cursor: number): boolean {
|
|
624
|
+
const prefix = '<script'
|
|
625
|
+
return startsWithAsciiInsensitive(html, prefix, cursor) && isTagBoundary(html[cursor + prefix.length])
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function isScriptEndTagAt(html: string, cursor: number): boolean {
|
|
629
|
+
const prefix = '</script'
|
|
630
|
+
return startsWithAsciiInsensitive(html, prefix, cursor) && isTagBoundary(html[cursor + prefix.length])
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function findOpenTableEntry(stack: StackEntry[], targets: Set<string>, boundaries: Set<string>): number {
|
|
634
|
+
for (let index = stack.length - 1; index >= 0; index--) {
|
|
635
|
+
const entry = stack[index]!
|
|
636
|
+
if (entry.metadataSuppressing || entry.foreignRoot) return -1
|
|
637
|
+
const name = entry.name
|
|
638
|
+
if (targets.has(name)) return index
|
|
639
|
+
if (boundaries.has(name)) return -1
|
|
640
|
+
}
|
|
641
|
+
return -1
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function findTableEndTarget(stack: StackEntry[], name: string): number {
|
|
645
|
+
const isSection = name === 'thead' || name === 'tbody' || name === 'tfoot'
|
|
646
|
+
if (!isSection && name !== 'table') return -1
|
|
647
|
+
|
|
648
|
+
const allowed = isSection ? new Set(['tr', 'td', 'th']) : new Set(['thead', 'tbody', 'tfoot', 'tr', 'td', 'th'])
|
|
649
|
+
for (let index = stack.length - 1; index >= 0; index--) {
|
|
650
|
+
const entry = stack[index]!
|
|
651
|
+
if (entry.metadataSuppressing || entry.foreignRoot) return -1
|
|
652
|
+
if (entry.name === name) return index
|
|
653
|
+
if (!allowed.has(entry.name)) return -1
|
|
654
|
+
}
|
|
655
|
+
return -1
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function consumeTagName(html: string, from: number): number {
|
|
659
|
+
let cursor = from
|
|
660
|
+
while (isTagNameCharacter(html[cursor])) cursor++
|
|
661
|
+
return cursor
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function skipHtmlWhitespace(html: string, from: number): number {
|
|
665
|
+
let cursor = from
|
|
666
|
+
while (isHtmlWhitespace(html[cursor])) cursor++
|
|
667
|
+
return cursor
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function isAsciiLetter(char: string | undefined): boolean {
|
|
671
|
+
return char !== undefined && /[A-Za-z]/u.test(char)
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function isTagNameCharacter(char: string | undefined): boolean {
|
|
675
|
+
return char !== undefined && /[A-Za-z0-9:_-]/u.test(char)
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function isXmlNameCharacter(char: string | undefined): boolean {
|
|
679
|
+
return char !== undefined && /[A-Za-z]/u.test(char)
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function isHtmlWhitespace(char: string | undefined): boolean {
|
|
683
|
+
return char === '\t' || char === '\n' || char === '\f' || char === '\r' || char === ' '
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function isTagBoundary(char: string | undefined): boolean {
|
|
687
|
+
return char === undefined || isHtmlWhitespace(char) || char === '/' || char === '>'
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function isDoctypeNameBoundary(char: string | undefined): boolean {
|
|
691
|
+
return isHtmlWhitespace(char) || char === '>'
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function isAttributeNameBoundary(char: string | undefined): boolean {
|
|
695
|
+
return char === undefined || isHtmlWhitespace(char) || char === '/' || char === '>' || char === '='
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function isUnquotedAttributeBoundary(char: string | undefined): boolean {
|
|
699
|
+
return char === undefined || isHtmlWhitespace(char) || char === '>'
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function isInvalidUnquotedAttributeCharacter(char: string | undefined): boolean {
|
|
703
|
+
return char === '<' || char === '=' || char === '"' || char === "'" || char === '`'
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function isIgnorablePrologText(value: string): boolean {
|
|
707
|
+
return /^\uFEFF?[\t\n\f\r ]*$/u.test(value)
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function trimHtmlWhitespace(value: string): string {
|
|
711
|
+
return value.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/gu, '')
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function asciiFoldSlice(value: string, start: number, end: number): string {
|
|
715
|
+
let folded = ''
|
|
716
|
+
for (let index = start; index < end; index++) folded += asciiFoldCharacter(value[index])
|
|
717
|
+
return folded
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
function asciiFold(value: string): string {
|
|
721
|
+
let folded = ''
|
|
722
|
+
for (const char of value) folded += asciiFoldCharacter(char)
|
|
723
|
+
return folded
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const NAMED_ATTRIBUTE_REFERENCES: Readonly<Record<string, string>> = {
|
|
727
|
+
amp: '&',
|
|
728
|
+
apos: "'",
|
|
729
|
+
colon: ':',
|
|
730
|
+
gt: '>',
|
|
731
|
+
lt: '<',
|
|
732
|
+
quot: '"',
|
|
733
|
+
sol: '/',
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const WINDOWS_1252_REFERENCES: Readonly<Record<number, number>> = {
|
|
737
|
+
0x80: 0x20ac,
|
|
738
|
+
0x82: 0x201a,
|
|
739
|
+
0x83: 0x0192,
|
|
740
|
+
0x84: 0x201e,
|
|
741
|
+
0x85: 0x2026,
|
|
742
|
+
0x86: 0x2020,
|
|
743
|
+
0x87: 0x2021,
|
|
744
|
+
0x88: 0x02c6,
|
|
745
|
+
0x89: 0x2030,
|
|
746
|
+
0x8a: 0x0160,
|
|
747
|
+
0x8b: 0x2039,
|
|
748
|
+
0x8c: 0x0152,
|
|
749
|
+
0x8e: 0x017d,
|
|
750
|
+
0x91: 0x2018,
|
|
751
|
+
0x92: 0x2019,
|
|
752
|
+
0x93: 0x201c,
|
|
753
|
+
0x94: 0x201d,
|
|
754
|
+
0x95: 0x2022,
|
|
755
|
+
0x96: 0x2013,
|
|
756
|
+
0x97: 0x2014,
|
|
757
|
+
0x98: 0x02dc,
|
|
758
|
+
0x99: 0x2122,
|
|
759
|
+
0x9a: 0x0161,
|
|
760
|
+
0x9b: 0x203a,
|
|
761
|
+
0x9c: 0x0153,
|
|
762
|
+
0x9e: 0x017e,
|
|
763
|
+
0x9f: 0x0178,
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function decodeRelevantAttributeValue(value: string): string | null {
|
|
767
|
+
let decoded = ''
|
|
768
|
+
let cursor = 0
|
|
769
|
+
while (cursor < value.length) {
|
|
770
|
+
const ampersand = value.indexOf('&', cursor)
|
|
771
|
+
if (ampersand < 0) return decoded + value.slice(cursor)
|
|
772
|
+
decoded += value.slice(cursor, ampersand)
|
|
773
|
+
const numeric = consumeNumericAttributeReference(value, ampersand)
|
|
774
|
+
if (numeric !== null) {
|
|
775
|
+
decoded += numeric.replacement
|
|
776
|
+
cursor = numeric.end
|
|
777
|
+
continue
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
const semicolon = value.indexOf(';', ampersand + 1)
|
|
781
|
+
if (semicolon < 0 || semicolon - ampersand > 32) return null
|
|
782
|
+
const replacement = NAMED_ATTRIBUTE_REFERENCES[asciiFold(value.slice(ampersand + 1, semicolon))]
|
|
783
|
+
if (replacement === undefined) return null
|
|
784
|
+
decoded += replacement
|
|
785
|
+
cursor = semicolon + 1
|
|
786
|
+
}
|
|
787
|
+
return decoded
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
function consumeNumericAttributeReference(
|
|
791
|
+
value: string,
|
|
792
|
+
ampersand: number,
|
|
793
|
+
): { replacement: string; end: number } | null {
|
|
794
|
+
if (value[ampersand + 1] !== '#') return null
|
|
795
|
+
const hexadecimal = value[ampersand + 2] === 'x' || value[ampersand + 2] === 'X'
|
|
796
|
+
const digitStart = ampersand + (hexadecimal ? 3 : 2)
|
|
797
|
+
let digitEnd = digitStart
|
|
798
|
+
while (hexadecimal ? isAsciiHexDigit(value[digitEnd]) : isAsciiDigit(value[digitEnd])) digitEnd++
|
|
799
|
+
if (digitEnd === digitStart || digitEnd - ampersand > 32) return null
|
|
800
|
+
|
|
801
|
+
const digits = value.slice(digitStart, digitEnd)
|
|
802
|
+
const parsed = Number.parseInt(digits, hexadecimal ? 16 : 10)
|
|
803
|
+
const remapped = WINDOWS_1252_REFERENCES[parsed] ?? parsed
|
|
804
|
+
const replacement =
|
|
805
|
+
remapped === 0 || remapped > 0x10ffff || (remapped >= 0xd800 && remapped <= 0xdfff)
|
|
806
|
+
? '\uFFFD'
|
|
807
|
+
: String.fromCodePoint(remapped)
|
|
808
|
+
return { replacement, end: value[digitEnd] === ';' ? digitEnd + 1 : digitEnd }
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function isAsciiDigit(char: string | undefined): boolean {
|
|
812
|
+
return char !== undefined && char >= '0' && char <= '9'
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function isAsciiHexDigit(char: string | undefined): boolean {
|
|
816
|
+
return (
|
|
817
|
+
isAsciiDigit(char) || (char !== undefined && asciiFoldCharacter(char) >= 'a' && asciiFoldCharacter(char) <= 'f')
|
|
818
|
+
)
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function startsWithAsciiInsensitive(value: string, expectedLowercase: string, start: number): boolean {
|
|
822
|
+
if (start + expectedLowercase.length > value.length) return false
|
|
823
|
+
for (let index = 0; index < expectedLowercase.length; index++) {
|
|
824
|
+
if (asciiFoldCharacter(value[start + index]) !== expectedLowercase[index]) return false
|
|
825
|
+
}
|
|
826
|
+
return true
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function asciiFoldCharacter(char: string | undefined): string {
|
|
830
|
+
if (char === undefined) return ''
|
|
831
|
+
const code = char.charCodeAt(0)
|
|
832
|
+
return code >= 65 && code <= 90 ? String.fromCharCode(code + 32) : char
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function malformed(): HtmlLexicalWalkResult {
|
|
836
|
+
return { valid: false, failure: 'malformed' }
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function limitExceeded(): HtmlLexicalWalkResult {
|
|
840
|
+
return { valid: false, failure: 'limit' }
|
|
841
|
+
}
|