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

Fixed css selectors specificity #3475

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 11 additions & 3 deletions src/compiler/compile/css/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,29 @@ export default class Selector {
}

transform(code: MagicString, attr: string) {
const should_double_up_attr = this.blocks.filter(block => block.should_encapsulate).length === 1;

function encapsulate_block(block: Block) {
let i = block.selectors.length;
let encapsulationAttr = attr;

if (should_double_up_attr) {
encapsulationAttr = attr + attr;
}

while (i--) {
const selector = block.selectors[i];
if (selector.type === 'PseudoElementSelector' || selector.type === 'PseudoClassSelector') {
if (selector.name !== 'root') {
if (i === 0) code.prependRight(selector.start, attr);
if (i === 0) code.prependRight(selector.start, encapsulationAttr);
}
continue;
}

if (selector.type === 'TypeSelector' && selector.name === '*') {
code.overwrite(selector.start, selector.end, attr);
code.overwrite(selector.start, selector.end, encapsulationAttr);
} else {
code.appendLeft(selector.end, attr);
code.appendLeft(selector.end, encapsulationAttr);
}

break;
Expand Down
2 changes: 1 addition & 1 deletion test/css/samples/attribute-selector-bind/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
details[open].svelte-xyz{color:red}
details[open].svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/attribute-selector-only-name/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[foo].svelte-xyz{color:red}[baz].svelte-xyz{color:blue}
[foo].svelte-xyz.svelte-xyz{color:red}[baz].svelte-xyz.svelte-xyz{color:blue}
2 changes: 1 addition & 1 deletion test/css/samples/attribute-selector-unquoted/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[foo=bar].svelte-xyz{color:red}
[foo=bar].svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/basic/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz{color:red}
div.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/css-vars/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
:root{--root-test:20}div.svelte-xyz{--test:10}
:root{--root-test:20}div.svelte-xyz.svelte-xyz{--test:10}
2 changes: 1 addition & 1 deletion test/css/samples/directive-special-character/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo\:bar.svelte-xyz{color:red}
.foo\:bar.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/empty-rule-dev/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{}
.foo.svelte-xyz.svelte-xyz{}
2 changes: 1 addition & 1 deletion test/css/samples/global-keyframes/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/keyframes-autoprefixed/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/keyframes-from-to/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/keyframes/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/local-inside-global/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div .foo.svelte-xyz{color:red}div>.foo.svelte-xyz{font-weight:bold}
div .foo.svelte-xyz.svelte-xyz{color:red}div>.foo.svelte-xyz.svelte-xyz{font-weight:bold}
2 changes: 1 addition & 1 deletion test/css/samples/media-query-word/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media only screen and (min-width: 400px){div.svelte-xyz{color:red}}
@media only screen and (min-width: 400px){div.svelte-xyz.svelte-xyz{color:red}}
2 changes: 1 addition & 1 deletion test/css/samples/media-query/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media(min-width: 400px){.large-screen.svelte-xyz{display:block}}
@media(min-width: 400px){.large-screen.svelte-xyz.svelte-xyz{display:block}}
2 changes: 1 addition & 1 deletion test/css/samples/nested/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}.bar.svelte-xyz{font-style:italic}
.foo.svelte-xyz.svelte-xyz{color:red}.bar.svelte-xyz.svelte-xyz{font-style:italic}
2 changes: 1 addition & 1 deletion test/css/samples/not-selector/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.svelte-xyz:not(.foo){color:red}
.svelte-xyz.svelte-xyz:not(.foo){color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo*='bar'].svelte-xyz{color:red}
[data-foo*='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar' i].svelte-xyz{color:red}
[data-foo='bar' i].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar'].svelte-xyz{color:red}
[data-foo='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar'].svelte-xyz{color:red}
[data-foo='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo|='bar'].svelte-xyz{color:red}
[data-foo|='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo^='bar'].svelte-xyz{color:red}
[data-foo^='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo$='bar'].svelte-xyz{color:red}
[data-foo$='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo~='bar'].svelte-xyz{color:red}
[data-foo~='bar'].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[autoplay].svelte-xyz{color:red}
[autoplay].svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}
.foo.svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}
.foo.svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz .bar{color:red}
.foo.svelte-xyz.svelte-xyz .bar{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz>p>em{color:red}
div.svelte-xyz.svelte-xyz>p>em{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz>p{color:red}
div.svelte-xyz.svelte-xyz>p{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div>section>p.svelte-xyz{color:red}
div>section>p.svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div>p.svelte-xyz{color:red}
div>p.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute-id/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#foo.svelte-xyz{color:red}
#foo.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
p.svelte-xyz{color:red}
p.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/pseudo-element/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/spread/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red;font-size:2em;font-family:'Comic Sans MS'}
.foo.svelte-xyz.svelte-xyz{color:red;font-size:2em;font-family:'Comic Sans MS'}
2 changes: 1 addition & 1 deletion test/css/samples/supports-query/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@supports (display: grid){.maybe-grid.svelte-xyz{display:grid}}
@supports (display: grid){.maybe-grid.svelte-xyz.svelte-xyz{display:grid}}
2 changes: 1 addition & 1 deletion test/css/samples/universal-selector/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.svelte-xyz{color:red}
.svelte-xyz.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz{@apply --funky-div;}
div.svelte-xyz.svelte-xyz{@apply --funky-div;}
2 changes: 1 addition & 1 deletion test/css/samples/unknown-at-rule/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz{@apply --funky-div;}
div.svelte-xyz.svelte-xyz{@apply --funky-div;}
2 changes: 1 addition & 1 deletion test/css/samples/unused-selector-leading/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.bar.svelte-xyz{color:red}
.bar.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/unused-selector-ternary/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.active.svelte-xyz{color:red}.inactive.svelte-xyz{color:blue}
.active.svelte-xyz.svelte-xyz{color:red}.inactive.svelte-xyz.svelte-xyz{color:blue}
2 changes: 1 addition & 1 deletion test/css/samples/unused-selector/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}
.foo.svelte-xyz.svelte-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/weird-selectors/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.-foo.svelte-xyz{color:red}[title='['].svelte-xyz{color:blue}
.-foo.svelte-xyz.svelte-xyz{color:red}[title='['].svelte-xyz.svelte-xyz{color:blue}
4 changes: 2 additions & 2 deletions test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
function add_css() {
var style = element("style");
style.id = "svelte-1a7i8ec-style";
style.textContent = "p.svelte-1a7i8ec{color:red}";
style.textContent = "p.svelte-1a7i8ec.svelte-1a7i8ec{color:red}";
append(document.head, style);
}

Expand Down Expand Up @@ -62,4 +62,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
4 changes: 2 additions & 2 deletions test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
function add_css() {
var style = element("style");
style.id = "svelte-1slhpfn-style";
style.textContent = "@media(min-width: 1px){div.svelte-1slhpfn{color:red}}";
style.textContent = "@media(min-width: 1px){div.svelte-1slhpfn.svelte-1slhpfn{color:red}}";
append(document.head, style);
}

Expand Down Expand Up @@ -45,4 +45,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
div.svelte-bzh57p{color:red}
div.svelte-4yw8vx{color:green}
div.svelte-bzh57p.svelte-bzh57p{color:red}
div.svelte-4yw8vx.svelte-4yw8vx{color:green}
2 changes: 1 addition & 1 deletion test/server-side-rendering/samples/styles/_expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-bzh57p{color:red}
div.svelte-bzh57p.svelte-bzh57p{color:red}