Skip to content

Commit b2a3d04

Browse files
committed
Merge remote-tracking branch 'origin/master' into new-version
2 parents 01d5655 + 73abab3 commit b2a3d04

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ getPropertyName('border-width'); // => 'borderWidth'
9393
getStylesForProperty('borderWidth', '1 0 2 0'); // => { borderTopWidth: 1, ... }
9494
```
9595

96+
Should you wish to opt-out of transforming certain shorthands, an array of property names in camelCase can be passed as a second argument to `transform`.
97+
98+
```js
99+
transform([['border-radius', '50']], ['borderRadius']);
100+
// { borderRadius: 50 } rather than { borderTopLeft: ... }
101+
```
102+
96103
## License
97104

98105
Licensed under the MIT License, Copyright © 2016 Jacob Parker and Maximilian Stoiber.

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const transformRawValue = input => (
3030
export const parseProp = (propName, value) =>
3131
new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0];
3232

33-
export const getStylesForProperty = (propName, inputValue) => {
33+
export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
3434
const value = inputValue.trim();
3535

36-
const propValue = (transforms.indexOf(propName) !== -1)
36+
const propValue = (allowShorthand && transforms.indexOf(propName) !== -1)
3737
? parseProp(propName, value)
3838
: transformRawValue(value);
3939

@@ -44,9 +44,9 @@ export const getStylesForProperty = (propName, inputValue) => {
4444

4545
export const getPropertyName = camelizeStyleName;
4646

47-
export default rules => rules.reduce((accum, rule) => (
48-
Object.assign(accum, getStylesForProperty(
49-
getPropertyName(rule[0]),
50-
rule[1],
51-
))
52-
), {});
47+
export default (rules, shorthandBlacklist = []) => rules.reduce((accum, rule) => {
48+
const propertyName = getPropertyName(rule[0]);
49+
const value = rule[1];
50+
const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1;
51+
return Object.assign(accum, getStylesForProperty(propertyName, value, allowShorthand));
52+
}, {});

src/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,8 @@ it('omits line height if not specified', () => runTest([
285285
fontStyle: 'normal',
286286
fontVariant: [],
287287
}));
288+
289+
it('allows blacklisting shorthands', () => {
290+
const actualStyles = transformCss([['border-radius', '50']], ['borderRadius']);
291+
expect(actualStyles).toEqual({ borderRadius: 50 });
292+
});

0 commit comments

Comments
 (0)