| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'removes useless stroke and fill attributes';
|
|---|
| 8 |
|
|---|
| 9 | exports.params = {
|
|---|
| 10 | stroke: true,
|
|---|
| 11 | fill: true,
|
|---|
| 12 | removeNone: false,
|
|---|
| 13 | hasStyleOrScript: false
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | var shape = require('./_collections').elemsGroups.shape,
|
|---|
| 17 | regStrokeProps = /^stroke/,
|
|---|
| 18 | regFillProps = /^fill-/,
|
|---|
| 19 | styleOrScript = ['style', 'script'];
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Remove useless stroke and fill attrs.
|
|---|
| 23 | *
|
|---|
| 24 | * @param {Object} item current iteration item
|
|---|
| 25 | * @param {Object} params plugin params
|
|---|
| 26 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 27 | *
|
|---|
| 28 | * @author Kir Belevich
|
|---|
| 29 | */
|
|---|
| 30 | exports.fn = function(item, params) {
|
|---|
| 31 |
|
|---|
| 32 | if (item.isElem(styleOrScript)) {
|
|---|
| 33 | params.hasStyleOrScript = true;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | if (!params.hasStyleOrScript && item.isElem(shape) && !item.computedAttr('id')) {
|
|---|
| 37 |
|
|---|
| 38 | var stroke = params.stroke && item.computedAttr('stroke'),
|
|---|
| 39 | fill = params.fill && !item.computedAttr('fill', 'none');
|
|---|
| 40 |
|
|---|
| 41 | // remove stroke*
|
|---|
| 42 | if (
|
|---|
| 43 | params.stroke &&
|
|---|
| 44 | (!stroke ||
|
|---|
| 45 | stroke == 'none' ||
|
|---|
| 46 | item.computedAttr('stroke-opacity', '0') ||
|
|---|
| 47 | item.computedAttr('stroke-width', '0')
|
|---|
| 48 | )
|
|---|
| 49 | ) {
|
|---|
| 50 | var parentStroke = item.parentNode.computedAttr('stroke'),
|
|---|
| 51 | declineStroke = parentStroke && parentStroke != 'none';
|
|---|
| 52 |
|
|---|
| 53 | item.eachAttr(function(attr) {
|
|---|
| 54 | if (regStrokeProps.test(attr.name)) {
|
|---|
| 55 | item.removeAttr(attr.name);
|
|---|
| 56 | }
|
|---|
| 57 | });
|
|---|
| 58 |
|
|---|
| 59 | if (declineStroke) item.addAttr({
|
|---|
| 60 | name: 'stroke',
|
|---|
| 61 | value: 'none',
|
|---|
| 62 | prefix: '',
|
|---|
| 63 | local: 'stroke'
|
|---|
| 64 | });
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // remove fill*
|
|---|
| 68 | if (
|
|---|
| 69 | params.fill &&
|
|---|
| 70 | (!fill || item.computedAttr('fill-opacity', '0'))
|
|---|
| 71 | ) {
|
|---|
| 72 | item.eachAttr(function(attr) {
|
|---|
| 73 | if (regFillProps.test(attr.name)) {
|
|---|
| 74 | item.removeAttr(attr.name);
|
|---|
| 75 | }
|
|---|
| 76 | });
|
|---|
| 77 |
|
|---|
| 78 | if (fill) {
|
|---|
| 79 | if (item.hasAttr('fill'))
|
|---|
| 80 | item.attr('fill').value = 'none';
|
|---|
| 81 | else
|
|---|
| 82 | item.addAttr({
|
|---|
| 83 | name: 'fill',
|
|---|
| 84 | value: 'none',
|
|---|
| 85 | prefix: '',
|
|---|
| 86 | local: 'fill'
|
|---|
| 87 | });
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (params.removeNone &&
|
|---|
| 92 | (!stroke || item.hasAttr('stroke') && item.attr('stroke').value=='none') &&
|
|---|
| 93 | (!fill || item.hasAttr('fill') && item.attr('fill').value=='none')) {
|
|---|
| 94 |
|
|---|
| 95 | return false;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | };
|
|---|