Skip to content
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

Adds check to make sure duplicates are not created. #19

Merged
merged 1 commit into from
Jul 11, 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
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module.exports = postcss.plugin('postcss-initial', function (opts) {
opts.reset = opts.reset || 'all';
opts.replace = opts.replace || false;
var getFallback = makeFallbackFunction(opts.reset === 'inherited');
var getPropPrevTo = function (prop, decl) {
var foundPrev = false;
decl.parent.walkDecls(function (child) {
if (child.prop === decl.prop && child.value !== decl.value) {
foundPrev = true;
}
});
return foundPrev;
};
return function (css) {
css.walkDecls(function (decl) {
if (decl.value.indexOf('initial') < 0) {
Expand All @@ -14,7 +23,9 @@ module.exports = postcss.plugin('postcss-initial', function (opts) {
var fallBackRules = getFallback(decl.prop, decl.value);
if (fallBackRules.length === 0) return;
fallBackRules.forEach(function (rule) {
decl.cloneBefore(rule);
if ( !getPropPrevTo(decl.prop, decl) ) {
decl.cloneBefore(rule);
}
});
if (opts.replace === true) {
decl.remove();
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/no-duplication.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a {
overflow: visible;
overflow: initial;
width: auto;
width: initial;
}
6 changes: 6 additions & 0 deletions test/fixtures/no-duplication.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a {
overflow: visible;
overflow: initial;
width: auto;
width: initial;
}
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('postcss-initial', function () {
f('unknown.expected')
);
});
it('no duplication', function () {
test(
f('no-duplication'),
f('no-duplication.expected')
);
});
it('all:initial - default styles', function () {
test(
f('all-initial-default'),
Expand Down