Skip to content

Commit 7beef8f

Browse files
authored
Merge pull request #60 from kristerkari/bugfix/box-shadow
Bugfix/box shadow
2 parents 7347065 + 7829984 commit 7beef8f

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/index.test.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ it('transforms box-shadow into shadow- properties', () => runTest([
464464
shadowOffset: { width: 10, height: 20 },
465465
shadowRadius: 30,
466466
shadowColor: 'red',
467+
shadowOpacity: 1,
467468
}));
468469

469470
it('transforms box-shadow without blur-radius', () => runTest([
@@ -472,14 +473,16 @@ it('transforms box-shadow without blur-radius', () => runTest([
472473
shadowOffset: { width: 10, height: 20 },
473474
shadowRadius: 0,
474475
shadowColor: 'red',
476+
shadowOpacity: 1,
475477
}));
476478

477479
it('transforms box-shadow without color', () => runTest([
478-
['box-shadow', '10px 20px red'],
480+
['box-shadow', '10px 20px 30px'],
479481
], {
480482
shadowOffset: { width: 10, height: 20 },
481-
shadowRadius: 0,
482-
shadowColor: 'red',
483+
shadowRadius: 30,
484+
shadowColor: 'black',
485+
shadowOpacity: 1,
483486
}));
484487

485488
it('transforms box-shadow without blur-radius, color', () => runTest([
@@ -488,8 +491,50 @@ it('transforms box-shadow without blur-radius, color', () => runTest([
488491
shadowOffset: { width: 10, height: 20 },
489492
shadowRadius: 0,
490493
shadowColor: 'black',
494+
shadowOpacity: 1,
495+
}));
496+
497+
it('transforms box-shadow with rgb color', () => runTest([
498+
['box-shadow', '10px 20px rgb(100, 100, 100)'],
499+
], {
500+
shadowOffset: { width: 10, height: 20 },
501+
shadowRadius: 0,
502+
shadowColor: 'rgb(100, 100, 100)',
503+
shadowOpacity: 1,
491504
}));
492505

506+
it('transforms box-shadow with rgba color', () => runTest([
507+
['box-shadow', '10px 20px rgba(100, 100, 100, 0.5)'],
508+
], {
509+
shadowOffset: { width: 10, height: 20 },
510+
shadowRadius: 0,
511+
shadowColor: 'rgba(100, 100, 100, 0.5)',
512+
shadowOpacity: 1,
513+
}));
514+
515+
it('transforms box-shadow with hsl color', () => runTest([
516+
['box-shadow', '10px 20px hsl(120, 100%, 50%)'],
517+
], {
518+
shadowOffset: { width: 10, height: 20 },
519+
shadowRadius: 0,
520+
shadowColor: 'hsl(120, 100%, 50%)',
521+
shadowOpacity: 1,
522+
}));
523+
524+
it('transforms box-shadow with hsla color', () => runTest([
525+
['box-shadow', '10px 20px hsla(120, 100%, 50%, 0.7)'],
526+
], {
527+
shadowOffset: { width: 10, height: 20 },
528+
shadowRadius: 0,
529+
shadowColor: 'hsla(120, 100%, 50%, 0.7)',
530+
shadowOpacity: 1,
531+
}));
532+
533+
it('transforms box-shadow and throws if multiple colors are used', () => {
534+
expect(() => transformCss([['box-shadow', '0 0 0 red yellow green blue']]))
535+
.toThrow('Failed to parse declaration "boxShadow: 0 0 0 red yellow green blue"');
536+
});
537+
493538
it('transforms box-shadow enforces offset to be present', () => {
494539
expect(() => transformCss([['box-shadow', 'red']]))
495540
.toThrow('Failed to parse declaration "boxShadow: red"');

src/transforms/boxShadow.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { tokens } = require('../tokenTypes');
22

3-
const { NONE, SPACE, WORD, LENGTH } = tokens;
3+
const { NONE, SPACE, COLOR, LENGTH } = tokens;
44

55
module.exports = (tokenStream) => {
66
let offsetX;
@@ -11,7 +11,7 @@ module.exports = (tokenStream) => {
1111
if (tokenStream.matches(NONE)) {
1212
tokenStream.expectEmpty();
1313
return {
14-
$merge: { shadowOffset: { width: 0, height: 0 }, shadowRadius: 0, shadowColor: 'black' },
14+
$merge: { shadowOffset: { width: 0, height: 0 }, shadowRadius: 0, shadowColor: 'black', shadowOpacity: 1 },
1515
};
1616
}
1717

@@ -28,10 +28,8 @@ module.exports = (tokenStream) => {
2828
tokenStream.expect(SPACE);
2929
blurRadius = tokenStream.expect(LENGTH);
3030
}
31-
} else if (color === undefined && (
32-
tokenStream.matchesFunction() || tokenStream.matches(WORD)
33-
)) {
34-
color = String(tokenStream.lastValue);
31+
} else if (color === undefined && tokenStream.matches(COLOR)) {
32+
color = tokenStream.lastValue;
3533
} else {
3634
tokenStream.throw();
3735
}
@@ -45,6 +43,7 @@ module.exports = (tokenStream) => {
4543
shadowOffset: { width: offsetX, height: offsetY },
4644
shadowRadius: blurRadius !== undefined ? blurRadius : 0,
4745
shadowColor: color !== undefined ? color : 'black',
46+
shadowOpacity: 1,
4847
};
4948
return { $merge };
5049
};

0 commit comments

Comments
 (0)