@pairdraw/drawio 0.16.1 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { imageSize } from "image-size";
2
+ import { decode, isValid } from "js-base64";
2
3
  import { Inflate } from "fflate";
3
4
  import { DOMParser } from "@xmldom/xmldom";
4
5
  //#region src/inflate.ts
@@ -154,7 +155,15 @@ function drawioRoot(bytes, budget) {
154
155
  const root = source === null ? null : parseXML(source);
155
156
  if (root?.localName !== "svg") return root;
156
157
  const content = root.getAttribute("content");
157
- return content ? parseXML(content) : null;
158
+ return content ? parseXML(svgContent(content)) : null;
159
+ }
160
+ /**
161
+ * The diagram an SVG export keeps in its `content` attribute. draw.io writes it as XML, and also
162
+ * reads it percent-encoded or as Base64 of either form.
163
+ */
164
+ function svgContent(content) {
165
+ const text = content.startsWith("<") || content.startsWith("%") || !isValid(content) ? content : decode(content);
166
+ return text.startsWith("%") ? percentDecoded(text) : text;
158
167
  }
159
168
  /** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */
160
169
  function isDrawIOExportFileName(fileName) {
@@ -1 +1 @@
1
- {"version":3,"file":"embedded.js","names":[],"sources":["../../src/inflate.ts","../../src/xml.ts","../../src/embedded.ts"],"sourcesContent":["import { Inflate } from 'fflate'\n\n/**\n * The most a file's compressed parts may unpack to, all together, so a small crafted file with\n * many diagrams or image chunks cannot exhaust memory.\n */\nexport const MAX_INFLATED_BYTES = 32 * 1024 * 1024\n/** Input fed per step; deflate expands at most about 1032 times, so each step stays small. */\nconst STEP_BYTES = 4096\n\n/** What is left of a file's unpacking allowance, shared by every part it unpacks. */\nexport interface InflateBudget {\n remaining: number\n}\n\nexport function inflateBudget(bytes = MAX_INFLATED_BYTES): InflateBudget {\n return { remaining: bytes }\n}\n\n/** Raw deflate, charged to `budget`; throws once the file would unpack to more than it allows. */\nexport function boundedInflate(data: Uint8Array, budget: InflateBudget): Uint8Array {\n const chunks: Uint8Array[] = []\n let size = 0\n const inflater = new Inflate((chunk) => {\n size += chunk.length\n budget.remaining -= chunk.length\n if (budget.remaining < 0) throw new RangeError('The draw.io file unpacks to too much data')\n chunks.push(chunk)\n })\n for (let start = 0; start < data.length; start += STEP_BYTES) {\n const end = start + STEP_BYTES\n inflater.push(data.subarray(start, end), end >= data.length)\n }\n const output = new Uint8Array(size)\n let offset = 0\n for (const chunk of chunks) {\n output.set(chunk, offset)\n offset += chunk.length\n }\n return output\n}\n\n/** A `%` that does not start an escape, as in \"50% done\". */\nconst STRAY_PERCENT = /%(?![0-9A-Fa-f]{2})/g\n/** A run of escapes, decoded together so a character spread over several bytes stays whole. */\nconst ESCAPE_RUN = /(?:%[0-9A-Fa-f]{2})+/g\n\n/**\n * The text with its percent escapes decoded. A stray `%` is kept as it is, and an escape run that\n * is not valid UTF-8 stays encoded, so neither stops the rest of the text from decoding.\n */\nexport function percentDecoded(text: string): string {\n const escaped = text.replace(STRAY_PERCENT, '%25')\n try {\n return decodeURIComponent(escaped)\n } catch {\n return escaped.replace(ESCAPE_RUN, (run) => {\n try {\n return decodeURIComponent(run)\n } catch {\n return run\n }\n })\n }\n}\n","import { DOMParser, type Element } from '@xmldom/xmldom'\n\n/** The root element of `source`, or null when it is not well-formed XML. */\nexport function parseXML(source: string): Element | null {\n try {\n const parsed = new DOMParser({\n onError: (level, message) => {\n if (level !== 'warning') throw new Error(message)\n }\n }).parseFromString(source, 'text/xml')\n return parsed.documentElement\n } catch {\n return null\n }\n}\n\n/** The element children of `element`, named `name` when given. */\nexport function childElements(element: Element, name?: string): Element[] {\n const found: Element[] = []\n for (let node = element.firstChild; node; node = node.nextSibling) {\n if (node.nodeType === node.ELEMENT_NODE && (!name || (node as Element).localName === name)) {\n found.push(node as Element)\n }\n }\n return found\n}\n\n/** All of an element's attributes but those in `except`. */\nexport function attributesOf(\n element: Element,\n except: ReadonlySet<string>\n): Record<string, string> {\n const attributes: Record<string, string> = {}\n for (const attribute of Array.from(element.attributes)) {\n if (!except.has(attribute.name)) attributes[attribute.name] = attribute.value\n }\n return attributes\n}\n","import type { Element } from '@xmldom/xmldom'\nimport { imageSize } from 'image-size'\n\nimport { boundedInflate, percentDecoded, type InflateBudget } from './inflate'\nimport { parseXML } from './xml'\n\n/** A PNG's chunks start after its eight-byte signature. */\nconst PNG_SIGNATURE_LENGTH = 8\n/** The keyword draw.io stores its diagram under in a PNG's text chunks. */\nconst DRAWIO_KEYWORD = 'mxfile'\n\nconst latin1 = new TextDecoder('latin1')\nconst utf8 = new TextDecoder()\n\nfunction isPNG(bytes: Uint8Array): boolean {\n try {\n return imageSize(bytes).type === 'png'\n } catch {\n return false\n }\n}\n\n/** Where a NUL byte ends the field that starts at `from`, or -1. */\nfunction fieldEnd(data: Uint8Array, from: number): number {\n return data.indexOf(0, from)\n}\n\n/**\n * The diagram a text chunk holds when its keyword is draw.io's: `tEXt` as Latin-1, `zTXt`\n * zlib-compressed, `iTXt` as UTF-8, compressed or not. draw.io percent-encodes the XML in all three.\n */\nfunction chunkDiagram(type: string, data: Uint8Array, budget: InflateBudget): string | null {\n const keywordEnd = fieldEnd(data, 0)\n if (keywordEnd === -1 || latin1.decode(data.subarray(0, keywordEnd)) !== DRAWIO_KEYWORD)\n return null\n if (type === 'tEXt') return percentDecoded(latin1.decode(data.subarray(keywordEnd + 1)))\n if (type === 'zTXt') {\n // The compression method byte, then a zlib stream: a 2-byte header before raw deflate.\n const inflated = boundedInflate(data.subarray(keywordEnd + 4), budget)\n return percentDecoded(latin1.decode(inflated))\n }\n // iTXt: compressed flag and method, then language and translated keyword, each NUL-ended.\n const compressed = data[keywordEnd + 1] === 1\n const languageEnd = fieldEnd(data, keywordEnd + 3)\n const translatedEnd = languageEnd === -1 ? -1 : fieldEnd(data, languageEnd + 1)\n if (translatedEnd === -1) return null\n const text = data.subarray(translatedEnd + 1)\n return percentDecoded(utf8.decode(compressed ? boundedInflate(text.subarray(2), budget) : text))\n}\n\n/**\n * The draw.io diagram a PNG export carries, or null when it carries none. A compressed chunk that\n * is damaged or too large to unpack is an error that says so.\n */\nfunction diagramInPNG(bytes: Uint8Array, budget: InflateBudget): string | null {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n let offset = PNG_SIGNATURE_LENGTH\n while (offset + 8 <= bytes.length) {\n const length = view.getUint32(offset)\n const type = latin1.decode(bytes.subarray(offset + 4, offset + 8))\n const start = offset + 8\n if (type === 'IEND') return null\n if (start + length > bytes.length) {\n throw new Error('Could not read the draw.io diagram in this image: it is cut short')\n }\n if (type === 'tEXt' || type === 'zTXt' || type === 'iTXt') {\n let diagram: string | null\n try {\n diagram = chunkDiagram(type, bytes.subarray(start, start + length), budget)\n } catch (error) {\n throw new Error('Could not read the draw.io diagram in this image', { cause: error })\n }\n if (diagram) return diagram\n }\n // The chunk's data, then its 4-byte CRC.\n offset = start + length + 4\n }\n return null\n}\n\n/**\n * The root element of the draw.io XML a file holds: the file itself for `.drawio` and `.dio`, or\n * the diagram a PNG export embeds in a text chunk or an SVG export in its root's `content`. Null\n * when the file is not XML or an export embeds no diagram.\n */\nexport function drawioRoot(bytes: Uint8Array, budget: InflateBudget): Element | null {\n const source = isPNG(bytes) ? diagramInPNG(bytes, budget) : utf8.decode(bytes)\n const root = source === null ? null : parseXML(source)\n if (root?.localName !== 'svg') return root\n const content = root.getAttribute('content')\n return content ? parseXML(content) : null\n}\n\n/** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */\nexport function isDrawIOExportFileName(fileName: string): boolean {\n const name = fileName.toLowerCase()\n return name.endsWith('.drawio.png') || name.endsWith('.drawio.svg')\n}\n"],"mappings":";;;;;;;;AAMA,MAAa,qBAAqB;;AAElC,MAAM,aAAa;AAOnB,SAAgB,cAAc,QAAQ,oBAAmC;CACvE,OAAO,EAAE,WAAW,MAAM;AAC5B;;AAGA,SAAgB,eAAe,MAAkB,QAAmC;CAClF,MAAM,SAAuB,CAAC;CAC9B,IAAI,OAAO;CACX,MAAM,WA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,OAAmB,QAAsC;CAC7E,MAAM,OAAO,IAAI,SAAS,MAAM,QAAQ,MAAM,YAAY,MAAM,UAAU;CAC1E,IAAI,SAAS;CACb,OAAO,SAAS,KAAK,MAAM,QAAQ;EACjC,MAAM,SAAS,KAAK,UAAU,MAAM;EACpC,MAAM,OAAO,OAAO,OAAO,MAAM,SAAS,SAAS,GAAG,SAAS,CAAC,CAAC;EACjE,MAAM,QAAQ,SAAS;EACvB,IAAI,SAAS,QAAQ,OAAO;EAC5B,IAAI,QAAQ,SAAS,MAAM,QACzB,MAAM,IAAI,MAAM,mEAAmE;EAErF,IAAI,SAAS,UAAU,SAAS,UAAU,SAAS,QAAQ;GACzD,IAAI;GACJ,IAAI;IACF,UAAU,aAAa,MAAM,MAAM,SAAS,OAAO,QAAQ,MAAM,GAAG,MAAM;GAC5E,SAAS,OAAO;IACd,MAAM,IAAI,MAAM,oDAAoD,EAAE,OAAO,MAAM,CAAC;GACtF;GACA,IAAI,SAAS,OAAO;EACtB;EAEA,SAAS,QAAQ,SAAS;CAC5B;CACA,OAAO;AACT;;;;;;AAOA,SAAgB,WAAW,OAAmB,QAAuC;CACnF,MAAM,SAAS,MAAM,KAAK,IAAI,aAAa,OAAO,MAAM,IAAI,KAAK,OAAO,KAAK;CAC7E,MAAM,OAAO,WAAW,OAAO,OAAO,SAAS,MAAM;CACrD,IAAI,MAAM,cAAc,OAAO,OAAO;CACtC,MAAM,UAAU,KAAK,aAAa,SAAS;CAC3C,OAAO,UAAU,SAAS,OAAO,IAAI;AACvC;;AAGA,SAAgB,uBAAuB,UAA2B;CAChE,MAAM,OAAO,SAAS,YAAY;CAClC,OAAO,KAAK,SAAS,aAAa,KAAK,KAAK,SAAS,aAAa;AACpE"}
1
+ {"version":3,"file":"embedded.js","names":[],"sources":["../../src/inflate.ts","../../src/xml.ts","../../src/embedded.ts"],"sourcesContent":["import { Inflate } from 'fflate'\n\n/**\n * The most a file's compressed parts may unpack to, all together, so a small crafted file with\n * many diagrams or image chunks cannot exhaust memory.\n */\nexport const MAX_INFLATED_BYTES = 32 * 1024 * 1024\n/** Input fed per step; deflate expands at most about 1032 times, so each step stays small. */\nconst STEP_BYTES = 4096\n\n/** What is left of a file's unpacking allowance, shared by every part it unpacks. */\nexport interface InflateBudget {\n remaining: number\n}\n\nexport function inflateBudget(bytes = MAX_INFLATED_BYTES): InflateBudget {\n return { remaining: bytes }\n}\n\n/** Raw deflate, charged to `budget`; throws once the file would unpack to more than it allows. */\nexport function boundedInflate(data: Uint8Array, budget: InflateBudget): Uint8Array {\n const chunks: Uint8Array[] = []\n let size = 0\n const inflater = new Inflate((chunk) => {\n size += chunk.length\n budget.remaining -= chunk.length\n if (budget.remaining < 0) throw new RangeError('The draw.io file unpacks to too much data')\n chunks.push(chunk)\n })\n for (let start = 0; start < data.length; start += STEP_BYTES) {\n const end = start + STEP_BYTES\n inflater.push(data.subarray(start, end), end >= data.length)\n }\n const output = new Uint8Array(size)\n let offset = 0\n for (const chunk of chunks) {\n output.set(chunk, offset)\n offset += chunk.length\n }\n return output\n}\n\n/** A `%` that does not start an escape, as in \"50% done\". */\nconst STRAY_PERCENT = /%(?![0-9A-Fa-f]{2})/g\n/** A run of escapes, decoded together so a character spread over several bytes stays whole. */\nconst ESCAPE_RUN = /(?:%[0-9A-Fa-f]{2})+/g\n\n/**\n * The text with its percent escapes decoded. A stray `%` is kept as it is, and an escape run that\n * is not valid UTF-8 stays encoded, so neither stops the rest of the text from decoding.\n */\nexport function percentDecoded(text: string): string {\n const escaped = text.replace(STRAY_PERCENT, '%25')\n try {\n return decodeURIComponent(escaped)\n } catch {\n return escaped.replace(ESCAPE_RUN, (run) => {\n try {\n return decodeURIComponent(run)\n } catch {\n return run\n }\n })\n }\n}\n","import { DOMParser, type Element } from '@xmldom/xmldom'\n\n/** The root element of `source`, or null when it is not well-formed XML. */\nexport function parseXML(source: string): Element | null {\n try {\n const parsed = new DOMParser({\n onError: (level, message) => {\n if (level !== 'warning') throw new Error(message)\n }\n }).parseFromString(source, 'text/xml')\n return parsed.documentElement\n } catch {\n return null\n }\n}\n\n/** The element children of `element`, named `name` when given. */\nexport function childElements(element: Element, name?: string): Element[] {\n const found: Element[] = []\n for (let node = element.firstChild; node; node = node.nextSibling) {\n if (node.nodeType === node.ELEMENT_NODE && (!name || (node as Element).localName === name)) {\n found.push(node as Element)\n }\n }\n return found\n}\n\n/** All of an element's attributes but those in `except`. */\nexport function attributesOf(\n element: Element,\n except: ReadonlySet<string>\n): Record<string, string> {\n const attributes: Record<string, string> = {}\n for (const attribute of Array.from(element.attributes)) {\n if (!except.has(attribute.name)) attributes[attribute.name] = attribute.value\n }\n return attributes\n}\n","import type { Element } from '@xmldom/xmldom'\nimport { imageSize } from 'image-size'\nimport { decode, isValid } from 'js-base64'\n\nimport { boundedInflate, percentDecoded, type InflateBudget } from './inflate'\nimport { parseXML } from './xml'\n\n/** A PNG's chunks start after its eight-byte signature. */\nconst PNG_SIGNATURE_LENGTH = 8\n/** The keyword draw.io stores its diagram under in a PNG's text chunks. */\nconst DRAWIO_KEYWORD = 'mxfile'\n\nconst latin1 = new TextDecoder('latin1')\nconst utf8 = new TextDecoder()\n\nfunction isPNG(bytes: Uint8Array): boolean {\n try {\n return imageSize(bytes).type === 'png'\n } catch {\n return false\n }\n}\n\n/** Where a NUL byte ends the field that starts at `from`, or -1. */\nfunction fieldEnd(data: Uint8Array, from: number): number {\n return data.indexOf(0, from)\n}\n\n/**\n * The diagram a text chunk holds when its keyword is draw.io's: `tEXt` as Latin-1, `zTXt`\n * zlib-compressed, `iTXt` as UTF-8, compressed or not. draw.io percent-encodes the XML in all three.\n */\nfunction chunkDiagram(type: string, data: Uint8Array, budget: InflateBudget): string | null {\n const keywordEnd = fieldEnd(data, 0)\n if (keywordEnd === -1 || latin1.decode(data.subarray(0, keywordEnd)) !== DRAWIO_KEYWORD)\n return null\n if (type === 'tEXt') return percentDecoded(latin1.decode(data.subarray(keywordEnd + 1)))\n if (type === 'zTXt') {\n // The compression method byte, then a zlib stream: a 2-byte header before raw deflate.\n const inflated = boundedInflate(data.subarray(keywordEnd + 4), budget)\n return percentDecoded(latin1.decode(inflated))\n }\n // iTXt: compressed flag and method, then language and translated keyword, each NUL-ended.\n const compressed = data[keywordEnd + 1] === 1\n const languageEnd = fieldEnd(data, keywordEnd + 3)\n const translatedEnd = languageEnd === -1 ? -1 : fieldEnd(data, languageEnd + 1)\n if (translatedEnd === -1) return null\n const text = data.subarray(translatedEnd + 1)\n return percentDecoded(utf8.decode(compressed ? boundedInflate(text.subarray(2), budget) : text))\n}\n\n/**\n * The draw.io diagram a PNG export carries, or null when it carries none. A compressed chunk that\n * is damaged or too large to unpack is an error that says so.\n */\nfunction diagramInPNG(bytes: Uint8Array, budget: InflateBudget): string | null {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n let offset = PNG_SIGNATURE_LENGTH\n while (offset + 8 <= bytes.length) {\n const length = view.getUint32(offset)\n const type = latin1.decode(bytes.subarray(offset + 4, offset + 8))\n const start = offset + 8\n if (type === 'IEND') return null\n if (start + length > bytes.length) {\n throw new Error('Could not read the draw.io diagram in this image: it is cut short')\n }\n if (type === 'tEXt' || type === 'zTXt' || type === 'iTXt') {\n let diagram: string | null\n try {\n diagram = chunkDiagram(type, bytes.subarray(start, start + length), budget)\n } catch (error) {\n throw new Error('Could not read the draw.io diagram in this image', { cause: error })\n }\n if (diagram) return diagram\n }\n // The chunk's data, then its 4-byte CRC.\n offset = start + length + 4\n }\n return null\n}\n\n/**\n * The root element of the draw.io XML a file holds: the file itself for `.drawio` and `.dio`, or\n * the diagram a PNG export embeds in a text chunk or an SVG export in its root's `content`. Null\n * when the file is not XML or an export embeds no diagram.\n */\nexport function drawioRoot(bytes: Uint8Array, budget: InflateBudget): Element | null {\n const source = isPNG(bytes) ? diagramInPNG(bytes, budget) : utf8.decode(bytes)\n const root = source === null ? null : parseXML(source)\n if (root?.localName !== 'svg') return root\n const content = root.getAttribute('content')\n return content ? parseXML(svgContent(content)) : null\n}\n\n/**\n * The diagram an SVG export keeps in its `content` attribute. draw.io writes it as XML, and also\n * reads it percent-encoded or as Base64 of either form.\n */\nfunction svgContent(content: string): string {\n const text =\n content.startsWith('<') || content.startsWith('%') || !isValid(content)\n ? content\n : decode(content)\n return text.startsWith('%') ? percentDecoded(text) : text\n}\n\n/** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */\nexport function isDrawIOExportFileName(fileName: string): boolean {\n const name = fileName.toLowerCase()\n return name.endsWith('.drawio.png') || name.endsWith('.drawio.svg')\n}\n"],"mappings":";;;;;;;;;AAMA,MAAa,qBAAqB;;AAElC,MAAM,aAAa;AAOnB,SAAgB,cAAc,QAAQ,oBAAmC;CACvE,OAAO,EAAE,WAAW,MAAM;AAC5B;;AAGA,SAAgB,eAAe,MAAkB,QAAmC;CAClF,MAAM,SAAuB,CAAC;CAC9B,IAAI,OAAO;CACX,MAAM,WAAW,IAAI,SAAS,UAAU;EACtC,QAAQ,MAAM;EACd,OAAO,aAAa,MAAM;EAC1B,IAAI,OAAO,YAAY,GAAG,MAAM,IAAI,WAAW,2CAA2C;EAC1F,OAAO,KAAK,KAAK;CACnB,CAAC;CACD,KAAK,IAAI,QAAQ,GAAG,QAAQ,KAAK,QAAQ,SAAS,YAAY;EAC5D,MAAM,MAAM,QAAQ;EACpB,SAAS,KAAK,KAAK,SAAS,OAAO,GAAG,GAAG,OAAO,KAAK,MAAM;CAC7D;CACA,MAAM,SAAS,IAAI,WAAW,IAAI;CAClC,IAAI,SAAS;CACb,KAAK,MAAM,SAAS,QAAQ;EAC1B,OAAO,IAAI,OAAO,MAAM;EACxB,UAAU,MAAM;CAClB;CACA,OAAO;AACT;;AAGA,MAAM,gBAAgB;;AAEtB,MAAM,aAAa;;;;;AAMnB,SAAgB,eAAe,MAAsB;CACnD,MAAM,UAAU,KAAK,QAAQ,eAAe,KAAK;CACjD,IAAI;EACF,OAAO,mBAAmB,OAAO;CACnC,QAAQ;EACN,OAAO,QAAQ,QAA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;;;;;;AAOA,SAAgB,WAAW,OAAmB,QAAuC;CACnF,MAAM,SAAS,MAAM,KAAK,IAAI,aAAa,OAAO,MAAM,IAAI,KAAK,OAAO,KAAK;CAC7E,MAAM,OAAO,WAAW,OAAO,OAAO,SAAS,MAAM;CACrD,IAAI,MAAM,cAAc,OAAO,OAAO;CACtC,MAAM,UAAU,KAAK,aAAa,SAAS;CAC3C,OAAO,UAAU,SAAS,WAAW,OAAO,CAAC,IAAI;AACnD;;;;;AAMA,SAAS,WAAW,SAAyB;CAC3C,MAAM,OACJ,QAAQ,WAAW,GAAG,KAAK,QAAQ,WAAW,GAAG,KAAK,CAAC,QAAQ,OAAO,IAClE,UACA,OAAO,OAAO;CACpB,OAAO,KAAK,WAAW,GAAG,IAAI,eAAe,IAAI,IAAI;AACvD;;AAGA,SAAgB,uBAAuB,UAA2B;CAChE,MAAM,OAAO,SAAS,YAAY;CAClC,OAAO,KAAK,SAAS,aAAa,KAAK,KAAK,SAAS,aAAa;AACpE"}
@@ -1,6 +1,6 @@
1
1
  import { attributesOf, boundedInflate, childElements, drawioRoot, inflateBudget, parseXML, percentDecoded } from "./embedded.js";
2
- import { DOMImplementation, XMLSerializer } from "@xmldom/xmldom";
3
2
  import { isValid, toUint8Array } from "js-base64";
3
+ import { DOMImplementation, XMLSerializer } from "@xmldom/xmldom";
4
4
  //#region src/style.ts
5
5
  /** A `style` string as its style names and its pairs. */
6
6
  function parseStyle(style) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/model.ts","../src/inflate.ts","../src/embedded.ts","../src/read.ts","../src/style.ts","../src/write.ts","../src/index.ts"],"mappings":";;UACiB;EACf;EACA;;;;;;;;UASe;EACf;EACA;EACA;EACA;EACA;EACA,QAAQ;EACR,aAAa;EACb,aAAa;EACb,QAAQ;;;;;;UAOO;EACf;EACA,QAAQ,QAAQ;;;UAID;EACf;;EAEA;EACA;;EAEA;EACA,OAAO;EACP,UAAU;EACV;EACA;EACA;;;;;EAKA,YAAY;;;UAIG;EACf;EACA;EACA;EACA;;UAGe;EACf;EACA;;EAEA,YAAY;;EAEZ;EACA,QAAQ;EACR,OAAO;;;UAIQ;EACf,OAAO;EACP;;;;;;;;cCrEW;;UAKI;EACf;;;;;iBCkFc,uBAAuB;;;;;;;;iBCoEvB,gBAAgB,gBAAgB,SAAM,gBAAqB;;;;iBC/J3D,WAAW,gBAAgB;;iBAa3B,cAAc,OAAO,UAAU;;iBAQ/B,YAAY,OAAO,aAAa,aAAa;;iBAM7C,UAAU,OAAO,aAAa;;;;;;;;iBCiE9B,gBAAgB,MAAM;;;;;;;;iBCtEtB,gBAAgB,OAAO,aAAa"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/model.ts","../src/inflate.ts","../src/embedded.ts","../src/read.ts","../src/style.ts","../src/write.ts","../src/index.ts"],"mappings":";;UACiB;EACf;EACA;;;;;;;;UASe;EACf;EACA;EACA;EACA;EACA;EACA,QAAQ;EACR,aAAa;EACb,aAAa;EACb,QAAQ;;;;;;UAOO;EACf;EACA,QAAQ,QAAQ;;;UAID;EACf;;EAEA;EACA;;EAEA;EACA,OAAO;EACP,UAAU;EACV;EACA;EACA;;;;;EAKA,YAAY;;;UAIG;EACf;EACA;EACA;EACA;;UAGe;EACf;EACA;;EAEA,YAAY;;EAEZ;EACA,QAAQ;EACR,OAAO;;;UAIQ;EACf,OAAO;EACP;;;;;;;;cCrEW;;UAKI;EACf;;;;;iBC+Fc,uBAAuB;;;;;;;;iBCuDvB,gBAAgB,gBAAgB,SAAM,gBAAqB;;;;iBC/J3D,WAAW,gBAAgB;;iBAa3B,cAAc,OAAO,UAAU;;iBAQ/B,YAAY,OAAO,aAAa,aAAa;;iBAM7C,UAAU,OAAO,aAAa;;;;;;;;iBCiE9B,gBAAgB,MAAM;;;;;;;;iBCtEtB,gBAAgB,OAAO,aAAa"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pairdraw/drawio",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
package/src/embedded.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Element } from '@xmldom/xmldom'
2
2
  import { imageSize } from 'image-size'
3
+ import { decode, isValid } from 'js-base64'
3
4
 
4
5
  import { boundedInflate, percentDecoded, type InflateBudget } from './inflate'
5
6
  import { parseXML } from './xml'
@@ -88,7 +89,19 @@ export function drawioRoot(bytes: Uint8Array, budget: InflateBudget): Element |
88
89
  const root = source === null ? null : parseXML(source)
89
90
  if (root?.localName !== 'svg') return root
90
91
  const content = root.getAttribute('content')
91
- return content ? parseXML(content) : null
92
+ return content ? parseXML(svgContent(content)) : null
93
+ }
94
+
95
+ /**
96
+ * The diagram an SVG export keeps in its `content` attribute. draw.io writes it as XML, and also
97
+ * reads it percent-encoded or as Base64 of either form.
98
+ */
99
+ function svgContent(content: string): string {
100
+ const text =
101
+ content.startsWith('<') || content.startsWith('%') || !isValid(content)
102
+ ? content
103
+ : decode(content)
104
+ return text.startsWith('%') ? percentDecoded(text) : text
92
105
  }
93
106
 
94
107
  /** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */