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

Commit 465a0ad

Browse files
marchboxZacky Majonathantneal
authored
Add support for Shadow DOM CSS (#5)
* Add support for shadow DOM CSS * Change spaces to tabs for consistency * Fix comment language * Make sure this won't do anything to selectors with leading :root Co-authored-by: Zacky Ma <zacky@nianticlabs.com> Co-authored-by: Jonathan Neal <jonathantneal@hotmail.com>
1 parent 06f4e56 commit 465a0ad

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

.tape.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@ module.exports = {
1515
options: {
1616
preserve: true,
1717
},
18-
},
18+
},
19+
"basic:shadow": {
20+
message: "support { shadow: true } usage",
21+
source: "basic.css",
22+
options: {
23+
shadow: true
24+
},
25+
},
1926
};

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const dirRegex = /:dir\([^\)]*\)/;
55
module.exports = function creator(opts) {
66
const dir = Object(opts).dir;
77
const preserve = Boolean(Object(opts).preserve);
8+
const shadow = Boolean(Object(opts).shadow);
89

910
return {
1011
postcssPlugin: 'postcss-dir-pseudo-class',
@@ -73,6 +74,12 @@ module.exports = function creator(opts) {
7374
value: `"${ value }"`
7475
});
7576

77+
// :host-context([dir]) for Shadow DOM CSS
78+
const hostContextPseudo = selectorParser.pseudo({
79+
value: ':host-context'
80+
});
81+
hostContextPseudo.append(dirAttr);
82+
7683
// not[dir] attribute
7784
const notDirAttr = selectorParser.pseudo({
7885
value: `${firstIsHtml || firstIsRoot ? '' : 'html'}:not`
@@ -97,8 +104,11 @@ module.exports = function creator(opts) {
97104
selector.prepend(notDirAttr);
98105
}
99106
} else if (firstIsHtml) {
100-
// otherwise, insert dir attribute after html tag
107+
// insert dir attribute after html tag
101108
selector.insertAfter(first, dirAttr);
109+
} else if (shadow && !firstIsRoot) {
110+
// prepend :host-context([dir])
111+
selector.prepend(hostContextPseudo);
102112
} else {
103113
// otherwise, prepend the dir attribute
104114
selector.prepend(dirAttr);

test/basic.shadow.expect.css

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
:host-context([dir="ltr"]) {
2+
order: 0;
3+
}
4+
5+
:host-context([dir="ltr"]) test {
6+
order: 1;
7+
}
8+
9+
:host-context([dir="ltr"]) test * {
10+
order: 2;
11+
}
12+
13+
:host-context([dir="ltr"]) test * test {
14+
order: 3;
15+
}
16+
17+
:host-context([dir="ltr"]) test {
18+
order: 4;
19+
}
20+
21+
:host-context([dir="ltr"]) test test {
22+
order: 5;
23+
}
24+
25+
:host-context([dir="ltr"]) test test {
26+
order: 6;
27+
}
28+
29+
[dir="ltr"]:root * {
30+
order: 7;
31+
}
32+
33+
html[dir="ltr"] * {
34+
order: 8;
35+
}
36+
37+
:host-context([dir="rtl"]) {
38+
order: 9;
39+
}
40+
41+
:host-context([dir="rtl"]) test {
42+
order: 10;
43+
}
44+
45+
:host-context([dir="rtl"]) test * {
46+
order: 11;
47+
}
48+
49+
:host-context([dir="rtl"]) test * test {
50+
order: 12;
51+
}
52+
53+
:host-context([dir="rtl"]) test {
54+
order: 13;
55+
}
56+
57+
:host-context([dir="rtl"]) test test {
58+
order: 14;
59+
}
60+
61+
:host-context([dir="rtl"]) test test {
62+
order: 15;
63+
}
64+
65+
[dir="rtl"]:root * {
66+
order: 16;
67+
}
68+
69+
html[dir="rtl"] * {
70+
order: 17;
71+
}

0 commit comments

Comments
 (0)