| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = false;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'removes attributes of elements that match a css selector';
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Removes attributes of elements that match a css selector.
|
|---|
| 12 | *
|
|---|
| 13 | * @param {Object} item current iteration item
|
|---|
| 14 | * @param {Object} params plugin params
|
|---|
| 15 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 16 | *
|
|---|
| 17 | * @example
|
|---|
| 18 | * <caption>A selector removing a single attribute</caption>
|
|---|
| 19 | * plugins:
|
|---|
| 20 | * - removeAttributesBySelector:
|
|---|
| 21 | * selector: "[fill='#00ff00']"
|
|---|
| 22 | * attributes: "fill"
|
|---|
| 23 | *
|
|---|
| 24 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
|---|
| 25 | * ↓
|
|---|
| 26 | * <rect x="0" y="0" width="100" height="100" stroke="#00ff00"/>
|
|---|
| 27 | *
|
|---|
| 28 | * <caption>A selector removing multiple attributes</caption>
|
|---|
| 29 | * plugins:
|
|---|
| 30 | * - removeAttributesBySelector:
|
|---|
| 31 | * selector: "[fill='#00ff00']"
|
|---|
| 32 | * attributes:
|
|---|
| 33 | * - fill
|
|---|
| 34 | * - stroke
|
|---|
| 35 | *
|
|---|
| 36 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
|---|
| 37 | * ↓
|
|---|
| 38 | * <rect x="0" y="0" width="100" height="100"/>
|
|---|
| 39 | *
|
|---|
| 40 | * <caption>Multiple selectors removing attributes</caption>
|
|---|
| 41 | * plugins:
|
|---|
| 42 | * - removeAttributesBySelector:
|
|---|
| 43 | * selectors:
|
|---|
| 44 | * - selector: "[fill='#00ff00']"
|
|---|
| 45 | * attributes: "fill"
|
|---|
| 46 | *
|
|---|
| 47 | * - selector: "#remove"
|
|---|
| 48 | * attributes:
|
|---|
| 49 | * - stroke
|
|---|
| 50 | * - id
|
|---|
| 51 | *
|
|---|
| 52 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
|---|
| 53 | * ↓
|
|---|
| 54 | * <rect x="0" y="0" width="100" height="100"/>
|
|---|
| 55 | *
|
|---|
| 56 | * @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors|MDN CSS Selectors}
|
|---|
| 57 | *
|
|---|
| 58 | * @author Bradley Mease
|
|---|
| 59 | */
|
|---|
| 60 | exports.fn = function(item, params) {
|
|---|
| 61 |
|
|---|
| 62 | var selectors = Array.isArray(params.selectors) ? params.selectors : [params];
|
|---|
| 63 |
|
|---|
| 64 | selectors.map(function(i) {
|
|---|
| 65 | if (item.matches(i.selector)) {
|
|---|
| 66 | item.removeAttr(i.attributes);
|
|---|
| 67 | }
|
|---|
| 68 | });
|
|---|
| 69 |
|
|---|
| 70 | };
|
|---|