1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16 17// Check whether a DOM element contains another, or whether they're the same 18export function isOrContains(container: Element, target: Element): boolean { 19 return container === target || container.contains(target); 20} 21 22// Find a DOM element with a given "ref" attribute 23export function findRef(root: Element, ref: string): Element|null { 24 const query = `[ref=${ref}]`; 25 if (root.matches(query)) { 26 return root; 27 } else { 28 return root.querySelector(query); 29 } 30} 31 32// Safely case an Element to an HTMLElement. 33// Throws if the element is not an HTMLElement. 34export function toHTMLElement(el: Element): HTMLElement { 35 if (!(el instanceof HTMLElement)) { 36 throw new Error('Element is not an HTLMElement'); 37 } 38 return el as HTMLElement; 39} 40 41// Check if a mithril component vnode has children 42export function hasChildren({children}: m.Vnode<any>): boolean { 43 return Array.isArray(children) && children.length > 0; 44} 45