diff --git a/plugins/cleanupEnableBackground.js b/plugins/cleanupEnableBackground.js
index f27c5b38..119018c2 100644
--- a/plugins/cleanupEnableBackground.js
+++ b/plugins/cleanupEnableBackground.js
@@ -1,12 +1,9 @@
'use strict';
-exports.type = 'perItem';
+exports.type = 'full';
exports.active = true;
-var regEnableBackground = /^new\s0\s0\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)$/,
- elems = ['svg', 'mask', 'pattern'];
-
/**
* Remove or cleanup enable-background attr which coincides with a width/height box.
*
@@ -22,30 +19,64 @@ var regEnableBackground = /^new\s0\s0\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s([\-+]
*
* @author Kir Belevich
*/
-exports.fn = function(item) {
+exports.fn = function(data) {
- if (
- item.isElem(elems) &&
- item.hasAttr('enable-background') &&
- item.hasAttr('width') &&
- item.hasAttr('height')
- ) {
+ var regEnableBackground = /^new\s0\s0\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)$/,
+ hasFilter = false,
+ elems = ['svg', 'mask', 'pattern'];
- var match = item.attr('enable-background').value.match(regEnableBackground);
+ function checkEnableBackground(item) {
+ if (
+ item.isElem(elems) &&
+ item.hasAttr('enable-background') &&
+ item.hasAttr('width') &&
+ item.hasAttr('height')
+ ) {
- if (match) {
- if (
- item.attr('width').value === match[1] &&
- item.attr('height').value === match[3]
- ) {
- if (item.isElem('svg')) {
- item.removeAttr('enable-background');
- } else {
- item.attr('enable-background').value = 'new';
+ var match = item.attr('enable-background').value.match(regEnableBackground);
+
+ if (match) {
+ if (
+ item.attr('width').value === match[1] &&
+ item.attr('height').value === match[3]
+ ) {
+ if (item.isElem('svg')) {
+ item.removeAttr('enable-background');
+ } else {
+ item.attr('enable-background').value = 'new';
+ }
}
}
- }
+ }
}
+ function checkForFilter(item) {
+ if (item.isElem('filter')) {
+ hasFilter = true;
+ }
+ }
+
+ function monkeys(items, fn) {
+ items.content.forEach(function(item) {
+ fn(item);
+
+ if (item.content) {
+ monkeys(item, fn);
+ }
+ });
+ return items;
+ }
+
+ var firstStep = monkeys(data, function(item) {
+ checkEnableBackground(item);
+ if (!hasFilter) {
+ checkForFilter(item);
+ }
+ });
+
+ return hasFilter ? firstStep : monkeys(firstStep, function(item) {
+ //we don't need 'enable-background' if we have no filters
+ item.removeAttr('enable-background');
+ });
};
diff --git a/test/plugins/cleanupEnableBackground.01.svg b/test/plugins/cleanupEnableBackground.01.svg
index 2bbb8d46..a3038c5b 100644
--- a/test/plugins/cleanupEnableBackground.01.svg
+++ b/test/plugins/cleanupEnableBackground.01.svg
@@ -1,9 +1,19 @@
@@@
diff --git a/test/plugins/cleanupEnableBackground.02.svg b/test/plugins/cleanupEnableBackground.02.svg
index 7cac6c54..acafc83b 100644
--- a/test/plugins/cleanupEnableBackground.02.svg
+++ b/test/plugins/cleanupEnableBackground.02.svg
@@ -1,9 +1,19 @@
@@@
diff --git a/test/plugins/cleanupEnableBackground.03.svg b/test/plugins/cleanupEnableBackground.03.svg
index 9b505601..09112faa 100644
--- a/test/plugins/cleanupEnableBackground.03.svg
+++ b/test/plugins/cleanupEnableBackground.03.svg
@@ -1,4 +1,9 @@