The headless browser for AI agents and web scraping
fix(dom): stop a generic parsererror from erasing the reason
Nguyen Thanh Dat25af14453777
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 8a77042b..5b8cdb03 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -10399,20 +10399,40 @@ function _xmlWellFormed(src) {return stack.length === 0 && rootsClosed === 1;}+// The parsererror detail quotes tag names taken from the input, and it is+// written through innerHTML, so `<` and `&` have to stop being markup.+const _escapeXmlErrorText = (text) =>+ String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');+globalThis.DOMParser = class DOMParser {parseFromString(source, mimeType) {const html = String(source ?? "");const isXml = typeof mimeType === "string" && /xml/i.test(mimeType);const root = document.createElement("html");- // 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.+ // 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.+ //+ // Two checks, one decision. `_xmlWellFormed` is the stricter of the pair --+ // it also rejects input with no root element at all, and an unterminated+ // comment/CDATA/PI -- so it decides whether this is an error. What it cannot+ // do is say why: it returns a bool. `_checkXmlWellFormed` names the fault,+ // so its message fills the <div> when it has one.+ //+ // These used to run as two independent blocks, the second overwriting the+ // first. Since the stricter check flags everything the descriptive one+ // flags, the description never reached a caller.const xmlError = isXml ? _checkXmlWellFormed(html) : null;- const isParserError = xmlError && !xmlError.wellFormed;+ const isParserError = isXml && (!_xmlWellFormed(html) || !xmlError.wellFormed);if (isParserError) {- root.innerHTML = '<parsererror>' + xmlError.error + '</parsererror>';+ const detail = (xmlError && xmlError.error) || 'error while parsing XML';+ try {+ root.innerHTML =+ '<parsererror xmlns="http://www.w3.org/1999/xhtml">This page contains the following errors:<div>' ++ _escapeXmlErrorText(detail) ++ '</div></parsererror>';+ } catch (e) { /* ignore */ }} else {// innerHTML parses children via html5ever fragment-parsing rules. Most// HTML inputs start with `<!DOCTYPE>` / `<html>` / `<head>` etc.; the@@ -10421,15 +10441,6 @@ globalThis.DOMParser = class DOMParser {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;diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 1211d43e..251886bf 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -5254,6 +5254,42 @@ mod tests {assert_eq!(ok, serde_json::json!("OK"));}+ #[test]+ fn a_parsererror_says_what_was_wrong() {+ // The <div> is where Chrome puts the reason, and callers show it. A+ // parsererror that only says "error while parsing XML" tells a user+ // nothing the presence of the element did not already say.+ let mut rt = setup_runtime("<html><body></body></html>");+ let detail = rt+ .evaluate("(function(){var d=new DOMParser().parseFromString('<a><b></a>','application/xml'); return d.querySelector('parsererror').textContent;})()")+ .unwrap();+ let detail = detail.as_str().unwrap_or_default().to_string();+ assert!(+ detail.contains("mismatch") && detail.contains('b') && detail.contains('a'),+ "the reason must name the fault and the tags involved, got: {detail}"+ );+ }++ #[test]+ fn a_parsererror_detail_is_text_not_markup() {+ // The reason quotes a tag name lifted from the input and is written+ // through innerHTML, so an input tag name must not become an element.+ //+ // The input has to be one whose message carries the angle brackets:+ // "unclosed tag <img>" does, "mismatch: img and a" does not, and a test+ // built on the latter passes with the escaping removed.+ let mut rt = setup_runtime("<html><body></body></html>");+ let n = rt+ .evaluate("(function(){var d=new DOMParser().parseFromString('<img src=x>','application/xml'); var e=d.querySelector('parsererror'); return e ? e.querySelectorAll('img').length : -1;})()")+ .unwrap();+ // JS numbers arrive as f64, so compare as one.+ assert_eq!(+ n.as_f64(),+ Some(0.0),+ "a tag name from the input became an element in the error detail"+ );+ }+#[test]fn dom_parser_html_never_gets_parsererror() {// HTML parsing is tolerant and must never synthesize a parsererror.