UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

195 lines (194 loc) • 5.31 kB
//#region packages/markdown-core/src/html-scanner.ts const RAW_TEXT_TAGS = /* @__PURE__ */ new Set([ "script", "style", "noscript" ]); function isAsciiWhitespace(e) { return e === " " || e === "\n" || e === "\r" || e === " " || e === "\f"; } function isTagNameChar(e) { const t = e.charCodeAt(0); return t >= 48 && t <= 57 || t >= 65 && t <= 90 || t >= 97 && t <= 122 || e === "." || e === "-" || e === "_" || e === ":"; } function isTagNameStartChar(e) { const t = e.charCodeAt(0); return t >= 65 && t <= 90 || t >= 97 && t <= 122; } function isTagBoundary(e) { return !e || isAsciiWhitespace(e) || e === ">" || e === "/"; } function asciiLower(e) { const t = e.charCodeAt(0); return t >= 65 && t <= 90 ? String.fromCharCode(t + 32) : e; } function startsWithClosingTag(e, t, n) { if (e[t] !== "<" || e[t + 1] !== "/") return false; for (let r = 0; r < n.length; r += 1) if (asciiLower(e[t + 2 + r] ?? "") !== n[r]) return false; return isTagBoundary(e[t + 2 + n.length]); } function readRawTextOpenTagName(e, t) { if (e[t] !== "<" || e[t + 1] === "/") return; for (const n of RAW_TEXT_TAGS) { let r = true; for (let i = 0; i < n.length; i += 1) if (asciiLower(e[t + 1 + i] ?? "") !== n[i]) { r = false; break; } if (r && isTagBoundary(e[t + 1 + n.length])) return n; } } function findRawTextOpenTagStart(e, t, n) { const r = e.slice(t, n); for (let n = r.indexOf("<"); n !== -1; n = r.indexOf("<", n + 1)) { const r = t + n; if (readRawTextOpenTagName(e, r)) return r; } return -1; } function startsLikeHtmlTag(e, t) { const n = e[t + 1]; return n === "!" || n === "?" || n === "/" || isTagNameStartChar(n ?? ""); } function findTagEnd(e, t, n = "render") { const r = n === "render"; let i = false; let a; for (let n = t + 1; n < e.length; n += 1) { const t = e.charAt(n); if (i && isAsciiWhitespace(t)) continue; if ((!r || i) && (t === "\"" || t === "'")) { const o = e.indexOf(t, n + 1); if (r && a === void 0) { const t = findRawTextOpenTagStart(e, n + 1, o === -1 ? e.length : o); if (t !== -1) a = t; } if (o === -1) return { end: -1, rawTextStart: a }; n = o; i = false; continue; } i = false; if (r && readRawTextOpenTagName(e, n)) return { end: -1, rawTextStart: n }; if (t === ">") return { end: n }; if (t === "=") i = true; } return { end: -1, rawTextStart: a }; } function isSelfClosingTagRaw(e) { const t = e.trimEnd(); if (!t.endsWith("/")) return false; const n = t.charAt(t.length - 2); return !n || isAsciiWhitespace(n) || n === "\"" || n === "'" || !/[ \n\r\t\f]/.test(t.slice(0, -1)); } function skipHtmlComment(e, t) { if (e[t + 4] === ">") return t + 5; if (e.startsWith("->", t + 4)) return t + 6; for (let n = e.indexOf("--", t + 4); n !== -1; n = e.indexOf("--", n + 1)) { const t = e[n + 2] === "!" ? n + 3 : n + 2; if (e[t] === ">") return t + 1; } return e.length; } function readTagToken(e, t, n = "render") { const r = n === "render"; if (r && e.startsWith("<!--", t)) return { token: null, next: skipHtmlComment(e, t) }; const i = findTagEnd(e, t, n); const a = i.end; if (a === -1) return i.rawTextStart === void 0 ? null : { token: null, next: i.rawTextStart }; const o = e.slice(t + 1, a); let s = 0; const c = o[s] === "/"; if (c) s += 1; if (!isTagNameStartChar(o[s] ?? "")) return { token: null, next: a + 1 }; const l = s; while (s < o.length && !isAsciiWhitespace(o.charAt(s)) && o[s] !== "/" && o[s] !== ">") s += 1; const u = c ? "" : o.slice(s); return { token: { closing: c, name: o.slice(l, s).replace(/\0|[A-Z]/g, (e) => e === "\0" ? "�" : asciiLower(e)), raw: o, attrs: u, selfClosing: r ? isSelfClosingTagRaw(o) : !c && u.trimEnd().endsWith("/") }, next: a + 1 }; } function readRawTextBounds(e, t, n, r = "render") { if (t === "script") { let i = "data"; for (let a = n; a < e.length; a += 1) { if (i === "data" && e.startsWith("<!--", a)) { i = "escaped"; a += 1; continue; } if (i !== "data" && e.startsWith("-->", a)) { i = "data"; a += 2; continue; } if (e[a] !== "<") continue; if (startsWithClosingTag(e, a, t)) { if (i === "double-escaped") { i = "escaped"; a += t.length + 1; continue; } const n = findTagEnd(e, a, r).end; return { contentEnd: a, end: n === -1 ? e.length : n + 1 }; } if (i === "escaped" && readRawTextOpenTagName(e, a) === "script") { i = "double-escaped"; a += t.length; } } return { contentEnd: e.length, end: e.length }; } let i = e.indexOf("</", n); while (i !== -1) { if (startsWithClosingTag(e, i, t)) { const t = findTagEnd(e, i, r).end; return { contentEnd: i, end: t === -1 ? e.length : t + 1 }; } i = e.indexOf("</", i + 2); } return { contentEnd: e.length, end: e.length }; } function skipRawTextElement(e, t, n) { const r = findTagEnd(e, t); return readRawTextBounds(e, n, r.end === -1 ? t + n.length + 1 : r.end + 1).end; } //#endregion export { readRawTextBounds as a, skipHtmlComment as c, isTagNameChar as i, skipRawTextElement as l, findRawTextOpenTagStart as n, readRawTextOpenTagName as o, isAsciiWhitespace as r, readTagToken as s, RAW_TEXT_TAGS as t, startsLikeHtmlTag as u };