The headless browser for AI agents and web scraping
Merge pull request #545 from zhangyang-crazy-one/fix/domparser-xml-parsererror
SGavrl9e326ed1e3c6
diff --cc crates/obscura-js/js/bootstrap.jsindex 7beeb6b5,0ce923a9..e2ad6eb6--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@@ -9667,21 -5524,23 +9722,32 @@@ globalThis.DOMParser = class DOMParserconst html = String(source ?? "");const isXml = typeof mimeType === "string" && /xml/i.test(mimeType);const root = document.createElement("html");- // innerHTML parses children via html5ever fragment-parsing rules. Most- // HTML inputs start with `<!DOCTYPE>` / `<html>` / `<head>` etc.; the- // fragment parser strips the outer `<html>` and emits its head+body- // children, which is what callers want.- try { root.innerHTML = html; } catch (e) { /* leave empty on parse error */ }++ // For XML mime types, check well-formedness first (conservative: only+ // clear errors like tag mismatch / extra root are flagged). If the+ // check fires, build a <parsererror> root so callers doing+ // doc.querySelector('parsererror') get the same signal as in Chrome.+ const xmlError = isXml ? _checkXmlWellFormed(html) : null;+ const isParserError = xmlError && !xmlError.wellFormed;+ if (isParserError) {+ root.innerHTML = '<parsererror>' + xmlError.error + '</parsererror>';+ } else {+ // innerHTML parses children via html5ever fragment-parsing rules. Most+ // HTML inputs start with `<!DOCTYPE>` / `<html>` / `<head>` etc.; the+ // fragment parser strips the outer `<html>` and emits its head+body+ // children, which is what callers want.+ try { root.innerHTML = html; } catch (e) { /* leave empty on parse error */ }+ }+ // For XML mime types, surface a <parsererror> on clearly-malformed input so+ // error-detection code (doc.querySelector('parsererror')) works, matching+ // Chrome. obscura has no XML parser, so the tree stays HTML-parsed.+ if (isXml && !_xmlWellFormed(html)) {+ try {+ root.innerHTML = '<parsererror xmlns="http://www.w3.org/1999/xhtml">This page contains the following errors:<div>error while parsing XML</div></parsererror>';+ } catch (e) { /* ignore */ }+ }+// Helper: depth-first walk to find an element by predicate.const walk = (node, pred) => {if (!node) return null;