{"record":{"id":"b700781bbba7efc7","repo":"DayuanJiang/next-ai-draw-io","slug":"no-svg-element-found-in-the-input-string","errorCode":null,"errorMessage":"No SVG element found in the input string.","messagePattern":"No SVG element found in the input string\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/utils.ts","lineNumber":1685,"sourceCode":"    // so we can see what was fixed and what error remains\n    return {\n        valid: false,\n        error,\n        fixed: fixes.length > 0 ? fixed : null,\n        fixes,\n    }\n}\n\nexport function extractDiagramXML(xml_svg_string: string): string {\n    try {\n        // 1. Parse the SVG string (using built-in DOMParser in a browser-like environment)\n        const svgString = atob(xml_svg_string.slice(26))\n        const parser = new DOMParser()\n        const svgDoc = parser.parseFromString(svgString, \"image/svg+xml\")\n        const svgElement = svgDoc.querySelector(\"svg\")\n\n        if (!svgElement) {\n            throw new Error(\"No SVG element found in the input string.\")\n        }\n        // 2. Extract the 'content' attribute\n        const encodedContent = svgElement.getAttribute(\"content\")\n\n        if (!encodedContent) {\n            throw new Error(\"SVG element does not have a 'content' attribute.\")\n        }\n\n        // 3. Decode HTML entities (using a minimal function)\n        function decodeHtmlEntities(str: string) {\n            const textarea = document.createElement(\"textarea\") // Use built-in element\n            textarea.innerHTML = str\n            return textarea.value\n        }\n        const xmlContent = decodeHtmlEntities(encodedContent)\n\n        // 4. Parse the XML content\n        const xmlDoc = parser.parseFromString(xmlContent, \"text/xml\")","sourceCodeStart":1667,"sourceCodeEnd":1703,"githubUrl":"https://github.com/DayuanJiang/next-ai-draw-io/blob/155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1/lib/utils.ts#L1667-L1703","documentation":"This error is thrown while decoding an embedded draw.io diagram from a base64 data-URI SVG: after stripping the 26-char prefix, base64-decoding, and parsing as image/svg+xml, no <svg> root element could be found. The library expects the input to be a draw.io-exported SVG (one starting with 'data:image/svg+xml;base64,'). A parse failure (e.g. decode produced garbage) leaves querySelector('svg') null.","triggerScenarios":"Passing a string whose first 26 characters are not a valid base64 SVG data-URI prefix, passing an uncompressed .svg file content directly instead of the base64-encoded data URI, or corrupt base64 that atob decodes into non-SVG bytes.","commonSituations":"Reading the 'xml_svg_string' from a database or clipboard where the prefix was trimmed or URL-encoded; passing raw SVG markup instead of the data-URI form; encoding mismatches (UTF-16 vs UTF-8) before base64.","solutions":["Verify the input starts with 'data:image/svg+xml;base64,' (26 chars) before calling; strip whitespace/BOM","If you have raw SVG, base64-encode it first: 'data:image/svg+xml;base64,' + btoa(svgString)","Check the decoded string with atob yourself and confirm it parses as SVG","URL-decode the value first if it came from a query parameter"],"exampleFix":"// before\nextractDiagramFromSvg(rawSvgMarkup) // throws: No SVG element found\n\n// after\nconst dataUri = 'data:image/svg+xml;base64,' + btoa(rawSvgMarkup)\nextractDiagramFromSvg(dataUri)","handlingStrategy":"validation","validationCode":"const PREFIX = 'data:image/svg+xml;base64,'\nif (!str.startsWith(PREFIX) || str.length <= PREFIX.length) throw new Error('expected base64 svg data URI')\nconst decoded = atob(str.slice(26))\nif (!new DOMParser().parseFromString(decoded, 'image/svg+xml').querySelector('svg')) throw new Error('not an svg payload')","typeGuard":"function isBase64SvgDataUri(s: string): boolean {\n  return /^data:image\\/svg\\+xml;base64,[A-Za-z0-9+/=]+$/.test(s.trim())\n}","tryCatchPattern":"try { extract(str) } catch (e) { if ((e as Error).message.includes('No SVG element')) throw new Error(`input is not a draw.io svg data URI: ${str.slice(0, 40)}...`) }","preventionTips":["Always pass the full data URI including the 26-char prefix","Trim whitespace/newlines introduced by transport before calling","URL-decode values that came from URLs"],"tags":["svg","base64","drawio","parsing"],"backgroundTag":"invalid-base64-payload","analyzedSha":"155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1","analyzedAt":"2026-08-27T11:40:38.297Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}