Skip to content

Commit cfc83a2

Browse files
committed
and another, albeit by replacing some bewildering code with some more bewildering code
1 parent 610c891 commit cfc83a2

File tree

1 file changed

+31
-13
lines changed
  • packages/svelte/src/compiler/phases/2-analyze/css

1 file changed

+31
-13
lines changed

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -680,21 +680,27 @@ function relative_selector_might_apply_to_node(relative_selector, rule, element,
680680
* @param {boolean} include_self
681681
*/
682682
function get_following_sibling_elements(element, include_self) {
683-
/** @type {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.Root | null} */
684-
let parent = get_element_parent(element);
683+
const path = element.metadata.path;
684+
let i = path.length;
685+
686+
/** @type {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | null} */
687+
let parent = null;
688+
let nodes = /** @type {Compiler.AST.Fragment} */ (path[0]).nodes;
685689

686-
if (!parent) {
687-
parent = element;
688-
while (parent?.type !== 'Root') {
689-
parent = /** @type {any} */ (parent).parent;
690+
while (i--) {
691+
const node = path[i];
692+
if (node.type === 'RegularElement' || node.type === 'SvelteElement') {
693+
parent = node;
694+
nodes = node.fragment.nodes;
695+
break;
690696
}
691697
}
692698

693699
/** @type {Array<Compiler.AST.RegularElement | Compiler.AST.SvelteElement>} */
694700
const sibling_elements = [];
695701
let found_parent = false;
696702

697-
for (const el of parent.fragment.nodes) {
703+
for (const el of nodes) {
698704
if (found_parent) {
699705
walk(
700706
el,
@@ -709,13 +715,25 @@ function get_following_sibling_elements(element, include_self) {
709715
}
710716
);
711717
} else {
712-
/** @type {any} */
713-
let child = element;
714-
while (child !== el && child !== parent) {
715-
child = child.parent;
716-
}
717-
if (child === el) {
718+
// TODO this code is highly confusing
719+
if (el === element) {
718720
found_parent = true;
721+
} else {
722+
let path = element.metadata.path;
723+
let i = path.length;
724+
725+
while (i--) {
726+
let node = path[i];
727+
728+
if (node === parent) {
729+
break;
730+
}
731+
732+
if (node === el) {
733+
found_parent = true;
734+
break;
735+
}
736+
}
719737
}
720738
}
721739
}

0 commit comments

Comments
 (0)