-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
removeDeprecatedAttrs.js
120 lines (104 loc) · 2.88 KB
/
removeDeprecatedAttrs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as csswhat from 'css-what';
import { attrsGroupsDeprecated, elems } from './_collections.js';
import { collectStylesheet } from '../lib/style.js';
export const name = 'removeDeprecatedAttrs';
export const description = 'removes deprecated attributes';
/**
* @typedef {{ safe?: Set<string>; unsafe?: Set<string> }} DeprecatedAttrs
* @typedef {import('../lib/types.js').XastElement} XastElement
*/
/**
* @param {import('../lib/types.js').Stylesheet} stylesheet
* @returns {Set<string>}
*/
function extractAttributesInStylesheet(stylesheet) {
const attributesInStylesheet = new Set();
stylesheet.rules.forEach((rule) => {
const selectors = csswhat.parse(rule.selector);
selectors.forEach((subselector) => {
subselector.forEach((segment) => {
if (segment.type !== 'attribute') {
return;
}
attributesInStylesheet.add(segment.name);
});
});
});
return attributesInStylesheet;
}
/**
* @param {XastElement} node
* @param {DeprecatedAttrs | undefined} deprecatedAttrs
* @param {import('./plugins-types.js').DefaultPlugins['removeDeprecatedAttrs']} params
* @param {Set<string>} attributesInStylesheet
*/
function processAttributes(
node,
deprecatedAttrs,
params,
attributesInStylesheet,
) {
if (!deprecatedAttrs) {
return;
}
if (deprecatedAttrs.safe) {
deprecatedAttrs.safe.forEach((name) => {
if (attributesInStylesheet.has(name)) {
return;
}
delete node.attributes[name];
});
}
if (params.removeUnsafe && deprecatedAttrs.unsafe) {
deprecatedAttrs.unsafe.forEach((name) => {
if (attributesInStylesheet.has(name)) {
return;
}
delete node.attributes[name];
});
}
}
/**
* Remove deprecated attributes.
*
* @type {import('./plugins-types.js').Plugin<'removeDeprecatedAttrs'>}
*/
export function fn(root, params) {
const stylesheet = collectStylesheet(root);
const attributesInStylesheet = extractAttributesInStylesheet(stylesheet);
return {
element: {
enter: (node) => {
const elemConfig = elems[node.name];
if (!elemConfig) {
return;
}
// Special cases
// Removing deprecated xml:lang is safe when the lang attribute exists.
if (
elemConfig.attrsGroups.has('core') &&
node.attributes['xml:lang'] &&
!attributesInStylesheet.has('xml:lang') &&
node.attributes['lang']
) {
delete node.attributes['xml:lang'];
}
// General cases
elemConfig.attrsGroups.forEach((attrsGroup) => {
processAttributes(
node,
attrsGroupsDeprecated[attrsGroup],
params,
attributesInStylesheet,
);
});
processAttributes(
node,
elemConfig.deprecated,
params,
attributesInStylesheet,
);
},
},
};
}