| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'full';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'remove or cleanup enable-background attribute when possible';
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Remove or cleanup enable-background attr which coincides with a width/height box.
|
|---|
| 11 | *
|
|---|
| 12 | * @see http://www.w3.org/TR/SVG/filters.html#EnableBackgroundProperty
|
|---|
| 13 | *
|
|---|
| 14 | * @example
|
|---|
| 15 | * <svg width="100" height="50" enable-background="new 0 0 100 50">
|
|---|
| 16 | * ⬇
|
|---|
| 17 | * <svg width="100" height="50">
|
|---|
| 18 | *
|
|---|
| 19 | * @param {Object} item current iteration item
|
|---|
| 20 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 21 | *
|
|---|
| 22 | * @author Kir Belevich
|
|---|
| 23 | */
|
|---|
| 24 | exports.fn = function(data) {
|
|---|
| 25 |
|
|---|
| 26 | var regEnableBackground = /^new\s0\s0\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)$/,
|
|---|
| 27 | hasFilter = false,
|
|---|
| 28 | elems = ['svg', 'mask', 'pattern'];
|
|---|
| 29 |
|
|---|
| 30 | function checkEnableBackground(item) {
|
|---|
| 31 | if (
|
|---|
| 32 | item.isElem(elems) &&
|
|---|
| 33 | item.hasAttr('enable-background') &&
|
|---|
| 34 | item.hasAttr('width') &&
|
|---|
| 35 | item.hasAttr('height')
|
|---|
| 36 | ) {
|
|---|
| 37 |
|
|---|
| 38 | var match = item.attr('enable-background').value.match(regEnableBackground);
|
|---|
| 39 |
|
|---|
| 40 | if (match) {
|
|---|
| 41 | if (
|
|---|
| 42 | item.attr('width').value === match[1] &&
|
|---|
| 43 | item.attr('height').value === match[3]
|
|---|
| 44 | ) {
|
|---|
| 45 | if (item.isElem('svg')) {
|
|---|
| 46 | item.removeAttr('enable-background');
|
|---|
| 47 | } else {
|
|---|
| 48 | item.attr('enable-background').value = 'new';
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | function checkForFilter(item) {
|
|---|
| 57 | if (item.isElem('filter')) {
|
|---|
| 58 | hasFilter = true;
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | function monkeys(items, fn) {
|
|---|
| 63 | items.content.forEach(function(item) {
|
|---|
| 64 | fn(item);
|
|---|
| 65 |
|
|---|
| 66 | if (item.content) {
|
|---|
| 67 | monkeys(item, fn);
|
|---|
| 68 | }
|
|---|
| 69 | });
|
|---|
| 70 | return items;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | var firstStep = monkeys(data, function(item) {
|
|---|
| 74 | checkEnableBackground(item);
|
|---|
| 75 | if (!hasFilter) {
|
|---|
| 76 | checkForFilter(item);
|
|---|
| 77 | }
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | return hasFilter ? firstStep : monkeys(firstStep, function(item) {
|
|---|
| 81 | //we don't need 'enable-background' if we have no filters
|
|---|
| 82 | item.removeAttr('enable-background');
|
|---|
| 83 | });
|
|---|
| 84 | };
|
|---|