diff --git a/index.js b/index.js index c2dc9e1..c4831cb 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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(); diff --git a/test/fixtures/no-duplication.css b/test/fixtures/no-duplication.css new file mode 100644 index 0000000..3c70951 --- /dev/null +++ b/test/fixtures/no-duplication.css @@ -0,0 +1,6 @@ +a { + overflow: visible; + overflow: initial; + width: auto; + width: initial; +} diff --git a/test/fixtures/no-duplication.expected.css b/test/fixtures/no-duplication.expected.css new file mode 100644 index 0000000..3c70951 --- /dev/null +++ b/test/fixtures/no-duplication.expected.css @@ -0,0 +1,6 @@ +a { + overflow: visible; + overflow: initial; + width: auto; + width: initial; +} diff --git a/test/index.js b/test/index.js index 7c50d91..d99b8c6 100644 --- a/test/index.js +++ b/test/index.js @@ -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'),