@svgflex/core 0.1.0 → 0.1.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/README.md +32 -3
- package/package.json +1 -1
- package/types/svgflex-config.d.ts +7 -0
- package/types/svgflex-config.d.ts.map +1 -1
- package/utils/svg-config-processor.d.ts.map +1 -1
- package/utils/svg-config-processor.js +4 -2
- package/utils/svg-sanitizer.d.ts +10 -1
- package/utils/svg-sanitizer.d.ts.map +1 -1
- package/utils/svg-sanitizer.js +23 -1
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ This package is intended to be used by framework-specific implementations (e.g.,
|
|
|
10
10
|
|
|
11
11
|
- Framework-agnostic type definitions
|
|
12
12
|
- SVG sanitization utilities (removes scripts and event handlers)
|
|
13
|
+
- Automatic color replacement for hardcoded SVG colors
|
|
13
14
|
- Configuration processors for size and color handling
|
|
14
15
|
- Shared utilities for SVG manipulation
|
|
15
16
|
|
|
@@ -54,15 +55,43 @@ Processes SVG configuration and returns normalized size and color values.
|
|
|
54
55
|
}
|
|
55
56
|
```
|
|
56
57
|
|
|
57
|
-
### `sanitizeSvg(svgContent: string): string`
|
|
58
|
+
### `sanitizeSvg(svgContent: string, replaceColors?: boolean): string`
|
|
58
59
|
|
|
59
|
-
Sanitizes SVG content by removing potentially dangerous elements and attributes.
|
|
60
|
+
Sanitizes SVG content by removing potentially dangerous elements and attributes. Optionally replaces hardcoded colors with `currentColor` to enable dynamic color control.
|
|
60
61
|
|
|
61
62
|
**Parameters:**
|
|
62
63
|
- `svgContent` - Raw SVG string
|
|
64
|
+
- `replaceColors` - Whether to replace hardcoded colors with `currentColor` (default: `false`)
|
|
63
65
|
|
|
64
66
|
**Returns:**
|
|
65
|
-
- Sanitized SVG string with scripts and event handlers removed
|
|
67
|
+
- Sanitized SVG string with scripts and event handlers removed, and optionally with colors replaced
|
|
68
|
+
|
|
69
|
+
**Example:**
|
|
70
|
+
```typescript
|
|
71
|
+
// Basic sanitization
|
|
72
|
+
const safeSvg = sanitizeSvg(rawSvgContent);
|
|
73
|
+
|
|
74
|
+
// Sanitization with color replacement
|
|
75
|
+
const safeSvgWithColors = sanitizeSvg(rawSvgContent, true);
|
|
76
|
+
// This will convert fill="#4E7079" to fill="currentColor"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### `replaceColorsWithCurrentColor(svgContent: string): string`
|
|
80
|
+
|
|
81
|
+
Replaces all hardcoded colors in SVG attributes (`fill` and `stroke`) with `currentColor`, while preserving `none`, `transparent`, `currentColor`, and `url()` references.
|
|
82
|
+
|
|
83
|
+
**Parameters:**
|
|
84
|
+
- `svgContent` - SVG string with hardcoded colors
|
|
85
|
+
|
|
86
|
+
**Returns:**
|
|
87
|
+
- SVG string with colors replaced by `currentColor`
|
|
88
|
+
|
|
89
|
+
**Example:**
|
|
90
|
+
```typescript
|
|
91
|
+
const svgWithHardcodedColor = '<svg><path fill="#FF0000" stroke="blue" /></svg>';
|
|
92
|
+
const svgWithCurrentColor = replaceColorsWithCurrentColor(svgWithHardcodedColor);
|
|
93
|
+
// Result: '<svg><path fill="currentColor" stroke="currentColor" /></svg>'
|
|
94
|
+
```
|
|
66
95
|
|
|
67
96
|
## Usage in Framework Implementations
|
|
68
97
|
|
package/package.json
CHANGED
|
@@ -44,6 +44,12 @@ export interface SvgFlexConfig {
|
|
|
44
44
|
* When true, fetches and embeds the SVG content for full style control
|
|
45
45
|
*/
|
|
46
46
|
inline?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Whether to replace hardcoded colors with currentColor (default: true)
|
|
49
|
+
* When true, automatically replaces fill and stroke attributes with currentColor
|
|
50
|
+
* to allow the 'color' property to work even with SVGs that have hardcoded colors
|
|
51
|
+
*/
|
|
52
|
+
replaceColors?: boolean;
|
|
47
53
|
}
|
|
48
54
|
/**
|
|
49
55
|
* Internal processed configuration
|
|
@@ -56,5 +62,6 @@ export interface ProcessedSvgConfig {
|
|
|
56
62
|
class: string;
|
|
57
63
|
ariaLabel?: string;
|
|
58
64
|
inline: boolean;
|
|
65
|
+
replaceColors: boolean;
|
|
59
66
|
}
|
|
60
67
|
//# sourceMappingURL=svgflex-config.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svgflex-config.d.ts","sourceRoot":"","sources":["../../src/types/svgflex-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE/D;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG;IACtC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE9B;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,KAAK,CAAC,EAAE,QAAQ,CAAC;IAEjB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"svgflex-config.d.ts","sourceRoot":"","sources":["../../src/types/svgflex-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE/D;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG;IACtC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE9B;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,KAAK,CAAC,EAAE,QAAQ,CAAC;IAEjB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;CACxB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svg-config-processor.d.ts","sourceRoot":"","sources":["../../src/utils/svg-config-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"svg-config-processor.d.ts","sourceRoot":"","sources":["../../src/utils/svg-config-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAarF;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAuBxF;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,GAAG,kBAAkB,CAa1E"}
|
|
@@ -5,7 +5,8 @@ const DEFAULT_CONFIG = {
|
|
|
5
5
|
color: 'currentColor',
|
|
6
6
|
size: 24,
|
|
7
7
|
inline: true,
|
|
8
|
-
class: ''
|
|
8
|
+
class: '',
|
|
9
|
+
replaceColors: true
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
12
|
* Processes size value into width and height strings
|
|
@@ -41,6 +42,7 @@ export function processSvgConfig(config) {
|
|
|
41
42
|
height,
|
|
42
43
|
class: config.class || DEFAULT_CONFIG.class,
|
|
43
44
|
ariaLabel: config.ariaLabel,
|
|
44
|
-
inline: config.inline !== undefined ? config.inline : DEFAULT_CONFIG.inline
|
|
45
|
+
inline: config.inline !== undefined ? config.inline : DEFAULT_CONFIG.inline,
|
|
46
|
+
replaceColors: config.replaceColors !== undefined ? config.replaceColors : DEFAULT_CONFIG.replaceColors
|
|
45
47
|
};
|
|
46
48
|
}
|
package/utils/svg-sanitizer.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaces hardcoded colors in SVG with currentColor
|
|
3
|
+
* This allows the SVG to inherit color from CSS
|
|
4
|
+
*
|
|
5
|
+
* @param svgContent - SVG content with hardcoded colors
|
|
6
|
+
* @returns SVG content with currentColor
|
|
7
|
+
*/
|
|
8
|
+
export declare function replaceColorsWithCurrentColor(svgContent: string): string;
|
|
1
9
|
/**
|
|
2
10
|
* Sanitizes SVG content by removing potentially harmful elements
|
|
3
11
|
*
|
|
4
12
|
* @param svgContent - Raw SVG content
|
|
13
|
+
* @param replaceColors - Whether to replace hardcoded colors with currentColor (default: false)
|
|
5
14
|
* @returns Sanitized SVG content
|
|
6
15
|
*/
|
|
7
|
-
export declare function sanitizeSvg(svgContent: string): string;
|
|
16
|
+
export declare function sanitizeSvg(svgContent: string, replaceColors?: boolean): string;
|
|
8
17
|
//# sourceMappingURL=svg-sanitizer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svg-sanitizer.d.ts","sourceRoot":"","sources":["../../src/utils/svg-sanitizer.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"svg-sanitizer.d.ts","sourceRoot":"","sources":["../../src/utils/svg-sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAkBxE;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,GAAE,OAAe,GAAG,MAAM,CAqBtF"}
|
package/utils/svg-sanitizer.js
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaces hardcoded colors in SVG with currentColor
|
|
3
|
+
* This allows the SVG to inherit color from CSS
|
|
4
|
+
*
|
|
5
|
+
* @param svgContent - SVG content with hardcoded colors
|
|
6
|
+
* @returns SVG content with currentColor
|
|
7
|
+
*/
|
|
8
|
+
export function replaceColorsWithCurrentColor(svgContent) {
|
|
9
|
+
let result = svgContent;
|
|
10
|
+
// Replace fill attributes with hex colors, rgb, rgba, named colors, etc.
|
|
11
|
+
// But preserve fill="none" and fill="transparent"
|
|
12
|
+
result = result.replace(/fill\s*=\s*["'](?!none["']|transparent["']|currentColor["']|url\()/gi, 'fill="currentColor');
|
|
13
|
+
// Replace stroke attributes with hex colors, rgb, rgba, named colors, etc.
|
|
14
|
+
// But preserve stroke="none" and stroke="transparent"
|
|
15
|
+
result = result.replace(/stroke\s*=\s*["'](?!none["']|transparent["']|currentColor["']|url\()/gi, 'stroke="currentColor');
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
1
18
|
/**
|
|
2
19
|
* Sanitizes SVG content by removing potentially harmful elements
|
|
3
20
|
*
|
|
4
21
|
* @param svgContent - Raw SVG content
|
|
22
|
+
* @param replaceColors - Whether to replace hardcoded colors with currentColor (default: false)
|
|
5
23
|
* @returns Sanitized SVG content
|
|
6
24
|
*/
|
|
7
|
-
export function sanitizeSvg(svgContent) {
|
|
25
|
+
export function sanitizeSvg(svgContent, replaceColors = false) {
|
|
8
26
|
// Remove script tags and event handlers for security
|
|
9
27
|
let sanitized = svgContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
|
|
10
28
|
// Remove common event handlers
|
|
@@ -16,5 +34,9 @@ export function sanitizeSvg(svgContent) {
|
|
|
16
34
|
const regex = new RegExp(`${handler}\\s*=\\s*["'][^"']*["']`, 'gi');
|
|
17
35
|
sanitized = sanitized.replace(regex, '');
|
|
18
36
|
});
|
|
37
|
+
// Replace hardcoded colors if requested
|
|
38
|
+
if (replaceColors) {
|
|
39
|
+
sanitized = replaceColorsWithCurrentColor(sanitized);
|
|
40
|
+
}
|
|
19
41
|
return sanitized;
|
|
20
42
|
}
|