Skip to content

Commit 447f82c

Browse files
authored
Convert addAttributesToSVGElement to item plugin (#1448)
"full" plugins prevents from possible optimisation. We need to migrate all plugins to "perItem" type and later implement visitor plugin api to allow state.
1 parent 13a0ad0 commit 447f82c

File tree

5 files changed

+55
-20
lines changed

5 files changed

+55
-20
lines changed

plugins/addAttributesToSVGElement.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

3-
exports.type = 'full';
3+
const { closestByName } = require('../lib/xast.js');
4+
5+
exports.type = 'perItem';
46

57
exports.active = false;
68

@@ -50,30 +52,32 @@ plugins: [
5052
*
5153
* @author April Arcus
5254
*/
53-
exports.fn = function (data, params) {
54-
if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
55-
console.error(ENOCLS);
56-
return data;
57-
}
55+
exports.fn = (node, params) => {
56+
if (
57+
node.type === 'element' &&
58+
node.name === 'svg' &&
59+
closestByName(node.parentNode, 'svg') == null
60+
) {
61+
if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
62+
console.error(ENOCLS);
63+
return;
64+
}
5865

59-
var attributes = params.attributes || [params.attribute],
60-
svg = data.children[0];
66+
const attributes = params.attributes || [params.attribute];
6167

62-
if (svg.isElem('svg')) {
63-
attributes.forEach(function (attribute) {
68+
for (const attribute of attributes) {
6469
if (typeof attribute === 'string') {
65-
if (svg.attributes[attribute] == null) {
66-
svg.attributes[attribute] = undefined;
70+
if (node.attributes[attribute] == null) {
71+
node.attributes[attribute] = undefined;
6772
}
68-
} else if (typeof attribute === 'object') {
69-
Object.keys(attribute).forEach(function (key) {
70-
if (svg.attributes[key] == null) {
71-
svg.attributes[key] = attribute[key];
73+
}
74+
if (typeof attribute === 'object') {
75+
for (const key of Object.keys(attribute)) {
76+
if (node.attributes[key] == null) {
77+
node.attributes[key] = attribute[key];
7278
}
73-
});
79+
}
7480
}
75-
});
81+
}
7682
}
77-
78-
return data;
7983
};

test/plugins/addAttributesToSVGElement.01.svg

Lines changed: 4 additions & 0 deletions
Loading

test/plugins/addAttributesToSVGElement.02.svg

Lines changed: 4 additions & 0 deletions
Loading

test/plugins/addAttributesToSVGElement.03.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 19 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)