css-has-pseudo 2.0.0 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +100 -0
- package/README.md +12 -7
- package/browser.js +2 -2
- package/dist/browser-global.js +2 -0
- package/dist/browser-global.js.map +1 -0
- package/dist/browser.cjs +2 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.mjs +2 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/cli.cjs +3 -0
- package/dist/index.cjs +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +50 -56
- package/cli.js +0 -184
- package/index.js +0 -109
- package/index.js.map +0 -1
- package/index.mjs +0 -107
- package/index.mjs.map +0 -1
- package/postcss.js +0 -73
- package/postcss.js.map +0 -1
- package/postcss.mjs +0 -67
- package/postcss.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,74 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "css-has-pseudo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Style elements relative to other elements in CSS",
|
|
5
5
|
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
|
|
6
6
|
"license": "CC0-1.0",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/css-has-pseudo#readme",
|
|
8
|
+
"bugs": "https://github.com/csstools/postcss-plugins/issues",
|
|
9
|
+
"main": "dist/index.cjs",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"./browser": {
|
|
18
|
+
"import": "./dist/browser.mjs",
|
|
19
|
+
"require": "./dist/browser.cjs",
|
|
20
|
+
"default": "./dist/browser.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./browser-global": {
|
|
23
|
+
"default": "./dist/browser-global.js"
|
|
24
|
+
}
|
|
14
25
|
},
|
|
15
26
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"index.mjs.map",
|
|
22
|
-
"postcss.js",
|
|
23
|
-
"postcss.js.map",
|
|
24
|
-
"postcss.mjs",
|
|
25
|
-
"postcss.mjs.map"
|
|
27
|
+
"CHANGELOG.md",
|
|
28
|
+
"LICENSE.md",
|
|
29
|
+
"README.md",
|
|
30
|
+
"dist",
|
|
31
|
+
"browser.js"
|
|
26
32
|
],
|
|
33
|
+
"bin": {
|
|
34
|
+
"css-has-pseudo": "dist/cli.cjs"
|
|
35
|
+
},
|
|
27
36
|
"scripts": {
|
|
28
|
-
"build": "
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"test": "
|
|
36
|
-
"test:js": "eslint src/{*,**/*}.js --cache --ignore-path .gitignore --quiet",
|
|
37
|
-
"test:postcss": "postcss-tape --plugin postcss.js"
|
|
37
|
+
"build": "rollup -c ../../rollup/default.js && npm run copy-browser-scripts-to-old-location",
|
|
38
|
+
"clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
|
|
39
|
+
"copy-browser-scripts-to-old-location": "node -e \"fs.copyFileSync('./dist/browser-global.js', './browser.js')\"",
|
|
40
|
+
"lint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
|
|
41
|
+
"prepublishOnly": "npm run clean && npm run build && npm run test",
|
|
42
|
+
"stryker": "stryker run --logLevel error",
|
|
43
|
+
"test": "postcss-tape --ci && npm run test:exports",
|
|
44
|
+
"test:exports": "node ./test/_import.mjs && node ./test/_require.cjs"
|
|
38
45
|
},
|
|
39
46
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
41
|
-
},
|
|
42
|
-
"peerDependencies": {
|
|
43
|
-
"postcss": ">=8.3"
|
|
47
|
+
"node": "^12 || ^14 || >=16"
|
|
44
48
|
},
|
|
45
49
|
"dependencies": {
|
|
46
|
-
"postcss-selector-parser": "^6"
|
|
50
|
+
"postcss-selector-parser": "^6.0.8"
|
|
47
51
|
},
|
|
48
52
|
"devDependencies": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"@rollup/plugin-babel": "5.3.0",
|
|
52
|
-
"cross-env": "7.0.3",
|
|
53
|
-
"eslint": "7.32.0",
|
|
54
|
-
"postcss": "8.3.4",
|
|
55
|
-
"postcss-tape": "6.0.1",
|
|
56
|
-
"pre-commit": "1.2.2",
|
|
57
|
-
"rollup": "2.56.3",
|
|
58
|
-
"rollup-plugin-terser": "7.0.2"
|
|
53
|
+
"postcss": "^8.3.6",
|
|
54
|
+
"postcss-tape": "^6.0.1"
|
|
59
55
|
},
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"browser": true,
|
|
63
|
-
"es6": true,
|
|
64
|
-
"node": true
|
|
65
|
-
},
|
|
66
|
-
"extends": "eslint:recommended",
|
|
67
|
-
"parserOptions": {
|
|
68
|
-
"ecmaVersion": 2020,
|
|
69
|
-
"sourceType": "module"
|
|
70
|
-
},
|
|
71
|
-
"root": true
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"postcss": "^8.3"
|
|
72
58
|
},
|
|
73
59
|
"keywords": [
|
|
74
60
|
"postcss",
|
|
@@ -82,5 +68,13 @@
|
|
|
82
68
|
"descendant",
|
|
83
69
|
"pseudo",
|
|
84
70
|
"selector"
|
|
85
|
-
]
|
|
71
|
+
],
|
|
72
|
+
"repository": {
|
|
73
|
+
"type": "git",
|
|
74
|
+
"url": "https://github.com/csstools/postcss-plugins.git",
|
|
75
|
+
"directory": "plugins/css-has-pseudo"
|
|
76
|
+
},
|
|
77
|
+
"volta": {
|
|
78
|
+
"extends": "../../package.json"
|
|
79
|
+
}
|
|
86
80
|
}
|
package/cli.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
var fs = require('fs');
|
|
4
|
-
var parser = require('postcss-selector-parser');
|
|
5
|
-
|
|
6
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
-
|
|
8
|
-
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
9
|
-
var parser__default = /*#__PURE__*/_interopDefaultLegacy(parser);
|
|
10
|
-
|
|
11
|
-
const creator = (
|
|
12
|
-
/** @type {{ preserve: true | false }} */
|
|
13
|
-
opts) => {
|
|
14
|
-
opts = typeof opts === 'object' && opts || defaultOptions;
|
|
15
|
-
/** Whether the original rule should be preserved. */
|
|
16
|
-
|
|
17
|
-
const shouldPreserve = Boolean('preserve' in opts ? opts.preserve : true);
|
|
18
|
-
return {
|
|
19
|
-
postcssPlugin: 'css-has-pseudo',
|
|
20
|
-
Rule: rule => {
|
|
21
|
-
if (rule.selector.includes(':has(')) {
|
|
22
|
-
const fallbackSelector = getFallbackSelector(rule.selector);
|
|
23
|
-
if (shouldPreserve) rule.cloneBefore({
|
|
24
|
-
selector: fallbackSelector
|
|
25
|
-
});else rule.assign({
|
|
26
|
-
selector: fallbackSelector
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
creator.postcss = true;
|
|
34
|
-
|
|
35
|
-
const getFallbackSelector = (
|
|
36
|
-
/** @type {string} */
|
|
37
|
-
selectorText) => parser__default['default'](selectors => {
|
|
38
|
-
selectors.walkPseudos(selector => {
|
|
39
|
-
if (selector.value === ':has' && selector.nodes) {
|
|
40
|
-
const isNotHas = isParentInNotPseudo(selector);
|
|
41
|
-
selector.value = isNotHas ? ':not-has' : ':has';
|
|
42
|
-
const attribute = parser__default['default'].attribute({
|
|
43
|
-
attribute: getEscapedCss(String(selector))
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
if (isNotHas) {
|
|
47
|
-
selector.parent.parent.replaceWith(attribute);
|
|
48
|
-
} else {
|
|
49
|
-
selector.replaceWith(attribute);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}).processSync(selectorText);
|
|
54
|
-
/** Default options. */
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const defaultOptions = {
|
|
58
|
-
preserve: true
|
|
59
|
-
};
|
|
60
|
-
/** Returns the string as an escaped CSS identifier. */
|
|
61
|
-
|
|
62
|
-
const getEscapedCss = (
|
|
63
|
-
/** @type {string} */
|
|
64
|
-
value) => encodeURIComponent(value).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',').replace(/[():%[\],]/g, '\\$&');
|
|
65
|
-
/** Returns whether the selector is within a `:not` pseudo-class. */
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const isParentInNotPseudo = selector => {
|
|
69
|
-
var _selector$parent, _selector$parent$pare;
|
|
70
|
-
|
|
71
|
-
return ((_selector$parent = selector.parent) === null || _selector$parent === void 0 ? void 0 : (_selector$parent$pare = _selector$parent.parent) === null || _selector$parent$pare === void 0 ? void 0 : _selector$parent$pare.type) === 'pseudo' && selector.parent.parent.value === ':not';
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/* eslint no-console: 0 */
|
|
75
|
-
|
|
76
|
-
const fileRegExp = /^[\w/.]+$/;
|
|
77
|
-
const argRegExp = /^--(\w+)=("|')?(.+)\2$/;
|
|
78
|
-
const relaxedJsonPropRegExp = /(['"])?([a-z0-9A-Z_]+)(['"])?:/g;
|
|
79
|
-
const relaxedJsonValueRegExp = /("[a-z0-9A-Z_]+":\s*)(?!true|false|null|\d+)'?([A-z0-9]+)'?([,}])/g;
|
|
80
|
-
const argo = process.argv.slice(2).reduce((object, arg) => {
|
|
81
|
-
const argMatch = arg.match(argRegExp);
|
|
82
|
-
const fileMatch = arg.match(fileRegExp);
|
|
83
|
-
|
|
84
|
-
if (argMatch) {
|
|
85
|
-
object[argMatch[1]] = argMatch[3];
|
|
86
|
-
} else if (fileMatch) {
|
|
87
|
-
if (object.from === '<stdin>') {
|
|
88
|
-
object.from = arg;
|
|
89
|
-
} else if (object.to === '<stdout>') {
|
|
90
|
-
object.to = arg;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return object;
|
|
95
|
-
}, {
|
|
96
|
-
from: '<stdin>',
|
|
97
|
-
to: '<stdout>',
|
|
98
|
-
opts: 'null'
|
|
99
|
-
}); // get css from command line arguments or stdin
|
|
100
|
-
|
|
101
|
-
(argo.from === '<stdin>' ? getStdin() : readFile(argo.from)).then(css => {
|
|
102
|
-
if (argo.from === '<stdin>' && !css) {
|
|
103
|
-
console.log(['CSS Has Pseudo\n', ' Transforms CSS with :has {}\n', 'Usage:\n', ' css-has-pseudo source.css transformed.css', ' css-has-pseudo --from=source.css --to=transformed.css --opts={}', ' echo "body:has(:focus) {}" | css-has-pseudo\n'].join('\n'));
|
|
104
|
-
process.exit(0);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const pluginOpts = JSON.parse(argo.opts.replace(relaxedJsonPropRegExp, '"$2": ').replace(relaxedJsonValueRegExp, '$1"$2"$3'));
|
|
108
|
-
const processOptions = Object.assign({
|
|
109
|
-
from: argo.from,
|
|
110
|
-
to: argo.to || argo.from
|
|
111
|
-
}, argo.map ? {
|
|
112
|
-
map: JSON.parse(argo.map)
|
|
113
|
-
} : {});
|
|
114
|
-
const result = creator.process(css, processOptions, pluginOpts);
|
|
115
|
-
|
|
116
|
-
if (argo.to === '<stdout>') {
|
|
117
|
-
return result.css;
|
|
118
|
-
} else {
|
|
119
|
-
return writeFile(argo.to, result.css).then(() => `CSS was written to "${argo.to}"`);
|
|
120
|
-
}
|
|
121
|
-
}).catch(error => {
|
|
122
|
-
if (Object(error).name === 'CssSyntaxError') {
|
|
123
|
-
throw new Error(`PostCSS had trouble reading the file (${error.reason} on line ${error.line}, column ${error.column}).`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (Object(error).errno === -2) {
|
|
127
|
-
throw new Error(`Sorry, "${error.path}" could not be read.`);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
throw error;
|
|
131
|
-
}).then(result => {
|
|
132
|
-
console.log(result);
|
|
133
|
-
process.exit(0);
|
|
134
|
-
}, error => {
|
|
135
|
-
console.error(Object(error).message || 'Something bad happened and we don’t even know what it was.');
|
|
136
|
-
process.exit(1);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
function readFile(pathname) {
|
|
140
|
-
return new Promise((resolve, reject) => {
|
|
141
|
-
fs__default['default'].readFile(pathname, 'utf8', (error, data) => {
|
|
142
|
-
if (error) {
|
|
143
|
-
reject(error);
|
|
144
|
-
} else {
|
|
145
|
-
resolve(data);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function writeFile(pathname, data) {
|
|
152
|
-
return new Promise((resolve, reject) => {
|
|
153
|
-
fs__default['default'].writeFile(pathname, data, (error, content) => {
|
|
154
|
-
if (error) {
|
|
155
|
-
reject(error);
|
|
156
|
-
} else {
|
|
157
|
-
resolve(content);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function getStdin() {
|
|
164
|
-
return new Promise(resolve => {
|
|
165
|
-
let data = '';
|
|
166
|
-
|
|
167
|
-
if (process.stdin.isTTY) {
|
|
168
|
-
resolve(data);
|
|
169
|
-
} else {
|
|
170
|
-
process.stdin.setEncoding('utf8');
|
|
171
|
-
process.stdin.on('readable', () => {
|
|
172
|
-
let chunk;
|
|
173
|
-
|
|
174
|
-
while (chunk = process.stdin.read()) {
|
|
175
|
-
data += chunk;
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
process.stdin.on('end', () => {
|
|
179
|
-
resolve(data);
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
//# sourceMappingURL=cli.js.map
|
package/index.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function cssHasPseudo(document) {
|
|
4
|
-
const observedItems = []; // document.createAttribute() doesn't support `:` in the name. innerHTML does
|
|
5
|
-
|
|
6
|
-
const attributeElement = document.createElement('x'); // walk all stylesheets to collect observed css rules
|
|
7
|
-
|
|
8
|
-
[].forEach.call(document.styleSheets, walkStyleSheet);
|
|
9
|
-
transformObservedItems(); // observe DOM modifications that affect selectors
|
|
10
|
-
|
|
11
|
-
const mutationObserver = new MutationObserver(mutationsList => {
|
|
12
|
-
mutationsList.forEach(mutation => {
|
|
13
|
-
[].forEach.call(mutation.addedNodes || [], node => {
|
|
14
|
-
// walk stylesheets to collect observed css rules
|
|
15
|
-
if (node.nodeType === 1 && node.sheet) {
|
|
16
|
-
walkStyleSheet(node.sheet);
|
|
17
|
-
}
|
|
18
|
-
}); // transform observed css rules
|
|
19
|
-
|
|
20
|
-
cleanupObservedCssRules();
|
|
21
|
-
transformObservedItems();
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
mutationObserver.observe(document, {
|
|
25
|
-
childList: true,
|
|
26
|
-
subtree: true
|
|
27
|
-
}); // observe DOM events that affect pseudo-selectors
|
|
28
|
-
|
|
29
|
-
document.addEventListener('focus', transformObservedItems, true);
|
|
30
|
-
document.addEventListener('blur', transformObservedItems, true);
|
|
31
|
-
document.addEventListener('input', transformObservedItems); // transform observed css rules
|
|
32
|
-
|
|
33
|
-
function transformObservedItems() {
|
|
34
|
-
requestAnimationFrame(() => {
|
|
35
|
-
observedItems.forEach(item => {
|
|
36
|
-
const nodes = [];
|
|
37
|
-
[].forEach.call(document.querySelectorAll(item.scopeSelector), element => {
|
|
38
|
-
const nthChild = [].indexOf.call(element.parentNode.children, element) + 1;
|
|
39
|
-
const relativeSelectors = item.relativeSelectors.map(relativeSelector => item.scopeSelector + ':nth-child(' + nthChild + ') ' + relativeSelector).join(); // find any relative :has element from the :scope element
|
|
40
|
-
|
|
41
|
-
const relativeElement = element.parentNode.querySelector(relativeSelectors);
|
|
42
|
-
const shouldElementMatch = item.isNot ? !relativeElement : relativeElement;
|
|
43
|
-
|
|
44
|
-
if (shouldElementMatch) {
|
|
45
|
-
// memorize the node
|
|
46
|
-
nodes.push(element); // set an attribute with an irregular attribute name
|
|
47
|
-
// document.createAttribute() doesn't support special characters
|
|
48
|
-
|
|
49
|
-
attributeElement.innerHTML = '<x ' + item.attributeName + '>';
|
|
50
|
-
element.setAttributeNode(attributeElement.children[0].attributes[0].cloneNode()); // trigger a style refresh in IE and Edge
|
|
51
|
-
|
|
52
|
-
document.documentElement.style.zoom = 1;
|
|
53
|
-
document.documentElement.style.zoom = null;
|
|
54
|
-
}
|
|
55
|
-
}); // remove the encoded attribute from all nodes that no longer match them
|
|
56
|
-
|
|
57
|
-
item.nodes.forEach(node => {
|
|
58
|
-
if (nodes.indexOf(node) === -1) {
|
|
59
|
-
node.removeAttribute(item.attributeName); // trigger a style refresh in IE and Edge
|
|
60
|
-
|
|
61
|
-
document.documentElement.style.zoom = 1;
|
|
62
|
-
document.documentElement.style.zoom = null;
|
|
63
|
-
}
|
|
64
|
-
}); // update the
|
|
65
|
-
|
|
66
|
-
item.nodes = nodes;
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
} // remove any observed cssrules that no longer apply
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
function cleanupObservedCssRules() {
|
|
73
|
-
[].push.apply(observedItems, observedItems.splice(0).filter(item => item.rule.parentStyleSheet && item.rule.parentStyleSheet.ownerNode && document.documentElement.contains(item.rule.parentStyleSheet.ownerNode)));
|
|
74
|
-
} // walk a stylesheet to collect observed css rules
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
function walkStyleSheet(styleSheet) {
|
|
78
|
-
try {
|
|
79
|
-
// walk a css rule to collect observed css rules
|
|
80
|
-
[].forEach.call(styleSheet.cssRules || [], rule => {
|
|
81
|
-
if (rule.selectorText) {
|
|
82
|
-
// decode the selector text in all browsers to:
|
|
83
|
-
// [1] = :scope, [2] = :not(:has), [3] = :has relative, [4] = :scope relative
|
|
84
|
-
const selectors = decodeURIComponent(rule.selectorText.replace(/\\(.)/g, '$1')).match(/^(.*?)\[:(not-)?has\((.+?)\)\](.*?)$/);
|
|
85
|
-
|
|
86
|
-
if (selectors) {
|
|
87
|
-
const attributeName = ':' + (selectors[2] ? 'not-' : '') + 'has(' + // encode a :has() pseudo selector as an attribute name
|
|
88
|
-
encodeURIComponent(selectors[3]).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',') + ')';
|
|
89
|
-
observedItems.push({
|
|
90
|
-
rule,
|
|
91
|
-
scopeSelector: selectors[1],
|
|
92
|
-
isNot: selectors[2],
|
|
93
|
-
relativeSelectors: selectors[3].split(/\s*,\s*/),
|
|
94
|
-
attributeName,
|
|
95
|
-
nodes: []
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
walkStyleSheet(rule);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
} catch (error) {
|
|
103
|
-
/* do nothing and continue */
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = cssHasPseudo;
|
|
109
|
-
//# sourceMappingURL=index.js.map
|
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["src/browser.js"],"sourcesContent":["export default function cssHasPseudo (document) {\n\tconst observedItems = [];\n\n\t// document.createAttribute() doesn't support `:` in the name. innerHTML does\n\tconst attributeElement = document.createElement('x');\n\n\t// walk all stylesheets to collect observed css rules\n\t[].forEach.call(document.styleSheets, walkStyleSheet);\n\ttransformObservedItems();\n\n\t// observe DOM modifications that affect selectors\n\tconst mutationObserver = new MutationObserver(mutationsList => {\n\t\tmutationsList.forEach(mutation => {\n\t\t\t[].forEach.call(mutation.addedNodes || [], node => {\n\t\t\t\t// walk stylesheets to collect observed css rules\n\t\t\t\tif (node.nodeType === 1 && node.sheet) {\n\t\t\t\t\twalkStyleSheet(node.sheet);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\t// transform observed css rules\n\t\t\tcleanupObservedCssRules();\n\t\t\ttransformObservedItems();\n\t\t});\n\t});\n\n\tmutationObserver.observe(document, { childList: true, subtree: true });\n\n\t// observe DOM events that affect pseudo-selectors\n\tdocument.addEventListener('focus', transformObservedItems, true);\n\tdocument.addEventListener('blur', transformObservedItems, true);\n\tdocument.addEventListener('input', transformObservedItems);\n\n\t// transform observed css rules\n\tfunction transformObservedItems () {\n\t\trequestAnimationFrame(() => {\n\t\t\tobservedItems.forEach(\n\t\t\t\titem => {\n\t\t\t\t\tconst nodes = [];\n\n\t\t\t\t\t[].forEach.call(\n\t\t\t\t\t\tdocument.querySelectorAll(item.scopeSelector),\n\t\t\t\t\t\telement => {\n\t\t\t\t\t\t\tconst nthChild = [].indexOf.call(element.parentNode.children, element) + 1;\n\t\t\t\t\t\t\tconst relativeSelectors = item.relativeSelectors.map(\n\t\t\t\t\t\t\t\trelativeSelector => item.scopeSelector + ':nth-child(' + nthChild + ') ' + relativeSelector\n\t\t\t\t\t\t\t).join();\n\n\t\t\t\t\t\t\t// find any relative :has element from the :scope element\n\t\t\t\t\t\t\tconst relativeElement = element.parentNode.querySelector(relativeSelectors);\n\n\t\t\t\t\t\t\tconst shouldElementMatch = item.isNot ? !relativeElement : relativeElement;\n\n\t\t\t\t\t\t\tif (shouldElementMatch) {\n\t\t\t\t\t\t\t\t// memorize the node\n\t\t\t\t\t\t\t\tnodes.push(element);\n\n\t\t\t\t\t\t\t\t// set an attribute with an irregular attribute name\n\t\t\t\t\t\t\t\t// document.createAttribute() doesn't support special characters\n\t\t\t\t\t\t\t\tattributeElement.innerHTML = '<x ' + item.attributeName + '>';\n\n\t\t\t\t\t\t\t\telement.setAttributeNode(attributeElement.children[0].attributes[0].cloneNode());\n\n\t\t\t\t\t\t\t\t// trigger a style refresh in IE and Edge\n\t\t\t\t\t\t\t\tdocument.documentElement.style.zoom = 1; document.documentElement.style.zoom = null;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\n\t\t\t\t\t// remove the encoded attribute from all nodes that no longer match them\n\t\t\t\t\titem.nodes.forEach(node => {\n\t\t\t\t\t\tif (nodes.indexOf(node) === -1) {\n\t\t\t\t\t\t\tnode.removeAttribute(item.attributeName);\n\n\t\t\t\t\t\t\t// trigger a style refresh in IE and Edge\n\t\t\t\t\t\t\tdocument.documentElement.style.zoom = 1; document.documentElement.style.zoom = null;\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\t// update the\n\t\t\t\t\titem.nodes = nodes;\n\t\t\t\t}\n\t\t\t);\n\t\t});\n\t}\n\n\t// remove any observed cssrules that no longer apply\n\tfunction cleanupObservedCssRules () {\n\t\t[].push.apply(\n\t\t\tobservedItems,\n\t\t\tobservedItems.splice(0).filter(\n\t\t\t\titem => item.rule.parentStyleSheet &&\n\t\t\t\t\titem.rule.parentStyleSheet.ownerNode &&\n\t\t\t\t\tdocument.documentElement.contains(item.rule.parentStyleSheet.ownerNode)\n\t\t\t)\n\t\t);\n\t}\n\n\t// walk a stylesheet to collect observed css rules\n\tfunction walkStyleSheet (styleSheet) {\n\t\ttry {\n\t\t\t// walk a css rule to collect observed css rules\n\t\t\t[].forEach.call(styleSheet.cssRules || [], rule => {\n\t\t\t\tif (rule.selectorText) {\n\t\t\t\t\t// decode the selector text in all browsers to:\n\t\t\t\t\t// [1] = :scope, [2] = :not(:has), [3] = :has relative, [4] = :scope relative\n\t\t\t\t\tconst selectors = decodeURIComponent(rule.selectorText.replace(/\\\\(.)/g, '$1')).match(/^(.*?)\\[:(not-)?has\\((.+?)\\)\\](.*?)$/);\n\n\t\t\t\t\tif (selectors) {\n\t\t\t\t\t\tconst attributeName = ':' + (selectors[2] ? 'not-' : '') + 'has(' +\n\t\t\t\t\t\t\t// encode a :has() pseudo selector as an attribute name\n\t\t\t\t\t\t\tencodeURIComponent(selectors[3]).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',') +\n\t\t\t\t\t\t')';\n\n\t\t\t\t\t\tobservedItems.push({\n\t\t\t\t\t\t\trule,\n\t\t\t\t\t\t\tscopeSelector: selectors[1],\n\t\t\t\t\t\t\tisNot: selectors[2],\n\t\t\t\t\t\t\trelativeSelectors: selectors[3].split(/\\s*,\\s*/),\n\t\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\t\tnodes: []\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\twalkStyleSheet(rule);\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\t/* do nothing and continue */\n\t\t}\n\t}\n}\n"],"names":["cssHasPseudo","document","observedItems","attributeElement","createElement","forEach","call","styleSheets","walkStyleSheet","transformObservedItems","mutationObserver","MutationObserver","mutationsList","mutation","addedNodes","node","nodeType","sheet","cleanupObservedCssRules","observe","childList","subtree","addEventListener","requestAnimationFrame","item","nodes","querySelectorAll","scopeSelector","element","nthChild","indexOf","parentNode","children","relativeSelectors","map","relativeSelector","join","relativeElement","querySelector","shouldElementMatch","isNot","push","innerHTML","attributeName","setAttributeNode","attributes","cloneNode","documentElement","style","zoom","removeAttribute","apply","splice","filter","rule","parentStyleSheet","ownerNode","contains","styleSheet","cssRules","selectorText","selectors","decodeURIComponent","replace","match","encodeURIComponent","split","error"],"mappings":";;AAAe,SAASA,YAAT,CAAuBC,QAAvB,EAAiC;AAC/C,QAAMC,aAAa,GA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AA,kBAAkB,CAACJ,SAAS,CAAC,CAAD,CAAV,CAAlB,CAAiCE,OAAjC,CAAyC,MAAzC,EAAiD,GAAjD,EAAsDA,OAAtD,CAA8D,MAA9D,EAAsE,GAAtE,EAA2EA,OAA3E,CAAmF,MAAnF,EAA2F,GAA3F,EAAgGA,OAAhG,CAAwG,MAAxG,EAAgH,GAAhH,CAFqB,GAGtB,GAHA;AAKA7D,YAAAA,aAAa,CAACuC,IAAd,CAAmB;AAClBa,cAAAA,IADkB;AAElB3B,cAAAA,aAAa,EAAEkC,SAAS,CAAC,CAAD,CAFN;AAGlBrB,cAAAA,KAAK,EAAEqB,SAAS,CAAC,CAAD,CAHE;AAIlB5B,cAAAA,iBAAiB,EAAE4B,SAAS,CAAC,CAAD,CAAT,CAAaK,KAAb,CAAmB,SAAnB,CAJD;AAKlBvB,cAAAA,aALkB;AAMlBlB,cAAAA,KAAK,EAAE;AANW,aAAnB;AAQA;AACD,SApBD,MAoBO;AACNjB,UAAAA,cAAc,CAAC8C,IAAD,CAAd;AACA;AACD,OAxBD;AAyBA,KA3BD,CA2BE,OAAOa,KAAP,EAAc;AACf;AACA;AACD;AACD;;;;"}
|
package/index.mjs
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
function cssHasPseudo(document) {
|
|
2
|
-
const observedItems = []; // document.createAttribute() doesn't support `:` in the name. innerHTML does
|
|
3
|
-
|
|
4
|
-
const attributeElement = document.createElement('x'); // walk all stylesheets to collect observed css rules
|
|
5
|
-
|
|
6
|
-
[].forEach.call(document.styleSheets, walkStyleSheet);
|
|
7
|
-
transformObservedItems(); // observe DOM modifications that affect selectors
|
|
8
|
-
|
|
9
|
-
const mutationObserver = new MutationObserver(mutationsList => {
|
|
10
|
-
mutationsList.forEach(mutation => {
|
|
11
|
-
[].forEach.call(mutation.addedNodes || [], node => {
|
|
12
|
-
// walk stylesheets to collect observed css rules
|
|
13
|
-
if (node.nodeType === 1 && node.sheet) {
|
|
14
|
-
walkStyleSheet(node.sheet);
|
|
15
|
-
}
|
|
16
|
-
}); // transform observed css rules
|
|
17
|
-
|
|
18
|
-
cleanupObservedCssRules();
|
|
19
|
-
transformObservedItems();
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
mutationObserver.observe(document, {
|
|
23
|
-
childList: true,
|
|
24
|
-
subtree: true
|
|
25
|
-
}); // observe DOM events that affect pseudo-selectors
|
|
26
|
-
|
|
27
|
-
document.addEventListener('focus', transformObservedItems, true);
|
|
28
|
-
document.addEventListener('blur', transformObservedItems, true);
|
|
29
|
-
document.addEventListener('input', transformObservedItems); // transform observed css rules
|
|
30
|
-
|
|
31
|
-
function transformObservedItems() {
|
|
32
|
-
requestAnimationFrame(() => {
|
|
33
|
-
observedItems.forEach(item => {
|
|
34
|
-
const nodes = [];
|
|
35
|
-
[].forEach.call(document.querySelectorAll(item.scopeSelector), element => {
|
|
36
|
-
const nthChild = [].indexOf.call(element.parentNode.children, element) + 1;
|
|
37
|
-
const relativeSelectors = item.relativeSelectors.map(relativeSelector => item.scopeSelector + ':nth-child(' + nthChild + ') ' + relativeSelector).join(); // find any relative :has element from the :scope element
|
|
38
|
-
|
|
39
|
-
const relativeElement = element.parentNode.querySelector(relativeSelectors);
|
|
40
|
-
const shouldElementMatch = item.isNot ? !relativeElement : relativeElement;
|
|
41
|
-
|
|
42
|
-
if (shouldElementMatch) {
|
|
43
|
-
// memorize the node
|
|
44
|
-
nodes.push(element); // set an attribute with an irregular attribute name
|
|
45
|
-
// document.createAttribute() doesn't support special characters
|
|
46
|
-
|
|
47
|
-
attributeElement.innerHTML = '<x ' + item.attributeName + '>';
|
|
48
|
-
element.setAttributeNode(attributeElement.children[0].attributes[0].cloneNode()); // trigger a style refresh in IE and Edge
|
|
49
|
-
|
|
50
|
-
document.documentElement.style.zoom = 1;
|
|
51
|
-
document.documentElement.style.zoom = null;
|
|
52
|
-
}
|
|
53
|
-
}); // remove the encoded attribute from all nodes that no longer match them
|
|
54
|
-
|
|
55
|
-
item.nodes.forEach(node => {
|
|
56
|
-
if (nodes.indexOf(node) === -1) {
|
|
57
|
-
node.removeAttribute(item.attributeName); // trigger a style refresh in IE and Edge
|
|
58
|
-
|
|
59
|
-
document.documentElement.style.zoom = 1;
|
|
60
|
-
document.documentElement.style.zoom = null;
|
|
61
|
-
}
|
|
62
|
-
}); // update the
|
|
63
|
-
|
|
64
|
-
item.nodes = nodes;
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
} // remove any observed cssrules that no longer apply
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
function cleanupObservedCssRules() {
|
|
71
|
-
[].push.apply(observedItems, observedItems.splice(0).filter(item => item.rule.parentStyleSheet && item.rule.parentStyleSheet.ownerNode && document.documentElement.contains(item.rule.parentStyleSheet.ownerNode)));
|
|
72
|
-
} // walk a stylesheet to collect observed css rules
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
function walkStyleSheet(styleSheet) {
|
|
76
|
-
try {
|
|
77
|
-
// walk a css rule to collect observed css rules
|
|
78
|
-
[].forEach.call(styleSheet.cssRules || [], rule => {
|
|
79
|
-
if (rule.selectorText) {
|
|
80
|
-
// decode the selector text in all browsers to:
|
|
81
|
-
// [1] = :scope, [2] = :not(:has), [3] = :has relative, [4] = :scope relative
|
|
82
|
-
const selectors = decodeURIComponent(rule.selectorText.replace(/\\(.)/g, '$1')).match(/^(.*?)\[:(not-)?has\((.+?)\)\](.*?)$/);
|
|
83
|
-
|
|
84
|
-
if (selectors) {
|
|
85
|
-
const attributeName = ':' + (selectors[2] ? 'not-' : '') + 'has(' + // encode a :has() pseudo selector as an attribute name
|
|
86
|
-
encodeURIComponent(selectors[3]).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',') + ')';
|
|
87
|
-
observedItems.push({
|
|
88
|
-
rule,
|
|
89
|
-
scopeSelector: selectors[1],
|
|
90
|
-
isNot: selectors[2],
|
|
91
|
-
relativeSelectors: selectors[3].split(/\s*,\s*/),
|
|
92
|
-
attributeName,
|
|
93
|
-
nodes: []
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
walkStyleSheet(rule);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
} catch (error) {
|
|
101
|
-
/* do nothing and continue */
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export { cssHasPseudo as default };
|
|
107
|
-
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["src/browser.js"],"sourcesContent":["export default function cssHasPseudo (document) {\n\tconst observedItems = [];\n\n\t// document.createAttribute() doesn't support `:` in the name. innerHTML does\n\tconst attributeElement = document.createElement('x');\n\n\t// walk all stylesheets to collect observed css rules\n\t[].forEach.call(document.styleSheets, walkStyleSheet);\n\ttransformObservedItems();\n\n\t// observe DOM modifications that affect selectors\n\tconst mutationObserver = new MutationObserver(mutationsList => {\n\t\tmutationsList.forEach(mutation => {\n\t\t\t[].forEach.call(mutation.addedNodes || [], node => {\n\t\t\t\t// walk stylesheets to collect observed css rules\n\t\t\t\tif (node.nodeType === 1 && node.sheet) {\n\t\t\t\t\twalkStyleSheet(node.sheet);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\t// transform observed css rules\n\t\t\tcleanupObservedCssRules();\n\t\t\ttransformObservedItems();\n\t\t});\n\t});\n\n\tmutationObserver.observe(document, { childList: true, subtree: true });\n\n\t// observe DOM events that affect pseudo-selectors\n\tdocument.addEventListener('focus', transformObservedItems, true);\n\tdocument.addEventListener('blur', transformObservedItems, true);\n\tdocument.addEventListener('input', transformObservedItems);\n\n\t// transform observed css rules\n\tfunction transformObservedItems () {\n\t\trequestAnimationFrame(() => {\n\t\t\tobservedItems.forEach(\n\t\t\t\titem => {\n\t\t\t\t\tconst nodes = [];\n\n\t\t\t\t\t[].forEach.call(\n\t\t\t\t\t\tdocument.querySelectorAll(item.scopeSelector),\n\t\t\t\t\t\telement => {\n\t\t\t\t\t\t\tconst nthChild = [].indexOf.call(element.parentNode.children, element) + 1;\n\t\t\t\t\t\t\tconst relativeSelectors = item.relativeSelectors.map(\n\t\t\t\t\t\t\t\trelativeSelector => item.scopeSelector + ':nth-child(' + nthChild + ') ' + relativeSelector\n\t\t\t\t\t\t\t).join();\n\n\t\t\t\t\t\t\t// find any relative :has element from the :scope element\n\t\t\t\t\t\t\tconst relativeElement = element.parentNode.querySelector(relativeSelectors);\n\n\t\t\t\t\t\t\tconst shouldElementMatch = item.isNot ? !relativeElement : relativeElement;\n\n\t\t\t\t\t\t\tif (shouldElementMatch) {\n\t\t\t\t\t\t\t\t// memorize the node\n\t\t\t\t\t\t\t\tnodes.push(element);\n\n\t\t\t\t\t\t\t\t// set an attribute with an irregular attribute name\n\t\t\t\t\t\t\t\t// document.createAttribute() doesn't support special characters\n\t\t\t\t\t\t\t\tattributeElement.innerHTML = '<x ' + item.attributeName + '>';\n\n\t\t\t\t\t\t\t\telement.setAttributeNode(attributeElement.children[0].attributes[0].cloneNode());\n\n\t\t\t\t\t\t\t\t// trigger a style refresh in IE and Edge\n\t\t\t\t\t\t\t\tdocument.documentElement.style.zoom = 1; document.documentElement.style.zoom = null;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\n\t\t\t\t\t// remove the encoded attribute from all nodes that no longer match them\n\t\t\t\t\titem.nodes.forEach(node => {\n\t\t\t\t\t\tif (nodes.indexOf(node) === -1) {\n\t\t\t\t\t\t\tnode.removeAttribute(item.attributeName);\n\n\t\t\t\t\t\t\t// trigger a style refresh in IE and Edge\n\t\t\t\t\t\t\tdocument.documentElement.style.zoom = 1; document.documentElement.style.zoom = null;\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\t// update the\n\t\t\t\t\titem.nodes = nodes;\n\t\t\t\t}\n\t\t\t);\n\t\t});\n\t}\n\n\t// remove any observed cssrules that no longer apply\n\tfunction cleanupObservedCssRules () {\n\t\t[].push.apply(\n\t\t\tobservedItems,\n\t\t\tobservedItems.splice(0).filter(\n\t\t\t\titem => item.rule.parentStyleSheet &&\n\t\t\t\t\titem.rule.parentStyleSheet.ownerNode &&\n\t\t\t\t\tdocument.documentElement.contains(item.rule.parentStyleSheet.ownerNode)\n\t\t\t)\n\t\t);\n\t}\n\n\t// walk a stylesheet to collect observed css rules\n\tfunction walkStyleSheet (styleSheet) {\n\t\ttry {\n\t\t\t// walk a css rule to collect observed css rules\n\t\t\t[].forEach.call(styleSheet.cssRules || [], rule => {\n\t\t\t\tif (rule.selectorText) {\n\t\t\t\t\t// decode the selector text in all browsers to:\n\t\t\t\t\t// [1] = :scope, [2] = :not(:has), [3] = :has relative, [4] = :scope relative\n\t\t\t\t\tconst selectors = decodeURIComponent(rule.selectorText.replace(/\\\\(.)/g, '$1')).match(/^(.*?)\\[:(not-)?has\\((.+?)\\)\\](.*?)$/);\n\n\t\t\t\t\tif (selectors) {\n\t\t\t\t\t\tconst attributeName = ':' + (selectors[2] ? 'not-' : '') + 'has(' +\n\t\t\t\t\t\t\t// encode a :has() pseudo selector as an attribute name\n\t\t\t\t\t\t\tencodeURIComponent(selectors[3]).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',') +\n\t\t\t\t\t\t')';\n\n\t\t\t\t\t\tobservedItems.push({\n\t\t\t\t\t\t\trule,\n\t\t\t\t\t\t\tscopeSelector: selectors[1],\n\t\t\t\t\t\t\tisNot: selectors[2],\n\t\t\t\t\t\t\trelativeSelectors: selectors[3].split(/\\s*,\\s*/),\n\t\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\t\tnodes: []\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\twalkStyleSheet(rule);\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\t/* do nothing and continue */\n\t\t}\n\t}\n}\n"],"names":["cssHasPseudo","document","observedItems","attributeElement","createElement","forEach","call","styleSheets","walkStyleSheet","transformObservedItems","mutationObserver","MutationObserver","mutationsList","mutation","addedNodes","node","nodeType","sheet","cleanupObservedCssRules","observe","childList","subtree","addEventListener","requestAnimationFrame","item","nodes","querySelectorAll","scopeSelector","element","nthChild","indexOf","parentNode","children","relativeSelectors","map","relativeSelector","join","relativeElement","querySelector","shouldElementMatch","isNot","push","innerHTML","attributeName","setAttributeNode","attributes","cloneNode","documentElement","style","zoom","removeAttribute","apply","splice","filter","rule","parentStyleSheet","ownerNode","contains","styleSheet","cssRules","selectorText","selectors","decodeURIComponent","replace","match","encodeURIComponent","split","error"],"mappings":"AAAe,SAASA,YAAT,CAAuBC,QAAvB,EAAiC;AAC/C,QAAMC,aAAa,GAAG,EAAtB,CAD+C;;AAI/C,QAAMC,gBAAgB,GAAGF,QAAQ,CAACG,aAAT,CAAuB,GAAvB,CAAzB,CAJ+C;;AAO/C,KAAGC,OAAH,CAAWC,IAAX,CAAgBL,QAAQ,CAACM,WAAzB,EAAsCC,cAAtC;AACAC,EAAAA,sBAAsB,GARyB;;AAW/C,QAAMC,gBAAgB,GAAG,IAAIC,gBAAJ,CAAqBC,aAAa,IAAI;AAC9DA,IAAAA,aAAa,CAACP,OAAd,CAAsBQ,QAAQ,IAAI;AACjC,SAAGR,OAAH,CAAWC,IAAX,CAAgBO,QAAQ,CAACC,UAAT,IAAuB,EAAvC,EAA2CC,IAAI,IAAI;AAClD;AACA,YAAIA,IAAI,CAACC,QAAL,KAAkB,CAAlB,IAAuBD,IAAI,CAACE,KAAhC,EAAuC;AACtCT,UAAAA,cAAc,CAACO,IAAI,CAACE,KAAN,CAAd;AACA;AACD,OALD,EADiC;;AASjCC,MAAAA,uBAAuB;AACvBT,MAAAA,sBAAsB;AACtB,KAXD;AAYA,GAbwB,CAAzB;AAeAC,EAAAA,gBAAgB,CAACS,OAAjB,CAAyBlB,QAAzB,EAAmC;AAAEmB,IAAAA,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js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var parser = require('postcss-selector-parser');
|
|
4
|
-
|
|
5
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
6
|
-
|
|
7
|
-
var parser__default = /*#__PURE__*/_interopDefaultLegacy(parser);
|
|
8
|
-
|
|
9
|
-
const creator = (
|
|
10
|
-
/** @type {{ preserve: true | false }} */
|
|
11
|
-
opts) => {
|
|
12
|
-
opts = typeof opts === 'object' && opts || defaultOptions;
|
|
13
|
-
/** Whether the original rule should be preserved. */
|
|
14
|
-
|
|
15
|
-
const shouldPreserve = Boolean('preserve' in opts ? opts.preserve : true);
|
|
16
|
-
return {
|
|
17
|
-
postcssPlugin: 'css-has-pseudo',
|
|
18
|
-
Rule: rule => {
|
|
19
|
-
if (rule.selector.includes(':has(')) {
|
|
20
|
-
const fallbackSelector = getFallbackSelector(rule.selector);
|
|
21
|
-
if (shouldPreserve) rule.cloneBefore({
|
|
22
|
-
selector: fallbackSelector
|
|
23
|
-
});else rule.assign({
|
|
24
|
-
selector: fallbackSelector
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
creator.postcss = true;
|
|
32
|
-
|
|
33
|
-
const getFallbackSelector = (
|
|
34
|
-
/** @type {string} */
|
|
35
|
-
selectorText) => parser__default['default'](selectors => {
|
|
36
|
-
selectors.walkPseudos(selector => {
|
|
37
|
-
if (selector.value === ':has' && selector.nodes) {
|
|
38
|
-
const isNotHas = isParentInNotPseudo(selector);
|
|
39
|
-
selector.value = isNotHas ? ':not-has' : ':has';
|
|
40
|
-
const attribute = parser__default['default'].attribute({
|
|
41
|
-
attribute: getEscapedCss(String(selector))
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
if (isNotHas) {
|
|
45
|
-
selector.parent.parent.replaceWith(attribute);
|
|
46
|
-
} else {
|
|
47
|
-
selector.replaceWith(attribute);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}).processSync(selectorText);
|
|
52
|
-
/** Default options. */
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const defaultOptions = {
|
|
56
|
-
preserve: true
|
|
57
|
-
};
|
|
58
|
-
/** Returns the string as an escaped CSS identifier. */
|
|
59
|
-
|
|
60
|
-
const getEscapedCss = (
|
|
61
|
-
/** @type {string} */
|
|
62
|
-
value) => encodeURIComponent(value).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',').replace(/[():%[\],]/g, '\\$&');
|
|
63
|
-
/** Returns whether the selector is within a `:not` pseudo-class. */
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const isParentInNotPseudo = selector => {
|
|
67
|
-
var _selector$parent, _selector$parent$pare;
|
|
68
|
-
|
|
69
|
-
return ((_selector$parent = selector.parent) === null || _selector$parent === void 0 ? void 0 : (_selector$parent$pare = _selector$parent.parent) === null || _selector$parent$pare === void 0 ? void 0 : _selector$parent$pare.type) === 'pseudo' && selector.parent.parent.value === ':not';
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
module.exports = creator;
|
|
73
|
-
//# sourceMappingURL=postcss.js.map
|