[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const { querySelectorAll } = require('../lib/xast.js');
|
---|
| 4 |
|
---|
| 5 | exports.name = 'removeAttributesBySelector';
|
---|
| 6 | exports.type = 'visitor';
|
---|
| 7 | exports.active = false;
|
---|
| 8 | exports.description =
|
---|
| 9 | 'removes attributes of elements that match a css selector';
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Removes attributes of elements that match a css selector.
|
---|
| 13 | *
|
---|
| 14 | * @example
|
---|
| 15 | * <caption>A selector removing a single attribute</caption>
|
---|
| 16 | * plugins: [
|
---|
| 17 | * {
|
---|
| 18 | * name: "removeAttributesBySelector",
|
---|
| 19 | * params: {
|
---|
| 20 | * selector: "[fill='#00ff00']"
|
---|
| 21 | * attributes: "fill"
|
---|
| 22 | * }
|
---|
| 23 | * }
|
---|
| 24 | * ]
|
---|
| 25 | *
|
---|
| 26 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
---|
| 27 | * ↓
|
---|
| 28 | * <rect x="0" y="0" width="100" height="100" stroke="#00ff00"/>
|
---|
| 29 | *
|
---|
| 30 | * <caption>A selector removing multiple attributes</caption>
|
---|
| 31 | * plugins: [
|
---|
| 32 | * {
|
---|
| 33 | * name: "removeAttributesBySelector",
|
---|
| 34 | * params: {
|
---|
| 35 | * selector: "[fill='#00ff00']",
|
---|
| 36 | * attributes: [
|
---|
| 37 | * "fill",
|
---|
| 38 | * "stroke"
|
---|
| 39 | * ]
|
---|
| 40 | * }
|
---|
| 41 | * }
|
---|
| 42 | * ]
|
---|
| 43 | *
|
---|
| 44 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
---|
| 45 | * ↓
|
---|
| 46 | * <rect x="0" y="0" width="100" height="100"/>
|
---|
| 47 | *
|
---|
| 48 | * <caption>Multiple selectors removing attributes</caption>
|
---|
| 49 | * plugins: [
|
---|
| 50 | * {
|
---|
| 51 | * name: "removeAttributesBySelector",
|
---|
| 52 | * params: {
|
---|
| 53 | * selectors: [
|
---|
| 54 | * {
|
---|
| 55 | * selector: "[fill='#00ff00']",
|
---|
| 56 | * attributes: "fill"
|
---|
| 57 | * },
|
---|
| 58 | * {
|
---|
| 59 | * selector: "#remove",
|
---|
| 60 | * attributes: [
|
---|
| 61 | * "stroke",
|
---|
| 62 | * "id"
|
---|
| 63 | * ]
|
---|
| 64 | * }
|
---|
| 65 | * ]
|
---|
| 66 | * }
|
---|
| 67 | * }
|
---|
| 68 | * ]
|
---|
| 69 | *
|
---|
| 70 | * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
|
---|
| 71 | * ↓
|
---|
| 72 | * <rect x="0" y="0" width="100" height="100"/>
|
---|
| 73 | *
|
---|
| 74 | * @link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors|MDN CSS Selectors
|
---|
| 75 | *
|
---|
| 76 | * @author Bradley Mease
|
---|
| 77 | *
|
---|
| 78 | * @type {import('../lib/types').Plugin<any>}
|
---|
| 79 | */
|
---|
| 80 | exports.fn = (root, params) => {
|
---|
| 81 | const selectors = Array.isArray(params.selectors)
|
---|
| 82 | ? params.selectors
|
---|
| 83 | : [params];
|
---|
| 84 | for (const { selector, attributes } of selectors) {
|
---|
| 85 | const nodes = querySelectorAll(root, selector);
|
---|
| 86 | for (const node of nodes) {
|
---|
| 87 | if (node.type === 'element') {
|
---|
| 88 | if (Array.isArray(attributes)) {
|
---|
| 89 | for (const name of attributes) {
|
---|
| 90 | delete node.attributes[name];
|
---|
| 91 | }
|
---|
| 92 | } else {
|
---|
| 93 | delete node.attributes[attributes];
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | return {};
|
---|
| 99 | };
|
---|