postcss-discard-duplicates
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -0,1 +1,6 @@ | ||
# 2.1.0 | ||
* Now supports discarding duplicates irrespective of property order, better | ||
support for legacy hacks & partial duplicate removal (thanks to @andyjansson). | ||
# 2.0.2 | ||
@@ -2,0 +7,0 @@ |
@@ -7,32 +7,128 @@ 'use strict'; | ||
function dedupe(root) { | ||
root.each(function (node) { | ||
if (node.nodes) { | ||
dedupe(node); | ||
function noop() {} | ||
function trimValue(value) { | ||
return value ? value.trim() : value; | ||
} | ||
function empty(node) { | ||
return !node.nodes.filter(function (child) { | ||
return child.type !== 'comment'; | ||
}).length; | ||
} | ||
function equals(a, b) { | ||
if (a.type !== b.type) { | ||
return false; | ||
} | ||
if (a.important !== b.important) { | ||
return false; | ||
} | ||
if (a.raws && !b.raws || !a.raws && b.raws) { | ||
return false; | ||
} | ||
switch (a.type) { | ||
case 'rule': | ||
if (a.selector !== b.selector) { | ||
return false; | ||
} | ||
break; | ||
case 'atrule': | ||
if (a.name !== b.name || a.params !== b.params) { | ||
return false; | ||
} | ||
if (a.raws && trimValue(a.raws.before) !== trimValue(b.raws.before)) { | ||
return false; | ||
} | ||
if (a.raws && trimValue(a.raws.afterName) !== trimValue(b.raws.afterName)) { | ||
return false; | ||
} | ||
break; | ||
case 'decl': | ||
if (a.prop !== b.prop || a.value !== b.value) { | ||
return false; | ||
} | ||
if (a.raws && trimValue(a.raws.before) !== trimValue(b.raws.before)) { | ||
return false; | ||
} | ||
break; | ||
} | ||
if (a.nodes) { | ||
if (a.nodes.length !== b.nodes.length) { | ||
return false; | ||
} | ||
}); | ||
if (root.nodes.length < 2) { | ||
return; | ||
for (var i = 0; i < a.nodes.length; i++) { | ||
if (!equals(a.nodes[i], b.nodes[i])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
var toRemove = []; | ||
var map = {}; | ||
function dedupeRule(last, nodes) { | ||
var index = nodes.indexOf(last) - 1; | ||
root.each(function (node) { | ||
if (node.type === "comment") { | ||
return; | ||
var _loop = function _loop() { | ||
var node = nodes[index--]; | ||
if (node && node.type === 'rule' && node.selector === last.selector) { | ||
last.each(function (child) { | ||
if (child.type === 'decl') { | ||
dedupeNode(child, node.nodes); | ||
} | ||
}); | ||
if (empty(node)) { | ||
node.remove(); | ||
} | ||
} | ||
}; | ||
var str = node.toString(); | ||
var existing = map[str]; | ||
if (existing) { | ||
toRemove.push(existing); | ||
while (index >= 0) { | ||
_loop(); | ||
} | ||
} | ||
function dedupeNode(last, nodes) { | ||
var index = !!~nodes.indexOf(last) ? nodes.indexOf(last) - 1 : nodes.length - 1; | ||
while (index >= 0) { | ||
var _node = nodes[index--]; | ||
if (_node && equals(_node, last)) { | ||
_node.remove(); | ||
} | ||
map[str] = node; | ||
}); | ||
} | ||
} | ||
while (toRemove.length > 0) { | ||
toRemove.pop().remove(); | ||
var handlers = { | ||
rule: dedupeRule, | ||
atrule: dedupeNode, | ||
decl: dedupeNode, | ||
comment: noop | ||
}; | ||
function dedupe(root) { | ||
var nodes = root.nodes; | ||
if (!nodes) { | ||
return; | ||
} | ||
var index = nodes.length - 1; | ||
while (index >= 0) { | ||
var last = nodes[index--]; | ||
if (!last || !last.parent) { | ||
continue; | ||
} | ||
dedupe(last); | ||
handlers[last.type](last, nodes); | ||
} | ||
} | ||
@@ -39,0 +135,0 @@ |
{ | ||
"name": "postcss-discard-duplicates", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Discard duplicate rules in your CSS files with PostCSS.", | ||
@@ -31,3 +31,3 @@ "main": "dist/index.js", | ||
"all-contributors-cli": "^3.0.5", | ||
"ava": "^0.16.0", | ||
"ava": "^0.17.0", | ||
"babel-cli": "^6.3.17", | ||
@@ -34,0 +34,0 @@ "babel-core": "^6.3.26", |
@@ -64,4 +64,4 @@ # [postcss][postcss]-discard-duplicates [![Build Status](https://travis-ci.org/ben-eb/postcss-discard-duplicates.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-discard-duplicates.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-discard-duplicates.svg)][deps] | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
| [<img src="https://avatars.githubusercontent.com/u/1282980?v=3" width="100px;"/><br /><sub>Ben Briggs</sub>](http://beneb.info)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) | [<img src="https://avatars.githubusercontent.com/u/5635476?v=3" width="100px;"/><br /><sub>Bogdan Chadkin</sub>](https://github.com/TrySound)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=TrySound) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/68302?v=3" width="100px;"/><br /><sub>Lee Houghton</sub>](https://github.com/asztal)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=asztal) | | ||
| :---: | :---: | :---: | | ||
| [<img src="https://avatars.githubusercontent.com/u/1282980?v=3" width="100px;"/><br /><sub>Ben Briggs</sub>](http://beneb.info)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=ben-eb) | [<img src="https://avatars.githubusercontent.com/u/5635476?v=3" width="100px;"/><br /><sub>Bogdan Chadkin</sub>](https://github.com/TrySound)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=TrySound) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/68302?v=3" width="100px;"/><br /><sub>Lee Houghton</sub>](https://github.com/asztal)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=asztal) | [<img src="https://avatars.githubusercontent.com/u/1737375?v=3" width="100px;"/><br /><sub>Andy Jansson</sub>](https://github.com/andyjansson)<br />[💻](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=andyjansson) [⚠️](https://github.com/ben-eb/postcss-discard-duplicates/commits?author=andyjansson) | | ||
| :---: | :---: | :---: | :---: | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
@@ -68,0 +68,0 @@ |
10445
112