DayuanJiang/next-ai-draw-io · error · Error

No encoded data found in the diagram element

Error message

No encoded data found in the diagram element

What it means

A <diagram> element was found, but its textContent is empty, so there is no base64-encoded compressed diagram data to inflate. In draw.io files each <diagram> holds base64 (deflate-raw + urlencode) mxGraphModel data; an empty one means the page exists but has no stored content.

Source

Thrown at lib/utils.ts:1713

        function decodeHtmlEntities(str: string) {
            const textarea = document.createElement("textarea") // Use built-in element
            textarea.innerHTML = str
            return textarea.value
        }
        const xmlContent = decodeHtmlEntities(encodedContent)

        // 4. Parse the XML content
        const xmlDoc = parser.parseFromString(xmlContent, "text/xml")
        const diagramElement = xmlDoc.querySelector("diagram")

        if (!diagramElement) {
            throw new Error("No diagram element found")
        }
        // 5. Extract base64 encoded data
        const base64EncodedData = diagramElement.textContent

        if (!base64EncodedData) {
            throw new Error("No encoded data found in the diagram element")
        }

        // 6. Decode base64 data
        const binaryString = atob(base64EncodedData)

        // 7. Convert binary string to Uint8Array
        const len = binaryString.length
        const bytes = new Uint8Array(len)
        for (let i = 0; i < len; i++) {
            bytes[i] = binaryString.charCodeAt(i)
        }

        // 8. Decompress data using pako (equivalent to zlib.decompress with wbits=-15)
        const decompressedData = pako.inflate(bytes, { windowBits: -15 })

        // 9. Convert the decompressed data to a string
        const decoder = new TextDecoder("utf-8")
        const decodedString = decoder.decode(decompressedData)

View on GitHub (pinned to 155ef4f7ac)

Solutions

  1. Skip empty diagrams or treat them as blank pages instead of decoding
  2. If constructing pages programmatically, embed the base64 body: '<diagram>' + btoa(deflatedMxGraphModel) + '</diagram>'
  3. Validate textContent is non-empty before calling and report which page id/name is empty
  4. Regenerate the file from draw.io if the source is corrupt

Example fix

// before
const xml = decodeDiagramFromSvg(dataUri) // '<diagram id="1" name="p"/>'

// after
const diagrams = [...mxfileDoc.querySelectorAll('diagram')].filter(d => d.textContent?.trim())
if (!diagrams.length) throw new Error('file has no pages with content')
Defensive patterns

Strategy: validation

Validate before calling

const diagrams = [...mxfileDoc.querySelectorAll('diagram')].filter(d => d.textContent?.trim())
if (diagrams.length === 0) throw new Error('mxfile contains no diagram with encoded data')

Type guard

function diagramHasPayload(d: Element): boolean {
  return Boolean(d.textContent && d.textContent.trim().length > 0)
}

Try / catch

for (const d of diagrams) { if (!diagramHasPayload(d)) continue; results.push(decodeDiagram(d)) }

Prevention

When it happens

Trigger: Calling the decoder on an <mxfile> whose <diagram> element is self-closing or whitespace-only (e.g. '<diagram id="x" name="p"/>'), or a diagram whose content was stripped during processing.

Common situations: Hand-constructed mxfile XML that adds the diagram element but forgets the encoded body; sanitizers/XML transforms that drop text nodes; empty placeholder pages created by string templating.

Related errors


AI-assisted analysis of DayuanJiang/next-ai-draw-io@155ef4f7ac (2026-08-27). Data as JSON: /api/errors/a731173693682bea. Report an issue: GitHub.