fix css specificity for child combinator (#5399)

pull/5404/head
Tan Li Hau 4 years ago committed by GitHub
parent 7900e3eafa
commit 46d423d9db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@
## Unreleased
* Fix specificity of certain styles involving a child selector ([#4795](https://github.com/sveltejs/svelte/issues/4795))
* Fix scoping of styles involving child selector and `*` ([#5370](https://github.com/sveltejs/svelte/issues/5370))
## 3.25.0

@ -65,9 +65,8 @@ export default class Selector {
transform(code: MagicString, attr: string, max_amount_class_specificity_increased: number) {
const amount_class_specificity_to_increase = max_amount_class_specificity_increased - this.blocks.filter(block => block.should_encapsulate).length;
attr = attr.repeat(amount_class_specificity_to_increase + 1);
function encapsulate_block(block: Block) {
function encapsulate_block(block: Block, attr: string) {
let i = block.selectors.length;
while (i--) {
@ -89,15 +88,14 @@ export default class Selector {
}
}
this.blocks.forEach((block) => {
this.blocks.forEach((block, index) => {
if (block.global) {
const selector = block.selectors[0];
const first = selector.children[0];
const last = selector.children[selector.children.length - 1];
code.remove(selector.start, first.start).remove(last.end, selector.end);
}
if (block.should_encapsulate) encapsulate_block(block);
if (block.should_encapsulate) encapsulate_block(block, index === this.blocks.length - 1 ? attr.repeat(amount_class_specificity_to_increase + 1) : attr);
});
}

@ -0,0 +1 @@
main.svelte-xyz button.svelte-xyz.svelte-xyz{background-color:red}main.svelte-xyz div.svelte-xyz>button.svelte-xyz{background-color:blue}

@ -0,0 +1,14 @@
<style>
main button {
background-color: red;
}
main div > button {
background-color: blue;
}
</style>
<main>
<div>
<button type="submit">Blue</button>
</div>
</main>
Loading…
Cancel
Save