html-spec-tags
Version:
All HTML tags supported by the current spec. With types!
79 lines (78 loc) • 2.46 kB
JavaScript
import { AssertionError, assert, check } from '@augment-vir/assert';
import { wrapInTry } from '@augment-vir/common';
import { allHtmlSpecTagNames, htmlSpecConstructorsByTagName } from './html.js';
import { allMathmlSpecTagNames, mathmlSpecConstructorsByTagName, } from './mathml.js';
import { allSvgSpecTagNames, svgSpecConstructorsByTagName } from './svg.js';
/**
* All possible spec tag names in a single array.
*
* @category Tag
*/
export const allSpecTagNames = Array.from(new Set([
...allHtmlSpecTagNames,
...allSvgSpecTagNames,
...allMathmlSpecTagNames,
].sort()));
/**
* Get the constructor for the given tag name. Since there are some duplicate tag names, the
* priority is:
*
* 1. HTML tags
* 2. SVG tags
* 3. MathML tags
*
* Meaning, if a tag name is duplicated between HTML and SVG tags, the HTML constructor will be
* returned. If the lower priority tag constructor is desired these types of situations, use its
* constructor list directly. For example,use `svgSpecConstructorsByTagName` directly.
*
* @category Tag
*/
export function getSpecTagNameConstructor(tagName) {
const constructor = htmlSpecConstructorsByTagName[tagName] ||
svgSpecConstructorsByTagName[tagName] ||
mathmlSpecConstructorsByTagName[tagName];
if (!constructor) {
throw new TypeError(`Found no constructor for tag name '${tagName}'`);
}
return constructor;
}
/**
* Type guards the input as a valid spec tag name.
*
* @category Assertion
*/
export function isSpecTagName(input) {
return wrapInTry(() => {
assertSpecTagName(input);
return true;
}, {
fallbackValue: false,
});
}
/**
* Asserts that the input as a valid spec tag name.
*
* @category Assertion
*/
export function assertSpecTagName(input, failureMessage) {
assert.isString(input, failureMessage);
if (!check.hasValue(allSpecTagNames, input)) {
throw new AssertionError(`'${input}' is not tag name`, failureMessage);
}
}
/**
* Passes the input through if it's a valid spec tag name, throws an error if not.
*
* @category Assertion
*/
export function ensureSpecTagName(input) {
if (isSpecTagName(input)) {
return input;
}
else if (check.isString(input)) {
throw new TypeError(`'${input}' is not a valid tag name.`);
}
else {
throw new TypeError(`'${JSON.stringify(input)}' is not a string, it cannot be a valid tag name.`);
}
}