| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var DEFAULT_SEPARATOR = ':';
|
|---|
| 4 |
|
|---|
| 5 | exports.type = 'perItem';
|
|---|
| 6 |
|
|---|
| 7 | exports.active = false;
|
|---|
| 8 |
|
|---|
| 9 | exports.description = 'removes specified attributes';
|
|---|
| 10 |
|
|---|
| 11 | exports.params = {
|
|---|
| 12 | elemSeparator: DEFAULT_SEPARATOR,
|
|---|
| 13 | preserveCurrentColor: false,
|
|---|
| 14 | attrs: []
|
|---|
| 15 | };
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Remove attributes
|
|---|
| 19 | *
|
|---|
| 20 | * @param elemSeparator
|
|---|
| 21 | * format: string
|
|---|
| 22 | *
|
|---|
| 23 | * @param preserveCurrentColor
|
|---|
| 24 | * format: boolean
|
|---|
| 25 | *
|
|---|
| 26 | * @param attrs:
|
|---|
| 27 | *
|
|---|
| 28 | * format: [ element* : attribute* : value* ]
|
|---|
| 29 | *
|
|---|
| 30 | * element : regexp (wrapped into ^...$), single * or omitted > all elements (must be present when value is used)
|
|---|
| 31 | * attribute : regexp (wrapped into ^...$)
|
|---|
| 32 | * value : regexp (wrapped into ^...$), single * or omitted > all values
|
|---|
| 33 | *
|
|---|
| 34 | * examples:
|
|---|
| 35 | *
|
|---|
| 36 | * > basic: remove fill attribute
|
|---|
| 37 | * ---
|
|---|
| 38 | * removeAttrs:
|
|---|
| 39 | * attrs: 'fill'
|
|---|
| 40 | *
|
|---|
| 41 | * > remove fill attribute on path element
|
|---|
| 42 | * ---
|
|---|
| 43 | * attrs: 'path:fill'
|
|---|
| 44 | *
|
|---|
| 45 | * > remove fill attribute on path element where value is none
|
|---|
| 46 | * ---
|
|---|
| 47 | * attrs: 'path:fill:none'
|
|---|
| 48 | *
|
|---|
| 49 | *
|
|---|
| 50 | * > remove all fill and stroke attribute
|
|---|
| 51 | * ---
|
|---|
| 52 | * attrs:
|
|---|
| 53 | * - 'fill'
|
|---|
| 54 | * - 'stroke'
|
|---|
| 55 | *
|
|---|
| 56 | * [is same as]
|
|---|
| 57 | *
|
|---|
| 58 | * attrs: '(fill|stroke)'
|
|---|
| 59 | *
|
|---|
| 60 | * [is same as]
|
|---|
| 61 | *
|
|---|
| 62 | * attrs: '*:(fill|stroke)'
|
|---|
| 63 | *
|
|---|
| 64 | * [is same as]
|
|---|
| 65 | *
|
|---|
| 66 | * attrs: '.*:(fill|stroke)'
|
|---|
| 67 | *
|
|---|
| 68 | * [is same as]
|
|---|
| 69 | *
|
|---|
| 70 | * attrs: '.*:(fill|stroke):.*'
|
|---|
| 71 | *
|
|---|
| 72 | *
|
|---|
| 73 | * > remove all stroke related attributes
|
|---|
| 74 | * ----
|
|---|
| 75 | * attrs: 'stroke.*'
|
|---|
| 76 | *
|
|---|
| 77 | *
|
|---|
| 78 | * @param {Object} item current iteration item
|
|---|
| 79 | * @param {Object} params plugin params
|
|---|
| 80 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 81 | *
|
|---|
| 82 | * @author Benny Schudel
|
|---|
| 83 | */
|
|---|
| 84 | exports.fn = function(item, params) {
|
|---|
| 85 | // wrap into an array if params is not
|
|---|
| 86 | if (!Array.isArray(params.attrs)) {
|
|---|
| 87 | params.attrs = [params.attrs];
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | if (item.isElem()) {
|
|---|
| 91 | var elemSeparator = typeof params.elemSeparator == 'string' ? params.elemSeparator : DEFAULT_SEPARATOR;
|
|---|
| 92 | var preserveCurrentColor = typeof params.preserveCurrentColor == 'boolean' ? params.preserveCurrentColor : false;
|
|---|
| 93 |
|
|---|
| 94 | // prepare patterns
|
|---|
| 95 | var patterns = params.attrs.map(function(pattern) {
|
|---|
| 96 |
|
|---|
| 97 | // if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
|
|---|
| 98 | if (pattern.indexOf(elemSeparator) === -1) {
|
|---|
| 99 | pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join('');
|
|---|
| 100 |
|
|---|
| 101 | // if only 1 separator, assume it's element and attribute name, and apply regardless of attribute value
|
|---|
| 102 | } else if (pattern.split(elemSeparator).length < 3) {
|
|---|
| 103 | pattern = [pattern, elemSeparator, '.*'].join('');
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // create regexps for element, attribute name, and attribute value
|
|---|
| 107 | return pattern.split(elemSeparator)
|
|---|
| 108 | .map(function(value) {
|
|---|
| 109 |
|
|---|
| 110 | // adjust single * to match anything
|
|---|
| 111 | if (value === '*') { value = '.*'; }
|
|---|
| 112 |
|
|---|
| 113 | return new RegExp(['^', value, '$'].join(''), 'i');
|
|---|
| 114 | });
|
|---|
| 115 |
|
|---|
| 116 | });
|
|---|
| 117 |
|
|---|
| 118 | // loop patterns
|
|---|
| 119 | patterns.forEach(function(pattern) {
|
|---|
| 120 |
|
|---|
| 121 | // matches element
|
|---|
| 122 | if (pattern[0].test(item.elem)) {
|
|---|
| 123 |
|
|---|
| 124 | // loop attributes
|
|---|
| 125 | item.eachAttr(function(attr) {
|
|---|
| 126 | var name = attr.name;
|
|---|
| 127 | var value = attr.value;
|
|---|
| 128 | var isFillCurrentColor = preserveCurrentColor && name == 'fill' && value == 'currentColor';
|
|---|
| 129 | var isStrokeCurrentColor = preserveCurrentColor && name == 'stroke' && value == 'currentColor';
|
|---|
| 130 |
|
|---|
| 131 | if (!(isFillCurrentColor || isStrokeCurrentColor)) {
|
|---|
| 132 | // matches attribute name
|
|---|
| 133 | if (pattern[1].test(name)) {
|
|---|
| 134 |
|
|---|
| 135 | // matches attribute value
|
|---|
| 136 | if (pattern[2].test(attr.value)) {
|
|---|
| 137 | item.removeAttr(name);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | });
|
|---|
| 143 |
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | });
|
|---|
| 147 |
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | };
|
|---|