@dicebear/converter 9.4.0 → 9.4.2

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.
package/lib/node/core.js CHANGED
@@ -39,8 +39,9 @@ async function toArrayBuffer(rawSvg, format, exif, options) {
39
39
  }
40
40
  async function toBuffer(rawSvg, format, exif, options) {
41
41
  const hasFonts = Array.isArray(options.fonts);
42
- const { svg } = ensureSize(rawSvg, options.size);
42
+ const { svg, size } = ensureSize(rawSvg, options.size);
43
43
  let buffer = (await renderAsync(svg, {
44
+ fitTo: { mode: 'width', value: size },
44
45
  font: {
45
46
  loadSystemFonts: !hasFonts,
46
47
  fontFiles: hasFonts ? options.fonts : undefined,
package/lib/utils/svg.js CHANGED
@@ -1,6 +1,17 @@
1
- import { XMLParser } from 'fast-xml-parser';
1
+ import { XMLParser, XMLBuilder } from 'fast-xml-parser';
2
2
  const MAX_SIZE = 2048;
3
3
  const DEFAULT_SIZE = 512;
4
+ const MAX_METADATA_LENGTH = 1024;
5
+ const xmlRoundTripOptions = {
6
+ ignoreAttributes: false,
7
+ attributeNamePrefix: '@_',
8
+ preserveOrder: true,
9
+ commentPropName: '#comment',
10
+ allowBooleanAttributes: true,
11
+ processEntities: false,
12
+ };
13
+ const xmlRoundTripParser = new XMLParser(xmlRoundTripOptions);
14
+ const xmlRoundTripBuilder = new XMLBuilder(xmlRoundTripOptions);
4
15
  function sanitizeSize(size) {
5
16
  if (!Number.isFinite(size) || size <= 0) {
6
17
  return DEFAULT_SIZE;
@@ -8,34 +19,34 @@ function sanitizeSize(size) {
8
19
  return Math.floor(Math.min(size, MAX_SIZE));
9
20
  }
10
21
  export function ensureSize(svg, size = DEFAULT_SIZE) {
22
+ var _a;
11
23
  size = sanitizeSize(size);
12
- svg = svg.replace(/<svg([^>]*)/, (match, g1) => {
13
- if (g1.match(/width="([^"]+)"/)) {
14
- g1 = g1.replace(/width="([^"]+)"/, `width="${size}"`);
15
- }
16
- else {
17
- g1 += ` width="${size}"`;
18
- }
19
- if (g1.match(/height="([^"]+)"/)) {
20
- g1 = g1.replace(/height="([^"]+)"/, `height="${size}"`);
21
- }
22
- else {
23
- g1 += ` height="${size}"`;
24
- }
25
- return `<svg${g1}`;
26
- });
24
+ const parsed = xmlRoundTripParser.parse(svg);
25
+ const svgNode = parsed.find((node) => 'svg' in node);
26
+ if (svgNode) {
27
+ (_a = svgNode[':@']) !== null && _a !== void 0 ? _a : (svgNode[':@'] = {});
28
+ svgNode[':@']['@_width'] = String(size);
29
+ svgNode[':@']['@_height'] = String(size);
30
+ }
31
+ svg = xmlRoundTripBuilder.build(parsed);
27
32
  return { svg, size };
28
33
  }
34
+ function sanitizeMetadataValue(value) {
35
+ if (typeof value !== 'string' || value.length === 0) {
36
+ return undefined;
37
+ }
38
+ return value.slice(0, MAX_METADATA_LENGTH);
39
+ }
29
40
  export function getMetadata(svg) {
30
41
  var _a, _b;
31
42
  const parser = new XMLParser();
32
43
  const xml = parser.parse(svg);
33
44
  const rdfDescription = (_b = (_a = xml.svg.metadata) === null || _a === void 0 ? void 0 : _a['rdf:RDF']) === null || _b === void 0 ? void 0 : _b['rdf:Description'];
34
45
  return {
35
- title: rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:title'],
36
- source: rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:source'],
37
- creator: rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:creator'],
38
- license: rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dcterms:license'],
39
- copyright: rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:rights'],
46
+ title: sanitizeMetadataValue(rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:title']),
47
+ source: sanitizeMetadataValue(rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:source']),
48
+ creator: sanitizeMetadataValue(rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:creator']),
49
+ license: sanitizeMetadataValue(rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dcterms:license']),
50
+ copyright: sanitizeMetadataValue(rdfDescription === null || rdfDescription === void 0 ? void 0 : rdfDescription['dc:rights']),
40
51
  };
41
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/converter",
3
- "version": "9.4.0",
3
+ "version": "9.4.2",
4
4
  "description": "SVG Converter for DiceBear",
5
5
  "keywords": [
6
6
  "dicebear"