From 91ec3fcb7b2216d338a19b2b1cbe77718e14778a Mon Sep 17 00:00:00 2001 From: James Bradbury Date: Thu, 30 Dec 2021 12:31:06 +0000 Subject: [PATCH] abstract parent element check into function --- src/compiler/compile/nodes/Element.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index 7e488682d4..0d5dbe7c3e 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -98,6 +98,23 @@ const react_attributes = new Map([ const attributes_to_compact_whitespace = ['class', 'style']; +function is_parent(parent: INode, elements: Array) { + let check = false; + + while (parent) { + const parent_name = (parent as Element).name; + if (elements.includes(parent_name)) { + check = true; + break; + } + if (parent.type === 'Element') { + break; + } + parent = parent.parent; + } + return check; +} + function get_namespace(parent: Element, element: Element, explicit_namespace: string) { const parent_element = parent.find_nearest(/^Element/); @@ -376,14 +393,7 @@ export default class Element extends Node { component.warn(attribute, compiler_warnings.a11y_no_redundant_roles(value)); } - let { parent } = this; - let is_parent_section_or_article = false; - - while (parent) { - const parent_name = (parent as Element).name; - if ( parent_name === 'section' || parent_name === 'article') { - is_parent_section_or_article = true; - break; + const is_parent_section_or_article = is_parent(this.parent, ['section', 'article']) } if (parent.type === 'Element') { break;