Skip to content

Commit

Permalink
fix: πŸ› add !important only if it is not already there
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Aug 5, 2018
1 parent 0e1eca8 commit 8f0bc4c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .storybook/important.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const className1 = rule({
border: '1px solid red'
}, 'RedBorderImportant');

const className2 = rule({
border: '1px solid red !important'
}, 'RedBorderImportantImportant');

storiesOf('Addons/!important', module)
.add('Default', () =>
h('div', {className: className1}, 'Hello world')
)
.add('Double !important', () =>
h('div', {className: className2}, 'Hello world')
)
29 changes: 28 additions & 1 deletion addon/important.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
'use strict';

function hasImportant (rawDecl) {
var parts = rawDecl.split(' ');

for (var i = 0; i < parts.length; i++) {
var part = parts[i].trim();

if (part === '!important') return true;
}

return false;
}

exports.addon = function (renderer) {
var decl = renderer.decl;

renderer.decl = function (prop, value) {
return decl(prop, value).replace(/;/g, ' !important;');
var rawDecl = decl(prop, value);
var decls = rawDecl.split(';');
var css = '';

for (var i = 0; i < decls.length; i++) {
rawDecl = decls[i];

// Don't add "!important" if it is already added.
if (!hasImportant(rawDecl)) {
css += rawDecl + ' !important;';
} else {
css += rawDecl + ';';
}
}

return css;
};
};

1 comment on commit 8f0bc4c

@streamich
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build version: 3.1.0-master.85 🀞 master on CircleCI πŸŽ‰

Please sign in to comment.