Skip to content

Allow blacklisting property shorthands #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ getPropertyName('border-width'); // => 'borderWidth'
getStylesForProperty('borderWidth', '1 0 2 0'); // => { borderTopWidth: 1, ... }
```

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`.

```js
transform([['border-radius', '50']], ['borderRadius']);
// { borderRadius: 50 } rather than { borderTopLeft: ... }
```

## License

Licensed under the MIT License, Copyright © 2016 Jacob Parker and Maximilian Stoiber.
Expand Down
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const transformRawValue = input => (
: input
);

export const getStylesForProperty = (propName, inputValue) => {
export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
const value = inputValue.trim();

const propValue = (transforms.indexOf(propName) !== -1)
const propValue = (allowShorthand && transforms.indexOf(propName) !== -1)
? (new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0])
: transformRawValue(value);

Expand All @@ -41,9 +41,9 @@ export const getStylesForProperty = (propName, inputValue) => {

export const getPropertyName = camelizeStyleName;

export default rules => rules.reduce((accum, rule) => (
Object.assign(accum, getStylesForProperty(
getPropertyName(rule[0]),
rule[1],
))
), {});
export default (rules, shorthandBlacklist = []) => rules.reduce((accum, rule) => {
const propertyName = getPropertyName(rule[0]);
const value = rule[1];
const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1;
return Object.assign(accum, getStylesForProperty(propertyName, value, allowShorthand));
}, {});
5 changes: 5 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,8 @@ it('omits line height if not specified', () => runTest([
fontStyle: 'normal',
fontVariant: [],
}));

it('allows blacklisting shorthands', () => {
const actualStyles = transformCss([['border-radius', '50']], ['borderRadius']);
expect(actualStyles).toEqual({ borderRadius: 50 });
});