@pdfme/ui 5.4.3 → 5.4.4

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.
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdfme/ui",
3
- "version": "5.4.3",
3
+ "version": "5.4.4",
4
4
  "sideEffects": false,
5
5
  "author": "hand-dot",
6
6
  "license": "MIT",
@@ -41,6 +41,7 @@
41
41
  "@pdfme/converter": "*",
42
42
  "@scena/react-guides": "^0.28.2",
43
43
  "antd": "^5.26.3",
44
+ "dompurify": "^3.2.6",
44
45
  "form-render": "^2.5.3",
45
46
  "globrex": "^0.1.2",
46
47
  "hotkeys-js": "^3.13.14",
@@ -56,6 +57,7 @@
56
57
  "@pdfme/schemas": "*",
57
58
  "@testing-library/jest-dom": "^6.6.3",
58
59
  "@testing-library/react": "^12.1.2",
60
+ "@types/dompurify": "^3.0.5",
59
61
  "@types/jest": "^30.0.0",
60
62
  "@types/react": "^17.0.52",
61
63
  "@types/react-dom": "^17.0.18",
@@ -1,7 +1,8 @@
1
- import React, { useContext } from 'react';
1
+ import React, { useContext, useMemo } from 'react';
2
2
  import { Plugin, Schema } from '@pdfme/common';
3
3
  import { OptionsContext } from '../../contexts.js';
4
4
  import { theme } from 'antd';
5
+ import DOMPurify from 'dompurify';
5
6
 
6
7
  interface PluginIconProps {
7
8
  plugin: Plugin<Schema>;
@@ -10,27 +11,50 @@ interface PluginIconProps {
10
11
  styles?: React.CSSProperties;
11
12
  }
12
13
 
13
- const getWithModifiedSize = (
14
- htmlString: string,
15
- label: string,
16
- size: number,
17
- styles?: React.CSSProperties,
18
- ) => {
19
- const parser = new DOMParser();
20
- const doc = parser.parseFromString(htmlString, 'text/html');
14
+ const SVGIcon = ({ svgString, size, styles, label }: {
15
+ svgString: string;
16
+ size?: number;
17
+ styles?: React.CSSProperties;
18
+ label: string;
19
+ }) => {
20
+ const processedSVG = useMemo(() => {
21
+ // First sanitize the SVG string using DOMPurify with SVG profile
22
+ const sanitizedSVG = DOMPurify.sanitize(svgString, {
23
+ USE_PROFILES: { svg: true, svgFilters: true },
24
+ ALLOWED_TAGS: ['svg', 'path', 'circle', 'rect', 'line', 'polygon', 'polyline', 'ellipse', 'g', 'defs', 'title', 'desc', 'metadata'],
25
+ ALLOWED_ATTR: ['class', 'id', 'fill', 'stroke', 'stroke-width', 'viewBox', 'width', 'height', 'd', 'cx', 'cy', 'r', 'x', 'y', 'x1', 'y1', 'x2', 'y2', 'points', 'rx', 'ry', 'transform'],
26
+ FORBID_TAGS: ['script', 'foreignObject', 'use', 'embed', 'iframe', 'object', 'link', 'style'],
27
+ FORBID_ATTR: ['onload', 'onerror', 'onclick', 'onmouseover', 'onfocus', 'onblur', 'href', 'xlink:href', 'src', 'action', 'formaction'],
28
+ KEEP_CONTENT: false
29
+ });
21
30
 
22
- const modifyNode = (node: HTMLElement) => {
23
- if (node.tagName === 'SVG' || node.tagName === 'svg') {
24
- node.setAttribute('width', size.toString());
25
- node.setAttribute('height', size.toString());
31
+ const parser = new DOMParser();
32
+ const doc = parser.parseFromString(sanitizedSVG, 'image/svg+xml');
33
+
34
+ const svgElement = doc.querySelector('svg');
35
+ if (!svgElement) {
36
+ return null;
37
+ }
38
+
39
+ // Apply size attributes if specified
40
+ if (size) {
41
+ svgElement.setAttribute('width', size.toString());
42
+ svgElement.setAttribute('height', size.toString());
26
43
  }
27
- Array.from(node.children).forEach((child) => modifyNode(child as HTMLElement));
28
- };
29
44
 
30
- Array.from(doc.body.children).forEach((child) => modifyNode(child as HTMLElement));
45
+ return svgElement.outerHTML;
46
+ }, [svgString, size]);
47
+
48
+ if (!processedSVG) {
49
+ return null;
50
+ }
31
51
 
32
52
  return (
33
- <div style={styles} title={label} dangerouslySetInnerHTML={{ __html: doc.body.innerHTML }} />
53
+ <div
54
+ style={styles}
55
+ title={label}
56
+ dangerouslySetInnerHTML={{ __html: processedSVG }}
57
+ />
34
58
  );
35
59
  };
36
60
 
@@ -50,10 +74,7 @@ const PluginIcon = (props: PluginIconProps) => {
50
74
  };
51
75
 
52
76
  if (icon) {
53
- if (size) {
54
- return getWithModifiedSize(icon, label, size, iconStyles);
55
- }
56
- return <div style={iconStyles} title={label} dangerouslySetInnerHTML={{ __html: icon }} />;
77
+ return <SVGIcon svgString={icon} size={size} styles={iconStyles} label={label} />;
57
78
  }
58
79
 
59
80
  return (