Skip to content

Commit 353d697

Browse files
committed
simplify
1 parent 6e149f2 commit 353d697

File tree

1 file changed

+14
-23
lines changed
  • packages/svelte/src/compiler/phases/2-analyze/css

1 file changed

+14
-23
lines changed

packages/svelte/src/compiler/phases/2-analyze/css/Selector.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,15 @@ export default class Selector {
5959
this.selector_list = group_selectors(node, parent_selector_list);
6060

6161
this.local_selector_list = this.selector_list.map((complex_selector) => {
62-
const i = complex_selector.findLastIndex((block) => !block.global);
62+
const i = complex_selector.findLastIndex((block) => !block.can_ignore());
6363
return complex_selector.slice(0, i + 1);
6464
});
6565

66-
// Determine `used` based on the processed local_selector_list
67-
let host_only = false;
68-
let root_only = false;
69-
70-
// Check if there's exactly one group and one block within that group, and if it's host or root
71-
if (this.local_selector_list.length === 1 && this.local_selector_list[0].length === 1) {
72-
const single_block = this.local_selector_list[0][0];
73-
host_only = single_block.compound.host;
74-
root_only = single_block.compound.root;
75-
}
76-
77-
// Check if there are no local blocks across all groups, or if there's a host_only or root_only situation
78-
const no_local_blocks = this.local_selector_list.every((group) => group.length === 0);
79-
this.used = no_local_blocks || host_only || root_only;
66+
// if we have a `:root {...}` or `:global(...) {...}` selector, we need to mark
67+
// this selector as `used` even if the component doesn't contain any nodes
68+
this.used = this.local_selector_list.some((blocks) => blocks.length === 0);
8069
}
70+
8171
/**
8272
* Determines whether the given selector is used within the component's nodes
8373
* and marks the corresponding blocks for encapsulation if so.
@@ -108,7 +98,9 @@ export default class Selector {
10898
*/
10999
apply(node) {
110100
for (const complex_selector of this.local_selector_list) {
111-
if (apply_selector(complex_selector.slice(), node, this.stylesheet)) {
101+
if (complex_selector.length === 0) {
102+
this.used = true;
103+
} else if (apply_selector(complex_selector.slice(), node, this.stylesheet)) {
112104
this.used = true;
113105
}
114106
}
@@ -890,22 +882,21 @@ class RelativeSelector {
890882
this.compound.add(selector);
891883
}
892884

885+
can_ignore() {
886+
return this.compound.global || this.compound.host || this.compound.root;
887+
}
888+
893889
get global() {
894890
return this.compound.global;
895891
}
892+
896893
get host() {
897894
return this.compound.host;
898895
}
896+
899897
get root() {
900898
return this.compound.root;
901899
}
902-
get end() {
903-
return this.compound.end;
904-
}
905-
get start() {
906-
if (this.combinator) return this.combinator.start;
907-
return this.compound.start;
908-
}
909900

910901
get contains_invisible_selectors() {
911902
return this.compound.selectors.some((selector) => !selector.visible);

0 commit comments

Comments
 (0)