Skip to content

Commit 65aae85

Browse files
authored
Merge pull request #1631 from sveltejs/gh-1618
encapsulate local styles inside global ones
2 parents 7c51094 + 7eaf5dc commit 65aae85

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/css/Selector.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class Selector {
3030

3131
apply(node: Node, stack: Node[]) {
3232
const toEncapsulate: Node[] = [];
33+
3334
applySelector(this.stylesheet, this.localBlocks.slice(), node, stack.slice(), toEncapsulate);
3435

3536
if (toEncapsulate.length > 0) {
@@ -132,10 +133,6 @@ export default class Selector {
132133
}
133134
}
134135

135-
function isDescendantSelector(selector: Node) {
136-
return selector.type === 'WhiteSpace' || selector.type === 'Combinator';
137-
}
138-
139136
function applySelector(stylesheet: Stylesheet, blocks: Block[], node: Node, stack: Node[], toEncapsulate: any[]): boolean {
140137
const block = blocks.pop();
141138
if (!block) return false;
@@ -145,7 +142,6 @@ function applySelector(stylesheet: Stylesheet, blocks: Block[], node: Node, stac
145142
}
146143

147144
let i = block.selectors.length;
148-
let j = stack.length;
149145

150146
while (i--) {
151147
const selector = block.selectors[i];
@@ -202,12 +198,18 @@ function applySelector(stylesheet: Stylesheet, blocks: Block[], node: Node, stac
202198
}
203199
}
204200

201+
if (blocks.every(block => block.global)) {
202+
toEncapsulate.push({ node, block });
203+
return true;
204+
}
205+
205206
return false;
206207
} else if (block.combinator.name === '>') {
207208
if (applySelector(stylesheet, blocks, stack.pop(), stack, toEncapsulate)) {
208209
toEncapsulate.push({ node, block });
209210
return true;
210211
}
212+
211213
return false;
212214
}
213215

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
div .foo.svelte-xyz{color:red}div>.foo.svelte-xyz{font-weight:bold}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p class='foo'>red/bold</p>
2+
3+
<style>
4+
:global(div) .foo {
5+
color: red;
6+
}
7+
8+
:global(div) > .foo {
9+
font-weight: bold;
10+
}
11+
</style>

0 commit comments

Comments
 (0)