A basic utility to retrieve DOM nodes names from their constructors and vice-versa.
Right now this module has two methods: htmlClass.get(...)
and htmlClass.set(...)
.
The purpose of this API is to maintain in a single module all relations between HTML constructors and relative nodes. This module is environment agnostic and it does not require any DOM understand from the engine.
The .get(...)
method has 3 overloads. Following their description.
Given a generic DOM node name, it returns its constructor name.
const htmlClass = require('html-class');
// for known elements
// logs: 'HTMLAnchorElement'
htmlClass.get('a');
// unknown elements, empty string
// logs: ''
htmlClass.get('not-registered');
Given a generic DOM Class name, it returns a collection of elements that inherits form that prototype.
const htmlClass = require('html-class');
// for known constructors
// logs: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
htmlClass.get('HTMLHeadingElement');
// logs: ['button']
htmlClass.get('HTMLButtonElement');
// unknown constructors, empty Array
// logs: []
htmlClass.get('NotRegistered');
Given a generic regular expression, it returns a collection of elements or constructors that matched such test.
// returns ['HTMLButtonElement']
htmlClass.get(/^HTMLButton/);
// returns all known/registered custom elements
// ['my-el', 'x-form', 'some-data']
htmlClass.get(/^[a-z]+-/);
The .set(...)
method has 2 overloads. Following their description.
If not previously registered, adds nodeName
to the list and associate it with the Class
.
// register a generic custom element
htmlClass.set('my-custom-element', 'MyCustomElement');
// logs: 'MyCustomElement'
htmlClass.get('my-custom-element');
// logs: ['my-custom-element']
htmlClass.get('MyCustomElement');
// if repeated, nothing happens
htmlClass.set('my-custom-element', 'MyOtherElement');
// if another component wasn't registered though
// it will be added to the generic constructor list
htmlClass.set('my-other-element', 'MyCustomElement');
// logs: ['my-custom-element', 'my-other-element']
htmlClass.get('MyCustomElement');
Does exactly the same thing htmlClass.set(nodeName, Class)
does, assuming by specs constructors are PascalCase and node names are either fully lower or upper case.
The code is compatible with every JavaScript engine, old or new, either via browser or server.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。