-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.ts
149 lines (123 loc) · 4.1 KB
/
index.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type { Plugin, PluginCreator } from 'postcss';
import parser from 'postcss-selector-parser';
import isValidReplacement from './is-valid-replacement.mjs';
/** css-blank-pseudo plugin options */
export type pluginOptions = {
/** Preserve the original notation. default: true */
preserve?: boolean,
/** Replacement for ":blank". default: "[blank]" */
replaceWith?: string,
/** Do not inject "js-blank-pseudo" before each selector with "[blank]". default: false */
disablePolyfillReadyClass?: boolean,
};
const POLYFILL_READY_CLASSNAME = 'js-blank-pseudo';
const PSEUDO = ':blank';
const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
const options = Object.assign(
// Default options
{
preserve: true,
replaceWith: '[blank]',
disablePolyfillReadyClass: false,
},
// Provided options
opts,
);
const replacementAST = parser().astSync(options.replaceWith);
if (!isValidReplacement(options.replaceWith)) {
return {
postcssPlugin: 'css-blank-pseudo',
Once(root, { result }): void {
root.warn(
result,
`${options.replaceWith} is not a valid replacement since it can't be applied to single elements.`,
);
},
};
}
return {
postcssPlugin: 'css-blank-pseudo',
prepare(): Plugin {
const transformedNodes = new WeakSet();
return {
postcssPlugin: 'css-blank-pseudo',
Rule(rule, { result }): void {
if (transformedNodes.has(rule)) {
return;
}
if (!rule.selector.toLowerCase().includes(PSEUDO)) {
return;
}
const selectors = rule.selectors.flatMap((selector) => {
if (!selector.toLowerCase().includes(PSEUDO)) {
return [selector];
}
let selectorAST;
try {
selectorAST = parser().astSync(selector);
} catch (err) {
rule.warn(result, `Failed to parse selector : "${selector}" with message: "${(err instanceof Error) ? err.message : err}"`);
return [selector];
}
if (typeof selectorAST === 'undefined') {
return [selector];
}
let containsPseudo = false;
selectorAST.walkPseudos((pseudo) => {
if (pseudo.value.toLowerCase() !== PSEUDO) {
return;
}
if (pseudo.nodes && pseudo.nodes.length) {
return;
}
containsPseudo = true;
pseudo.replaceWith(replacementAST.clone({}));
});
if (!containsPseudo) {
return [selector];
}
const selectorASTClone = selectorAST.clone();
// html > .foo:focus-within
// becomes:
// html.js-blank-pseudo > .foo:focus-within,
// .js-blank-pseudo html > .foo:focus-within
if (!options.disablePolyfillReadyClass) {
if (selectorAST.nodes?.[0]?.nodes?.length) {
for (let i = 0; i < selectorAST.nodes[0].nodes.length; i++) {
const node = selectorAST.nodes[0].nodes[i];
if (node.type === 'combinator' || parser.isPseudoElement(node)) {
// Insert the class before the first combinator or pseudo element.
selectorAST.nodes[0].insertBefore(node, parser.className({ value: POLYFILL_READY_CLASSNAME }));
break;
}
if (i === selectorAST.nodes[0].nodes.length - 1) {
// Append the class to the end of the selector if no combinator or pseudo element was found.
selectorAST.nodes[0].append(parser.className({ value: POLYFILL_READY_CLASSNAME }));
break;
}
}
}
if (selectorAST.nodes?.[0]?.nodes) {
// Prepend a space combinator and the class to the beginning of the selector.
selectorASTClone.nodes[0].prepend(parser.combinator({ value: ' ' }));
selectorASTClone.nodes[0].prepend(parser.className({ value: POLYFILL_READY_CLASSNAME }));
}
return [selectorAST.toString(), selectorASTClone.toString()];
}
return [selectorAST.toString()];
});
if (selectors.join(',') === rule.selectors.join(',')) {
return;
}
transformedNodes.add(rule);
rule.cloneBefore({ selectors: selectors });
if (!options.preserve) {
rule.remove();
}
},
};
},
};
};
creator.postcss = true;
export default creator;