css-box-shadow-parser 1.0.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 +154 -0
- package/index.js +352 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HTML Code Generator (https://www.html-code-generator.com/)
|
|
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,154 @@
|
|
|
1
|
+
# css-box-shadow-parser
|
|
2
|
+
|
|
3
|
+
Parse any CSS `box-shadow` value into structured JavaScript objects.
|
|
4
|
+
Works in **Node.js** and the **browser**. Zero dependencies.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install css-box-shadow-parser
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
**CDN (browser)**
|
|
15
|
+
|
|
16
|
+
```html
|
|
17
|
+
<script src="https://unpkg.com/css-box-shadow-parser"></script>
|
|
18
|
+
<script>
|
|
19
|
+
const layers = BoxShadowParser.parse('4px 4px 10px rgba(0,0,0,0.4)');
|
|
20
|
+
</script>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const { parse, parseSingle, split } = require('css-box-shadow-parser');
|
|
29
|
+
|
|
30
|
+
// Single layer
|
|
31
|
+
parse('4px 4px 10px rgba(0,0,0,0.4)');
|
|
32
|
+
|
|
33
|
+
// Multi-layer
|
|
34
|
+
parse('0 1px 2px #0002, 0 4px 8px #0002');
|
|
35
|
+
|
|
36
|
+
// Full CSS declaration — strips "box-shadow:" and ";" automatically
|
|
37
|
+
parse('box-shadow: inset 0 2px 4px rgba(0,0,0,.24);');
|
|
38
|
+
|
|
39
|
+
// CSS keyword → empty array
|
|
40
|
+
parse('none'); // []
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `parse(shadow)` → `BoxShadowLayer[]`
|
|
48
|
+
|
|
49
|
+
Parses a full `box-shadow` value (single or multi-layer).
|
|
50
|
+
Accepts raw values **or** complete CSS declarations.
|
|
51
|
+
|
|
52
|
+
### `parseSingle(token)` → `BoxShadowLayer | null`
|
|
53
|
+
|
|
54
|
+
Parses a single shadow token. Returns `null` for invalid input.
|
|
55
|
+
|
|
56
|
+
### `split(shadow)` → `string[]`
|
|
57
|
+
|
|
58
|
+
Splits a compound value on top-level commas without parsing.
|
|
59
|
+
Commas inside `rgba()`, `hsl()` etc. are correctly ignored.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Return Type — `BoxShadowLayer`
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
{
|
|
67
|
+
inset: boolean // true if shadow uses `inset`
|
|
68
|
+
x: number // horizontal offset (px)
|
|
69
|
+
y: number // vertical offset (px)
|
|
70
|
+
blur: number // blur radius (px)
|
|
71
|
+
spread: number // spread radius (px, can be negative)
|
|
72
|
+
color: string // original CSS color token
|
|
73
|
+
alpha: number // opacity 0–1
|
|
74
|
+
hex: string // resolved 6-digit hex
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
const { parse } = require('css-box-shadow-parser');
|
|
84
|
+
|
|
85
|
+
// Named color → hex resolved
|
|
86
|
+
parse('0 0 28px 6px rebeccapurple')
|
|
87
|
+
// [{
|
|
88
|
+
// inset: false, x: 0, y: 0, blur: 28, spread: 6,
|
|
89
|
+
// color: 'rebeccapurple', alpha: 1, hex: '#663399'
|
|
90
|
+
// }]
|
|
91
|
+
|
|
92
|
+
// 8-digit hex — alpha extracted
|
|
93
|
+
parse('0 8px 24px #00000066')
|
|
94
|
+
// [{
|
|
95
|
+
// inset: false, x: 0, y: 8, blur: 24, spread: 0,
|
|
96
|
+
// color: '#00000066', alpha: 0.4, hex: '#000000'
|
|
97
|
+
// }]
|
|
98
|
+
|
|
99
|
+
// HSL color
|
|
100
|
+
parse('0 6px 20px hsl(270 80% 50% / 0.6)')
|
|
101
|
+
// [{
|
|
102
|
+
// inset: false, x: 0, y: 6, blur: 20, spread: 0,
|
|
103
|
+
// color: 'hsl(270 80% 50% / 0.6)', alpha: 0.6, hex: '#8019e6'
|
|
104
|
+
// }]
|
|
105
|
+
|
|
106
|
+
// Transparent (alpha = 0)
|
|
107
|
+
parse('0 4px 8px transparent')
|
|
108
|
+
// [{
|
|
109
|
+
// inset: false, x: 0, y: 4, blur: 8, spread: 0,
|
|
110
|
+
// color: 'transparent', alpha: 0, hex: '#000000'
|
|
111
|
+
// }]
|
|
112
|
+
|
|
113
|
+
// Inset
|
|
114
|
+
parse('inset 0 2px 4px rgba(0,0,0,0.24)')
|
|
115
|
+
// [{
|
|
116
|
+
// inset: true, x: 0, y: 2, blur: 4, spread: 0,
|
|
117
|
+
// color: 'rgba(0,0,0,0.24)', alpha: 0.24, hex: '#000000'
|
|
118
|
+
// }]
|
|
119
|
+
|
|
120
|
+
// Neumorphic — two layers
|
|
121
|
+
parse('6px 6px 12px #b8b9be, -6px -6px 12px #ffffff')
|
|
122
|
+
// [
|
|
123
|
+
// { inset: false, x: 6, y: 6, blur: 12, spread: 0, color: '#b8b9be', alpha: 1, hex: '#b8b9be' },
|
|
124
|
+
// { inset: false, x: -6, y: -6, blur: 12, spread: 0, color: '#ffffff', alpha: 1, hex: '#ffffff' }
|
|
125
|
+
// ]
|
|
126
|
+
|
|
127
|
+
// split only — no parsing
|
|
128
|
+
split('4px 4px 0 red, inset 0 0 10px rgba(0,0,0,.5)')
|
|
129
|
+
// ['4px 4px 0 red', 'inset 0 0 10px rgba(0,0,0,.5)']
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Supported Color Formats
|
|
135
|
+
|
|
136
|
+
| Format | Example |
|
|
137
|
+
|-----------------|--------------------------------|
|
|
138
|
+
| 3-digit hex | `#f00` |
|
|
139
|
+
| 4-digit hex | `#f00a` |
|
|
140
|
+
| 6-digit hex | `#ff0000` |
|
|
141
|
+
| 8-digit hex | `#ff000066` |
|
|
142
|
+
| `rgb()` | `rgb(255,0,0)` |
|
|
143
|
+
| `rgba()` | `rgba(0,0,0,0.5)` |
|
|
144
|
+
| `hsl()` | `hsl(270 80% 50%)` |
|
|
145
|
+
| `hsla()` | `hsla(270,80%,50%,0.6)` |
|
|
146
|
+
| Named color | `rebeccapurple`, `coral`, etc. |
|
|
147
|
+
| `transparent` | alpha = 0 |
|
|
148
|
+
| `currentcolor` | resolves to `#000000` |
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* css-box-shadow-parser v1.0.0
|
|
3
|
+
* Parse CSS box-shadow values into structured layer objects.
|
|
4
|
+
* https://github.com/html-code-generator/css-box-shadow-parser
|
|
5
|
+
*
|
|
6
|
+
* @author HTML Code Generator <htmlcodegenerator.com@gmail.com>
|
|
7
|
+
* @website https://www.html-code-generator.com/
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
(function (root, factory) {
|
|
12
|
+
const api = factory();
|
|
13
|
+
|
|
14
|
+
if (typeof module === 'object' && module.exports) {
|
|
15
|
+
module.exports = api;
|
|
16
|
+
} else {
|
|
17
|
+
root.BoxShadowParser = api;
|
|
18
|
+
}
|
|
19
|
+
}(
|
|
20
|
+
typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : this,
|
|
21
|
+
() => {
|
|
22
|
+
const NO_SHADOW = new Set(['none', 'initial', 'inherit', 'unset', 'revert', 'revert-layer']);
|
|
23
|
+
const UNRESOLVED_COLOR_HEX = '#000000';
|
|
24
|
+
|
|
25
|
+
const NAMED_COLORS = {
|
|
26
|
+
aliceblue: '#f0f8ff',
|
|
27
|
+
antiquewhite: '#faebd7',
|
|
28
|
+
aqua: '#00ffff',
|
|
29
|
+
aquamarine: '#7fffd4',
|
|
30
|
+
azure: '#f0ffff',
|
|
31
|
+
beige: '#f5f5dc',
|
|
32
|
+
bisque: '#ffe4c4',
|
|
33
|
+
black: '#000000',
|
|
34
|
+
blanchedalmond: '#ffebcd',
|
|
35
|
+
blue: '#0000ff',
|
|
36
|
+
blueviolet: '#8a2be2',
|
|
37
|
+
brown: '#a52a2a',
|
|
38
|
+
burlywood: '#deb887',
|
|
39
|
+
cadetblue: '#5f9ea0',
|
|
40
|
+
chartreuse: '#7fff00',
|
|
41
|
+
chocolate: '#d2691e',
|
|
42
|
+
coral: '#ff7f50',
|
|
43
|
+
cornflowerblue: '#6495ed',
|
|
44
|
+
cornsilk: '#fff8dc',
|
|
45
|
+
crimson: '#dc143c',
|
|
46
|
+
cyan: '#00ffff',
|
|
47
|
+
darkblue: '#00008b',
|
|
48
|
+
darkcyan: '#008b8b',
|
|
49
|
+
darkgoldenrod: '#b8860b',
|
|
50
|
+
darkgray: '#a9a9a9',
|
|
51
|
+
darkgreen: '#006400',
|
|
52
|
+
darkgrey: '#a9a9a9',
|
|
53
|
+
darkkhaki: '#bdb76b',
|
|
54
|
+
darkmagenta: '#8b008b',
|
|
55
|
+
darkolivegreen: '#556b2f',
|
|
56
|
+
darkorange: '#ff8c00',
|
|
57
|
+
darkorchid: '#9932cc',
|
|
58
|
+
darkred: '#8b0000',
|
|
59
|
+
darksalmon: '#e9967a',
|
|
60
|
+
darkseagreen: '#8fbc8f',
|
|
61
|
+
darkslateblue: '#483d8b',
|
|
62
|
+
darkslategray: '#2f4f4f',
|
|
63
|
+
darkslategrey: '#2f4f4f',
|
|
64
|
+
darkturquoise: '#00ced1',
|
|
65
|
+
darkviolet: '#9400d3',
|
|
66
|
+
deeppink: '#ff1493',
|
|
67
|
+
deepskyblue: '#00bfff',
|
|
68
|
+
dimgray: '#696969',
|
|
69
|
+
dimgrey: '#696969',
|
|
70
|
+
dodgerblue: '#1e90ff',
|
|
71
|
+
firebrick: '#b22222',
|
|
72
|
+
floralwhite: '#fffaf0',
|
|
73
|
+
forestgreen: '#228b22',
|
|
74
|
+
fuchsia: '#ff00ff',
|
|
75
|
+
gainsboro: '#dcdcdc',
|
|
76
|
+
ghostwhite: '#f8f8ff',
|
|
77
|
+
gold: '#ffd700',
|
|
78
|
+
goldenrod: '#daa520',
|
|
79
|
+
gray: '#808080',
|
|
80
|
+
green: '#008000',
|
|
81
|
+
greenyellow: '#adff2f',
|
|
82
|
+
grey: '#808080',
|
|
83
|
+
honeydew: '#f0fff0',
|
|
84
|
+
hotpink: '#ff69b4',
|
|
85
|
+
indianred: '#cd5c5c',
|
|
86
|
+
indigo: '#4b0082',
|
|
87
|
+
ivory: '#fffff0',
|
|
88
|
+
khaki: '#f0e68c',
|
|
89
|
+
lavender: '#e6e6fa',
|
|
90
|
+
lavenderblush: '#fff0f5',
|
|
91
|
+
lawngreen: '#7cfc00',
|
|
92
|
+
lemonchiffon: '#fffacd',
|
|
93
|
+
lightblue: '#add8e6',
|
|
94
|
+
lightcoral: '#f08080',
|
|
95
|
+
lightcyan: '#e0ffff',
|
|
96
|
+
lightgoldenrodyellow: '#fafad2',
|
|
97
|
+
lightgray: '#d3d3d3',
|
|
98
|
+
lightgreen: '#90ee90',
|
|
99
|
+
lightgrey: '#d3d3d3',
|
|
100
|
+
lightpink: '#ffb6c1',
|
|
101
|
+
lightsalmon: '#ffa07a',
|
|
102
|
+
lightseagreen: '#20b2aa',
|
|
103
|
+
lightskyblue: '#87cefa',
|
|
104
|
+
lightslategray: '#778899',
|
|
105
|
+
lightslategrey: '#778899',
|
|
106
|
+
lightsteelblue: '#b0c4de',
|
|
107
|
+
lightyellow: '#ffffe0',
|
|
108
|
+
lime: '#00ff00',
|
|
109
|
+
limegreen: '#32cd32',
|
|
110
|
+
linen: '#faf0e6',
|
|
111
|
+
magenta: '#ff00ff',
|
|
112
|
+
maroon: '#800000',
|
|
113
|
+
mediumaquamarine: '#66cdaa',
|
|
114
|
+
mediumblue: '#0000cd',
|
|
115
|
+
mediumorchid: '#ba55d3',
|
|
116
|
+
mediumpurple: '#9370db',
|
|
117
|
+
mediumseagreen: '#3cb371',
|
|
118
|
+
mediumslateblue: '#7b68ee',
|
|
119
|
+
mediumspringgreen: '#00fa9a',
|
|
120
|
+
mediumturquoise: '#48d1cc',
|
|
121
|
+
mediumvioletred: '#c71585',
|
|
122
|
+
midnightblue: '#191970',
|
|
123
|
+
mintcream: '#f5fffa',
|
|
124
|
+
mistyrose: '#ffe4e1',
|
|
125
|
+
moccasin: '#ffe4b5',
|
|
126
|
+
navajowhite: '#ffdead',
|
|
127
|
+
navy: '#000080',
|
|
128
|
+
oldlace: '#fdf5e6',
|
|
129
|
+
olive: '#808000',
|
|
130
|
+
olivedrab: '#6b8e23',
|
|
131
|
+
orange: '#ffa500',
|
|
132
|
+
orangered: '#ff4500',
|
|
133
|
+
orchid: '#da70d6',
|
|
134
|
+
palegoldenrod: '#eee8aa',
|
|
135
|
+
palegreen: '#98fb98',
|
|
136
|
+
paleturquoise: '#afeeee',
|
|
137
|
+
palevioletred: '#db7093',
|
|
138
|
+
papayawhip: '#ffefd5',
|
|
139
|
+
peachpuff: '#ffdab9',
|
|
140
|
+
peru: '#cd853f',
|
|
141
|
+
pink: '#ffc0cb',
|
|
142
|
+
plum: '#dda0dd',
|
|
143
|
+
powderblue: '#b0e0e6',
|
|
144
|
+
purple: '#800080',
|
|
145
|
+
rebeccapurple: '#663399',
|
|
146
|
+
red: '#ff0000',
|
|
147
|
+
rosybrown: '#bc8f8f',
|
|
148
|
+
royalblue: '#4169e1',
|
|
149
|
+
saddlebrown: '#8b4513',
|
|
150
|
+
salmon: '#fa8072',
|
|
151
|
+
sandybrown: '#f4a460',
|
|
152
|
+
seagreen: '#2e8b57',
|
|
153
|
+
seashell: '#fff5ee',
|
|
154
|
+
sienna: '#a0522d',
|
|
155
|
+
silver: '#c0c0c0',
|
|
156
|
+
skyblue: '#87ceeb',
|
|
157
|
+
slateblue: '#6a5acd',
|
|
158
|
+
slategray: '#708090',
|
|
159
|
+
slategrey: '#708090',
|
|
160
|
+
snow: '#fffafa',
|
|
161
|
+
springgreen: '#00ff7f',
|
|
162
|
+
steelblue: '#4682b4',
|
|
163
|
+
tan: '#d2b48c',
|
|
164
|
+
teal: '#008080',
|
|
165
|
+
thistle: '#d8bfd8',
|
|
166
|
+
tomato: '#ff6347',
|
|
167
|
+
turquoise: '#40e0d0',
|
|
168
|
+
violet: '#ee82ee',
|
|
169
|
+
wheat: '#f5deb3',
|
|
170
|
+
white: '#ffffff',
|
|
171
|
+
whitesmoke: '#f5f5f5',
|
|
172
|
+
yellow: '#ffff00',
|
|
173
|
+
yellowgreen: '#9acd32',
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const RE_COLOR_FN = /\b(?:rgba?|hsla?|oklch|oklab|lch|lab|hwb|color)\s*\([^()]*\)/i;
|
|
177
|
+
const RE_COLOR_HEX = /#[0-9a-fA-F]{8}\b|#[0-9a-fA-F]{6}\b|#[0-9a-fA-F]{4}\b|#[0-9a-fA-F]{3}\b/;
|
|
178
|
+
const RE_COLOR_NAME = new RegExp(`\\b(?:transparent|currentcolor|${Object.keys(NAMED_COLORS).join('|')})\\b`, 'i');
|
|
179
|
+
const RE_LENGTH = /[+-]?(?:\d*\.)?\d+(?:px|r?em|%|v(?:[hw]|min|max)|ch|ex|cm|mm|in|p[tc]|q)?(?=\s|$)/g;
|
|
180
|
+
|
|
181
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
182
|
+
const roundAlpha = (value) => Math.round(clamp(value, 0, 1) * 100) / 100;
|
|
183
|
+
|
|
184
|
+
const parseChannel = (raw) => {
|
|
185
|
+
const value = raw.trim();
|
|
186
|
+
const parsed = parseFloat(value);
|
|
187
|
+
return value.endsWith('%') ? clamp(parsed, 0, 100) * 2.55 : clamp(parsed, 0, 255);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const parseAlpha = (raw) => {
|
|
191
|
+
if (raw === undefined || raw === null || raw === '') return 1;
|
|
192
|
+
const value = raw.trim();
|
|
193
|
+
const parsed = parseFloat(value);
|
|
194
|
+
return roundAlpha(value.endsWith('%') ? parsed / 100 : parsed);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const toHex = (r, g, b) => (
|
|
198
|
+
'#' + [r, g, b]
|
|
199
|
+
.map((value) => Math.round(clamp(Number(value), 0, 255)).toString(16).padStart(2, '0'))
|
|
200
|
+
.join('')
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const hslToRgb = (h, s, l) => {
|
|
204
|
+
const hue = ((h % 360) + 360) % 360;
|
|
205
|
+
const sat = clamp(s, 0, 100) / 100;
|
|
206
|
+
const light = clamp(l, 0, 100) / 100;
|
|
207
|
+
const k = (n) => (n + hue / 30) % 12;
|
|
208
|
+
const a = sat * Math.min(light, 1 - light);
|
|
209
|
+
const f = (n) => light - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
|
|
210
|
+
return [f(0) * 255, f(8) * 255, f(4) * 255];
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const colorToHex = (color) => {
|
|
214
|
+
if (!color) return { hex: UNRESOLVED_COLOR_HEX, alpha: 1 };
|
|
215
|
+
|
|
216
|
+
const value = color.trim().toLowerCase();
|
|
217
|
+
|
|
218
|
+
if (/^#[0-9a-f]{6}$/.test(value)) return { hex: value, alpha: 1 };
|
|
219
|
+
|
|
220
|
+
if (/^#[0-9a-f]{3}$/.test(value)) {
|
|
221
|
+
return { hex: '#' + [...value.slice(1)].map((char) => char + char).join(''), alpha: 1 };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (/^#[0-9a-f]{8}$/.test(value)) {
|
|
225
|
+
return { hex: value.slice(0, 7), alpha: roundAlpha(parseInt(value.slice(7, 9), 16) / 255) };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (/^#[0-9a-f]{4}$/.test(value)) {
|
|
229
|
+
return {
|
|
230
|
+
hex: '#' + [...value.slice(1, 4)].map((char) => char + char).join(''),
|
|
231
|
+
alpha: roundAlpha(parseInt(value[4] + value[4], 16) / 255),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const rgbMatch = value.match(/^rgba?\(\s*([+-]?(?:\d*\.)?\d+%?)\s*(?:,|\s)\s*([+-]?(?:\d*\.)?\d+%?)\s*(?:,|\s)\s*([+-]?(?:\d*\.)?\d+%?)(?:\s*(?:,|\/)\s*([+-]?(?:\d*\.)?\d+%?))?\s*\)$/);
|
|
236
|
+
if (rgbMatch) {
|
|
237
|
+
return {
|
|
238
|
+
hex: toHex(parseChannel(rgbMatch[1]), parseChannel(rgbMatch[2]), parseChannel(rgbMatch[3])),
|
|
239
|
+
alpha: parseAlpha(rgbMatch[4]),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const hslMatch = value.match(/^hsla?\(\s*([+-]?(?:\d*\.)?\d+)(?:deg)?\s*(?:,|\s)\s*([+-]?(?:\d*\.)?\d+)%\s*(?:,|\s)\s*([+-]?(?:\d*\.)?\d+)%(?:\s*(?:,|\/)\s*([+-]?(?:\d*\.)?\d+%?))?\s*\)$/);
|
|
244
|
+
if (hslMatch) {
|
|
245
|
+
const [r, g, b] = hslToRgb(
|
|
246
|
+
parseFloat(hslMatch[1]),
|
|
247
|
+
parseFloat(hslMatch[2]),
|
|
248
|
+
parseFloat(hslMatch[3])
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
hex: toHex(r, g, b),
|
|
253
|
+
alpha: parseAlpha(hslMatch[4]),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (value === 'transparent') return { hex: UNRESOLVED_COLOR_HEX, alpha: 0 };
|
|
258
|
+
if (value === 'currentcolor') return { hex: UNRESOLVED_COLOR_HEX, alpha: 1 };
|
|
259
|
+
if (NAMED_COLORS[value]) return { hex: NAMED_COLORS[value], alpha: 1 };
|
|
260
|
+
|
|
261
|
+
return { hex: UNRESOLVED_COLOR_HEX, alpha: 1 };
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const splitShadows = (shadow) => {
|
|
265
|
+
if (typeof shadow !== 'string') return [];
|
|
266
|
+
|
|
267
|
+
const result = [];
|
|
268
|
+
let depth = 0;
|
|
269
|
+
let current = '';
|
|
270
|
+
|
|
271
|
+
for (const char of shadow) {
|
|
272
|
+
if (char === '(') {
|
|
273
|
+
depth += 1;
|
|
274
|
+
} else if (char === ')' && depth > 0) {
|
|
275
|
+
depth -= 1;
|
|
276
|
+
} else if (char === ',' && depth === 0) {
|
|
277
|
+
if (current.trim()) result.push(current.trim());
|
|
278
|
+
current = '';
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
current += char;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (current.trim()) result.push(current.trim());
|
|
286
|
+
return result;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const extractColor = (value) => {
|
|
290
|
+
let color = null;
|
|
291
|
+
let rest = value;
|
|
292
|
+
|
|
293
|
+
const tryExtract = (regex) => {
|
|
294
|
+
const match = rest.match(regex);
|
|
295
|
+
if (!match) return false;
|
|
296
|
+
|
|
297
|
+
color = match[0];
|
|
298
|
+
rest = `${rest.slice(0, match.index)} ${rest.slice(match.index + match[0].length)}`.trim();
|
|
299
|
+
return true;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
tryExtract(RE_COLOR_FN) || tryExtract(RE_COLOR_HEX) || tryExtract(RE_COLOR_NAME);
|
|
303
|
+
return { color, rest };
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const parseSingleShadow = (raw) => {
|
|
307
|
+
if (typeof raw !== 'string') return null;
|
|
308
|
+
|
|
309
|
+
let value = raw.trim();
|
|
310
|
+
if (!value || NO_SHADOW.has(value.toLowerCase())) return null;
|
|
311
|
+
|
|
312
|
+
const inset = /\binset\b/i.test(value);
|
|
313
|
+
value = value.replace(/\binset\b/gi, ' ').trim();
|
|
314
|
+
|
|
315
|
+
const extracted = extractColor(value);
|
|
316
|
+
const color = extracted.color;
|
|
317
|
+
value = extracted.rest;
|
|
318
|
+
|
|
319
|
+
const lengths = value.match(RE_LENGTH) || [];
|
|
320
|
+
if (lengths.length < 2) return null;
|
|
321
|
+
|
|
322
|
+
const { hex, alpha } = colorToHex(color);
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
inset,
|
|
326
|
+
x: parseFloat(lengths[0]),
|
|
327
|
+
y: parseFloat(lengths[1]),
|
|
328
|
+
blur: parseFloat(lengths[2]) || 0,
|
|
329
|
+
spread: parseFloat(lengths[3]) || 0,
|
|
330
|
+
color: color || UNRESOLVED_COLOR_HEX,
|
|
331
|
+
alpha,
|
|
332
|
+
hex,
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const parseBoxShadow = (shadow) => {
|
|
337
|
+
if (typeof shadow !== 'string') return [];
|
|
338
|
+
|
|
339
|
+
// Accept full CSS declarations: "box-shadow: ...; " → extract value only
|
|
340
|
+
let trimmed = shadow.trim().replace(/^box-shadow\s*:\s*/i, '').replace(/;$/, '').trim();
|
|
341
|
+
if (!trimmed || NO_SHADOW.has(trimmed.toLowerCase())) return [];
|
|
342
|
+
|
|
343
|
+
return splitShadows(trimmed).map(parseSingleShadow).filter(Boolean);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return {
|
|
347
|
+
parse: parseBoxShadow,
|
|
348
|
+
split: splitShadows,
|
|
349
|
+
parseSingle: parseSingleShadow,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
));
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "css-box-shadow-parser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Parse CSS box-shadow values into structured layer objects. Supports multi-layer, inset, all color formats (hex, rgb, hsl, named), and full CSS declarations.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"example": "node usage.js",
|
|
8
|
+
"prepublishOnly": "node -e \"const fs=require('fs'); fs.copyFileSync('../parser.js','index.js'); fs.copyFileSync('../LICENSE','LICENSE'); console.log('Synced parser.js → index.js & LICENSE');\""
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"css",
|
|
12
|
+
"box-shadow",
|
|
13
|
+
"shadow",
|
|
14
|
+
"parser",
|
|
15
|
+
"color",
|
|
16
|
+
"hex",
|
|
17
|
+
"rgba",
|
|
18
|
+
"hsl",
|
|
19
|
+
"inset",
|
|
20
|
+
"browser",
|
|
21
|
+
"node"
|
|
22
|
+
],
|
|
23
|
+
"author": "HTML Code Generator <htmlcodegenerator.com@gmail.com> (https://www.html-code-generator.com/)",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"files": [
|
|
26
|
+
"index.js",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/html-code-generator/css-box-shadow-parser.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/html-code-generator/css-box-shadow-parser/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://www.html-code-generator.com/",
|
|
40
|
+
"unpkg": "index.js",
|
|
41
|
+
"jsdelivr": "index.js"
|
|
42
|
+
}
|