From ed85e0142b5d5665e7f9a6a8b242741b1adb21dc Mon Sep 17 00:00:00 2001 From: shinyaigeek Date: Mon, 30 Aug 2021 20:43:01 +0900 Subject: [PATCH] [feature] add util module to check element is interactive element --- .../compile/utils/is_interactive_element.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/compiler/compile/utils/is_interactive_element.ts diff --git a/src/compiler/compile/utils/is_interactive_element.ts b/src/compiler/compile/utils/is_interactive_element.ts new file mode 100644 index 0000000000..acbc04108a --- /dev/null +++ b/src/compiler/compile/utils/is_interactive_element.ts @@ -0,0 +1,48 @@ +import Element from '../nodes/Element'; + +const interactiveInputTypes = new Set([ + 'submit', + 'reset', + 'image', + 'button', + 'radio', + 'checkbox' +]); + +export const is_interactive_element: (element: Element) => boolean = function ( + element +) { + if (element.name === 'details') { + return true; + } + + if (element.name === 'summary') { + return true; + } + + if (element.name === 'a') { + return element.attributes.some((attr) => attr.name === 'href'); + } + + if (element.name === 'button') { + return true; + } + + if (element.name === 'input') { + const typeValue = element.attributes.find((attr) => attr.name === 'type'); + if (typeValue) { + return interactiveInputTypes.has((typeValue.get_static_value() || '').toString()); + } + return false; + } + + if (element.name === 'option') { + return element.attributes.some((attr) => attr.name === 'value'); + } + + if (element.name === 'dialog') { + return true; + } + + return false; +};