-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
132 lines (108 loc) · 2.97 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
module.exports = (options = {}) => {
const defaults = {
// The selector we're working within.
scopeTo: '.editor-styles-wrapper',
// Increase specificity by repeating the selector.
repeat: 1,
remove: ['html'],
replace: ['body'],
ignore: [':root'],
tags: [
'a',
'svg',
'a:hover',
'a:focus',
'button',
'button:hover',
'button:focus',
'input',
'label',
'select',
'textarea',
'form',
'input[type="button"]',
'input[type="submit"]',
'input[type="reset"]',
'[type="button"]',
'[type="submit"]',
'[type="reset"]'
],
tagSuffix:
':not([class^="components-"]):not([class^="editor-"]):not([class^="block-"]):not([aria-owns]):not([id^="mceu_"])'
};
const opts = { ...defaults, ...options };
// Detect if there is a :where() pseudo class in the selectors.
const hasWherePseudo = (optionsVal, selectors) => {
let optionHasWhere = false;
if (typeof optionsVal === 'string') {
optionHasWhere = ':where()' === optionsVal;
} else if (Array.isArray(optionsVal)) {
optionHasWhere = optionsVal.find((el) => el === ':where()');
}
if (!optionHasWhere) {
return false;
}
return selectors.find((el) => el.startsWith(':where('));
}
const firstOrLastSelector = (optsArray, selectorArray) => {
let firstSelector = selectorArray[0];
let lastSelector = selectorArray[selectorArray.length - 1];
const whereMatch = hasWherePseudo(optsArray, [firstSelector, lastSelector]);
var selectorIn = [];
if(whereMatch) {
selectorIn.push(whereMatch);
} else if (-1 !== optsArray.indexOf(firstSelector)) {
selectorIn.push(firstSelector);
} else if (-1 !== optsArray.indexOf(lastSelector)) {
selectorIn.push(lastSelector);
} else {
return false;
}
return selectorIn;
};
return {
postcssPlugin: 'postcss-editor-styles',
Root (root, postcss) {
root.walkRules(rule => {
rule.selectors = rule.selectors.map(selector => {
const selectArr = selector.split(' ');
let firstSelector = selectArr[0];
let lastSelector = selectArr[selectArr.length - 1];
if (
rule.parent.type === 'atrule' &&
rule.parent.name === 'keyframes'
) {
return selector;
}
if (firstOrLastSelector(opts.remove, selectArr)) {
return rule.remove();
}
if (firstOrLastSelector(opts.replace, selectArr)) {
const hasReplaceOpt = firstOrLastSelector(
opts.replace,
selectArr
);
return selector.replace(
RegExp(hasReplaceOpt, 'g'),
opts.scopeTo.repeat(opts.repeat)
);
}
if (firstOrLastSelector(opts.ignore, selectArr)) {
return selector;
}
if (-1 != opts.tags.indexOf(lastSelector)) {
return `${opts.scopeTo.repeat(opts.repeat)} ${selector}${
opts.tagSuffix
}`;
}
if (firstOrLastSelector([opts.scopeTo], selectArr)) {
return selector;
}
// For anything else add it before the selector.
return `${opts.scopeTo.repeat(opts.repeat)} ${selector}`;
});
});
}
};
};
module.exports.postcss = true;