domtagger

The hyperHTML's template literal parser, used to handle all repeated updates per each attribute or node.
- CDN as global utility, via https://unpkg.com/domtagger
- ESM via
import domtagger from 'domtagger'
- CJS via
const domtagger = require('domtagger')
Live test
Example
The tagger accepts a configuration object with mandatory methods that should return a function to invoke per each update.
Optionally, the object could have a type
property, as either html
or svg
string, and a transform
method that must return some string as content, after receiving the markup that is going to be used.
var html = domtagger({
type: 'html',
attribute: function (node, name, attribute) {
return function (value) {
var type = typeof value;
if (type === 'boolean' || type === 'function')
node[name] = value;
else if (value == null)
node.removeAttribute(name);
else
node.setAttribute(name, value);
}
},
any: function (comment, childNodes) {
var parentNode = comment.parentNode;
return function (html) {
parentNode.innerHTML = html;
};
},
text: function (node) {
return function (textContent) {
node.textContent = textContent;
};
},
transform: function (html) {
return html;
},
convert: function (template) {
return template.join(domconstants.UIDC).replace(sani, tize);
}
});
document.body.appendChild(
render({
onclick: function (e) {
alert(e.currentTarget.outerHTML);
},
html: 'Hello <strong>domtagger</strong>!',
text: "isn't this cool?"
})
);
function render(model) {
return html`
<div onclick=${model.onclick}>
<div>${model.html}</div>
<textarea>${model.text}</textarea>
</div>
`;
}
If you'd like to create a dev only comment that will be removed at runtime once parsed, you can either start the comment with a ghost emoji 👻 or use /*
and */
right at the boundaries of the comment.