@svgflex/core 0.1.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.
- package/LICENSE +21 -0
- package/README.md +84 -0
- package/index.d.ts +8 -0
- package/index.d.ts.map +1 -0
- package/index.js +9 -0
- package/package.json +46 -0
- package/types/svgflex-config.d.ts +60 -0
- package/types/svgflex-config.d.ts.map +1 -0
- package/types/svgflex-config.js +1 -0
- package/utils/svg-config-processor.d.ts +13 -0
- package/utils/svg-config-processor.d.ts.map +1 -0
- package/utils/svg-config-processor.js +46 -0
- package/utils/svg-sanitizer.d.ts +8 -0
- package/utils/svg-sanitizer.d.ts.map +1 -0
- package/utils/svg-sanitizer.js +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kosuke Shimizu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# @svgflex/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic core library for SVGFlex. This package contains shared types, utilities, and sanitization logic used across all SVGFlex framework implementations.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package is intended to be used by framework-specific implementations (e.g., `@svgflex/angular`, `@svgflex/react`, `@svgflex/vue`) and is not typically installed directly by end users.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Framework-agnostic type definitions
|
|
12
|
+
- SVG sanitization utilities (removes scripts and event handlers)
|
|
13
|
+
- Configuration processors for size and color handling
|
|
14
|
+
- Shared utilities for SVG manipulation
|
|
15
|
+
|
|
16
|
+
## Exports
|
|
17
|
+
|
|
18
|
+
### Types
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import type {
|
|
22
|
+
SvgFlexConfig,
|
|
23
|
+
SvgSize,
|
|
24
|
+
SvgColor,
|
|
25
|
+
SizeConfig
|
|
26
|
+
} from '@svgflex/core';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Utilities
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import {
|
|
33
|
+
processSvgConfig,
|
|
34
|
+
sanitizeSvg
|
|
35
|
+
} from '@svgflex/core';
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API
|
|
39
|
+
|
|
40
|
+
### `processSvgConfig(config: SvgFlexConfig)`
|
|
41
|
+
|
|
42
|
+
Processes SVG configuration and returns normalized size and color values.
|
|
43
|
+
|
|
44
|
+
**Parameters:**
|
|
45
|
+
- `config.size` - Size value (number, string, or {width, height} object)
|
|
46
|
+
- `config.color` - Color value (CSS color string)
|
|
47
|
+
|
|
48
|
+
**Returns:**
|
|
49
|
+
```typescript
|
|
50
|
+
{
|
|
51
|
+
width: string | null;
|
|
52
|
+
height: string | null;
|
|
53
|
+
color: string;
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `sanitizeSvg(svgContent: string): string`
|
|
58
|
+
|
|
59
|
+
Sanitizes SVG content by removing potentially dangerous elements and attributes.
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
- `svgContent` - Raw SVG string
|
|
63
|
+
|
|
64
|
+
**Returns:**
|
|
65
|
+
- Sanitized SVG string with scripts and event handlers removed
|
|
66
|
+
|
|
67
|
+
## Usage in Framework Implementations
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { sanitizeSvg, processSvgConfig } from '@svgflex/core';
|
|
71
|
+
|
|
72
|
+
// Sanitize SVG content
|
|
73
|
+
const safeSvg = sanitizeSvg(rawSvgContent);
|
|
74
|
+
|
|
75
|
+
// Process configuration
|
|
76
|
+
const { width, height, color } = processSvgConfig({
|
|
77
|
+
size: '24px',
|
|
78
|
+
color: 'currentColor'
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,cAAc,wBAAwB,CAAC;AAGvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC"}
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@svgflex/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic core utilities for SVGFlex",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc && cp package.json README.md ../../LICENSE dist/",
|
|
10
|
+
"watch": "tsc --watch"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"svg",
|
|
14
|
+
"icons",
|
|
15
|
+
"framework-agnostic",
|
|
16
|
+
"utilities",
|
|
17
|
+
"customizable"
|
|
18
|
+
],
|
|
19
|
+
"author": "Kosuke Shimizu",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/KosukeShimizu/svgflex.git",
|
|
24
|
+
"directory": "packages/core"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/KosukeShimizu/svgflex#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/KosukeShimizu/svgflex/issues"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"*.js",
|
|
35
|
+
"*.d.ts",
|
|
36
|
+
"*.map",
|
|
37
|
+
"types",
|
|
38
|
+
"utils",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": false,
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "~5.8.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported size units for SVG dimensions
|
|
3
|
+
*/
|
|
4
|
+
export type SizeUnit = 'px' | 'rem' | 'em' | '%' | 'vw' | 'vh';
|
|
5
|
+
/**
|
|
6
|
+
* Size configuration for SVG
|
|
7
|
+
* Can be a number (interpreted as px), a string with units, or an object with width/height
|
|
8
|
+
*/
|
|
9
|
+
export type SvgSize = number | string | {
|
|
10
|
+
width: number | string;
|
|
11
|
+
height: number | string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Color configuration for SVG
|
|
15
|
+
* Supports CSS color values including currentColor
|
|
16
|
+
*/
|
|
17
|
+
export type SvgColor = string;
|
|
18
|
+
/**
|
|
19
|
+
* Configuration interface for SVGFlex
|
|
20
|
+
*/
|
|
21
|
+
export interface SvgFlexConfig {
|
|
22
|
+
/**
|
|
23
|
+
* Path or URL to the SVG file
|
|
24
|
+
*/
|
|
25
|
+
src: string;
|
|
26
|
+
/**
|
|
27
|
+
* Color for the SVG (defaults to 'currentColor' for CSS inheritance)
|
|
28
|
+
*/
|
|
29
|
+
color?: SvgColor;
|
|
30
|
+
/**
|
|
31
|
+
* Size for the SVG
|
|
32
|
+
*/
|
|
33
|
+
size?: SvgSize;
|
|
34
|
+
/**
|
|
35
|
+
* Additional CSS classes to apply
|
|
36
|
+
*/
|
|
37
|
+
class?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Accessibility label
|
|
40
|
+
*/
|
|
41
|
+
ariaLabel?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Whether to inline the SVG content (default: true)
|
|
44
|
+
* When true, fetches and embeds the SVG content for full style control
|
|
45
|
+
*/
|
|
46
|
+
inline?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Internal processed configuration
|
|
50
|
+
*/
|
|
51
|
+
export interface ProcessedSvgConfig {
|
|
52
|
+
src: string;
|
|
53
|
+
color: string;
|
|
54
|
+
width: string;
|
|
55
|
+
height: string;
|
|
56
|
+
class: string;
|
|
57
|
+
ariaLabel?: string;
|
|
58
|
+
inline: boolean;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=svgflex-config.d.ts.map
|
|
@@ -0,0 +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;CAClB;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;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SvgFlexConfig, ProcessedSvgConfig, SvgSize } from '../types/svgflex-config';
|
|
2
|
+
/**
|
|
3
|
+
* Processes size value into width and height strings
|
|
4
|
+
*/
|
|
5
|
+
export declare function processSize(size: SvgSize | undefined): {
|
|
6
|
+
width: string;
|
|
7
|
+
height: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Processes the SVGFlex configuration into a standardized format
|
|
11
|
+
*/
|
|
12
|
+
export declare function processSvgConfig(config: SvgFlexConfig): ProcessedSvgConfig;
|
|
13
|
+
//# sourceMappingURL=svg-config-processor.d.ts.map
|
|
@@ -0,0 +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;AAYrF;;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,CAY1E"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default configuration values
|
|
3
|
+
*/
|
|
4
|
+
const DEFAULT_CONFIG = {
|
|
5
|
+
color: 'currentColor',
|
|
6
|
+
size: 24,
|
|
7
|
+
inline: true,
|
|
8
|
+
class: ''
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Processes size value into width and height strings
|
|
12
|
+
*/
|
|
13
|
+
export function processSize(size) {
|
|
14
|
+
const defaultSize = `${DEFAULT_CONFIG.size}px`;
|
|
15
|
+
if (!size) {
|
|
16
|
+
return { width: defaultSize, height: defaultSize };
|
|
17
|
+
}
|
|
18
|
+
// If it's a number, convert to px
|
|
19
|
+
if (typeof size === 'number') {
|
|
20
|
+
const sizeStr = `${size}px`;
|
|
21
|
+
return { width: sizeStr, height: sizeStr };
|
|
22
|
+
}
|
|
23
|
+
// If it's a string, use it for both width and height
|
|
24
|
+
if (typeof size === 'string') {
|
|
25
|
+
return { width: size, height: size };
|
|
26
|
+
}
|
|
27
|
+
// If it's an object with width/height
|
|
28
|
+
const width = typeof size.width === 'number' ? `${size.width}px` : size.width;
|
|
29
|
+
const height = typeof size.height === 'number' ? `${size.height}px` : size.height;
|
|
30
|
+
return { width, height };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Processes the SVGFlex configuration into a standardized format
|
|
34
|
+
*/
|
|
35
|
+
export function processSvgConfig(config) {
|
|
36
|
+
const { width, height } = processSize(config.size);
|
|
37
|
+
return {
|
|
38
|
+
src: config.src,
|
|
39
|
+
color: config.color || DEFAULT_CONFIG.color,
|
|
40
|
+
width,
|
|
41
|
+
height,
|
|
42
|
+
class: config.class || DEFAULT_CONFIG.class,
|
|
43
|
+
ariaLabel: config.ariaLabel,
|
|
44
|
+
inline: config.inline !== undefined ? config.inline : DEFAULT_CONFIG.inline
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sanitizes SVG content by removing potentially harmful elements
|
|
3
|
+
*
|
|
4
|
+
* @param svgContent - Raw SVG content
|
|
5
|
+
* @returns Sanitized SVG content
|
|
6
|
+
*/
|
|
7
|
+
export declare function sanitizeSvg(svgContent: string): string;
|
|
8
|
+
//# sourceMappingURL=svg-sanitizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svg-sanitizer.d.ts","sourceRoot":"","sources":["../../src/utils/svg-sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAgBtD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sanitizes SVG content by removing potentially harmful elements
|
|
3
|
+
*
|
|
4
|
+
* @param svgContent - Raw SVG content
|
|
5
|
+
* @returns Sanitized SVG content
|
|
6
|
+
*/
|
|
7
|
+
export function sanitizeSvg(svgContent) {
|
|
8
|
+
// Remove script tags and event handlers for security
|
|
9
|
+
let sanitized = svgContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
|
|
10
|
+
// Remove common event handlers
|
|
11
|
+
const eventHandlers = [
|
|
12
|
+
'onclick', 'onload', 'onerror', 'onmouseover', 'onmouseout',
|
|
13
|
+
'onmousemove', 'onmousedown', 'onmouseup', 'onfocus', 'onblur'
|
|
14
|
+
];
|
|
15
|
+
eventHandlers.forEach(handler => {
|
|
16
|
+
const regex = new RegExp(`${handler}\\s*=\\s*["'][^"']*["']`, 'gi');
|
|
17
|
+
sanitized = sanitized.replace(regex, '');
|
|
18
|
+
});
|
|
19
|
+
return sanitized;
|
|
20
|
+
}
|