Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit a76e3f8

Browse files
committed
Update Plugin: Organize and document source
1 parent ff2c97e commit a76e3f8

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
// tooling
2-
const postcss = require('postcss');
2+
const postcss = require('postcss');
33
const selectorParser = require('postcss-selector-parser');
44

55
// plugin
6-
module.exports = postcss.plugin('postcss-dir-pseudo-class', () => (root) => {
6+
module.exports = postcss.plugin('postcss-dir-pseudo-class', (opts) => (root) => {
7+
// walk rules using the :dir pseudo-class
78
root.walkRules(/:dir\([^\)]*\)/, (rule) => {
9+
// update the rule selector
810
rule.selector = selectorParser((selectors) => {
11+
// for each (comma separated) selector
912
selectors.nodes.forEach(
1013
(selector) => {
14+
// walk all selector nodes that are :dir pseudo-classes
1115
selector.walk((node) => {
12-
// ...
1316
if ('pseudo' === node.type && ':dir' === node.value) {
17+
// previous and next selector nodes
1418
const prev = node.prev();
15-
const prevIsSpaceCombinator = prev && prev.type && 'combinator' === prev.type && ' ' === prev.value;
16-
1719
const next = node.next();
20+
21+
const prevIsSpaceCombinator = prev && prev.type && 'combinator' === prev.type && ' ' === prev.value;
1822
const nextIsSpaceCombinator = next && next.type && 'combinator' === next.type && ' ' === next.value;
1923

24+
// preserve the selector tree
2025
if (prevIsSpaceCombinator && (nextIsSpaceCombinator || !next)) {
2126
node.replaceWith(
2227
selectorParser.universal()
@@ -25,6 +30,7 @@ module.exports = postcss.plugin('postcss-dir-pseudo-class', () => (root) => {
2530
node.remove();
2631
}
2732

33+
// conditionally prepend a combinator before inserting the [dir] attribute
2834
const first = selector.nodes[0];
2935
const firstIsSpaceCombinator = first && 'combinator' === first.type && ' ' === first.value;
3036

@@ -36,11 +42,15 @@ module.exports = postcss.plugin('postcss-dir-pseudo-class', () => (root) => {
3642
);
3743
}
3844

45+
// value of the :dir pseudo-class
46+
const value = node.nodes.toString();
47+
48+
// prepend the dir attribute
3949
selector.prepend(
4050
selectorParser.attribute({
4151
attribute: 'dir',
4252
operator: '=',
43-
value: `"${ node.nodes.toString() }"`
53+
value: `"${ value }"`
4454
})
4555
);
4656
}

0 commit comments

Comments
 (0)