パッケージの詳細

custom-element-name

fullwebdev14MIT0.1.1

Check if a given string can REALLY be used as a custom element name

custom-element, custom, elements, custom element

readme

custom-element-name

Check if a given string can really be used as a custom element name.

import {
  validateAndCreateElement,
  InvalidElementNameError,
  NotAPotentialCustomElementNameError,
  AlreadyUsedElementNameError,
} from "custom-element-name";

class EmotionElement extends HTMLElement {
  //...
}

/** @type {EmotionElement} */
let el;
try {
  el = validateAndCreateElement("emotion-😍", EmotionElement);
} catch (e) {
  if (e instanceof InvalidElementNameError) {
    // not a valid element name`;
  } else if (e instanceof NotAPotentialCustomElementNameError) {
    // not a valid custom element name`;
  } else if (e instanceof AlreadyUsedElementNameError) {
    // already used name
  } else if (e instanceof DOMException && e.name === "InvalidCharacterError") {
    // HERE: 'emotion-😍' is not a valid name
    // browsers doesn't really like emojis & stuff 🤷‍
  }
}

Functions

isValidElementName(name: string)

Check if a string is a valid HTML Element name according to the standard.

See:

matchPotentialCustomElementName(name: string)

Check if a string is a valid custom element name according to the standard.

See:

validateAndCreateElement(name, class)

Define and create a custom element only if the given name is valid according to the standard.

Params:

  • name: string
  • class: CustomElementConstructor

Returns: HTMLElement | undefined

Throws:

  • InvalidElementNameError if the given name isn't a valid HTML Element name
  • NotAPotentialCustomElementNameError if the given name isn't a valid Custom Element name
  • AlreadyUsedElementNameError if a custom element was already defined for the given name
  • DOMException (name : InvalidCharacterError) if, well, the browser doesn't agree with some character you used anyway...

See :

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.1.1 (2021-03-17)

Bug Fixes

  • custom-element-name: bad regexs (bf4beb3)

0.1.0 (2021-02-27)

Features

  • custom-element-name: initial commit (0dcd670)