Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encapsulate local styles inside global ones #1631

Merged
merged 1 commit into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/css/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class Selector {

apply(node: Node, stack: Node[]) {
const toEncapsulate: Node[] = [];

applySelector(this.stylesheet, this.localBlocks.slice(), node, stack.slice(), toEncapsulate);

if (toEncapsulate.length > 0) {
Expand Down Expand Up @@ -132,10 +133,6 @@ export default class Selector {
}
}

function isDescendantSelector(selector: Node) {
return selector.type === 'WhiteSpace' || selector.type === 'Combinator';
}

function applySelector(stylesheet: Stylesheet, blocks: Block[], node: Node, stack: Node[], toEncapsulate: any[]): boolean {
const block = blocks.pop();
if (!block) return false;
Expand All @@ -145,7 +142,6 @@ function applySelector(stylesheet: Stylesheet, blocks: Block[], node: Node, stac
}

let i = block.selectors.length;
let j = stack.length;

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

if (blocks.every(block => block.global)) {
toEncapsulate.push({ node, block });
return true;
}

return false;
} else if (block.combinator.name === '>') {
if (applySelector(stylesheet, blocks, stack.pop(), stack, toEncapsulate)) {
toEncapsulate.push({ node, block });
return true;
}

return false;
}

Expand Down
1 change: 1 addition & 0 deletions test/css/samples/local-inside-global/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div .foo.svelte-xyz{color:red}div>.foo.svelte-xyz{font-weight:bold}
11 changes: 11 additions & 0 deletions test/css/samples/local-inside-global/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p class='foo'>red/bold</p>

<style>
:global(div) .foo {
color: red;
}

:global(div) > .foo {
font-weight: bold;
}
</style>