All files util.js

100% Statements 43/43
83.33% Branches 10/12
100% Functions 3/3
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 442x 2x 2x 2x 2x 2x 9x 9x 9x 1x 1x 9x 2x 2x 2x 2x 2x 2x 10x 11x 19x 19x 3x 2x 3x 1x 1x 3x 11x 9x 2x 2x 2x 2x 2x 2x 17x 17x 17x 17x 17x 17x 17x  
/**
 * Calls customElements.define without breaking flow if an error happens
 * @param {string} name - Name for the new custom element. Must be a valid custom element name.
 * @param {CustomElementConstructor} constructor - Name for the new custom element. Must be a valid custom element name.
 */
export function registerElement (name, constructor) {
  try {
    customElements.define(name, constructor)
  } catch (error) {
    console.error(error)
  }
}
 
/**
 * Registers CSS properties to fix Chrome "allow-discrete" transition on Chrome
 * @see https://issues.chromium.org/issues/360159391
 */
export function registerCSSProperties () {
  for (const [name, inherits] of /** @type {const} */([['--default-ui-mode', false], ['--ui-mode', true]])) {
    try {
      CSS.registerProperty({ name, inherits })
    } catch (e) {
      if (e instanceof DOMException) {
        // property registered, ignore it
      } else {
        throw e // re-throw the error
      }
    }
  }
}
 
/**
 * Gets host element of node inside shadow dom; returns undefined otherwise
 * @param {EventTarget | null} [node] - property transition event
 */
export function getHostElement (node) {
  if (!(node instanceof Node)) { return undefined }
  const rootNode = node.getRootNode()
  if (!(rootNode instanceof ShadowRoot)) { return undefined }
  const { host } = rootNode
  if (!(host instanceof Element)) { return undefined }
  return host
}