From da6e265735d708ebbd75c0a7454337f048f887db Mon Sep 17 00:00:00 2001 From: Nguyen Tran Date: Thu, 20 Apr 2023 01:40:47 -0400 Subject: [PATCH] Add documentation for find_previous_sibling --- src/compiler/compile/css/Selector.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/compiler/compile/css/Selector.ts b/src/compiler/compile/css/Selector.ts index 4d33d09082..705e341601 100644 --- a/src/compiler/compile/css/Selector.ts +++ b/src/compiler/compile/css/Selector.ts @@ -470,29 +470,43 @@ function get_element_parent(node: Element): Element | null { return parent as Element | null; } -function find_previous_sibling_considering_slots(node: INode): INode { +/** + * Finds the given node's previous sibling in the DOM + * + * Unless the component is a custom element (web component), which in this + * case, the element is actually real, the Svelte is just a + * placeholder and is not actually real. Any children nodes in + * are 'flattened' and considered as the same level as the 's siblings + * + * e.g. + *

Heading 1

+ * + *

Heading 2

+ *
+ * + * is considered to look like: + *

Heading 1

+ *

Heading 2

+ */ +function find_previous_sibling(node: INode): INode { if (node.component.compile_options.customElement) { return node.prev; } let current_node: INode = node; do { - // @ts-ignore if (current_node.type === 'Slot') { - const slot_children = (current_node as Element).children; + const slot_children = current_node.children; if (slot_children.length > 0) { current_node = slot_children.slice(-1)[0]; // go to its last child first continue; } } - // @ts-ignore while (!current_node.prev && current_node.parent && current_node.parent.type === 'Slot') { current_node = current_node.parent; } current_node = current_node.prev; - - // @ts-ignore } while (current_node && current_node.type === 'Slot'); return current_node; @@ -501,7 +515,7 @@ function find_previous_sibling_considering_slots(node: INode): INode { function get_possible_element_siblings(node: INode, adjacent_only: boolean): Map { const result: Map = new Map(); let prev: INode = node; - while (prev = find_previous_sibling_considering_slots(prev)) { + while (prev = find_previous_sibling(prev)) { if (prev.type === 'Element') { if (!prev.attributes.find(attr => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot')) { result.set(prev, NodeExist.Definitely);