emel
em(met) el(ement)
Create DOM elements with Emmet
Installation
npm install emel
Usage
You can use any emmet abbreviation.
Options:
import emel from "emel";
const emmetString = "table>thead>tr>th{col1}+th{col2}^^tbody>(tr>td[colspan=2]{2 col width})+tr>td.col${1 col width}*2";
const options = {};
const element = emel(emmetString, options);
Setting default options
You can set the default options by creating a new instance of emel and pass the default options.
import Emel from "emel";
const { emel } = new Emel({placeholders: [1, 2]});
const element = emel("div{?}+div{?}");
Doc
Default to browser document
.
For Angular SSR, use injector.get(DOCUMENT)
import { DOCUMENT } from '@angular/common';
emel(`div>p*3`, {
doc: injector.get(DOCUMENT)
});
ReturnSingleChild
By default emel will always return the elements as children of a document fragment.
If you set the returnSingleChild
option to true
it will only wrap the elements in a document fragment if there are multiple top level elements.
Multiline
By default space characters are not allowed outside of text nodes.
If you want to write readable strings with multiple lines you can use the multiline
option to remove newline characters and surrounding spaces.
emel(`
table>
thead>
tr>
th{col1}+
th{col2}^^
tbody>
tr>
td[colspan=2]{2 col width}^
tr>
td.col\${1 col width}*2
`, {
multiline: true
});
Note: The multiline
option will remove newline characters and surrounding spaces from text nodes as well (which might not be expected).
emel(`
div{
line 1
line 2
}
`, {
multiline: true
})
Placeholders
You can use placeholders to insert dynamic content into any string value.
Replacements can be a string to replace all placeholders with the same value:
emel("?#?.?[?=?]{?}", {
placeholders: "test"
});
Or an array to replace each placeholder with different value:
emel("?#?.?[?=?]{?}", {
placeholders: ["tag", "id", "class", "attrName", "attrValue", "text"]
});
Note: Placeholders are replaced after parsing so values do not need to be escaped. This comes with a few unintuitive cases.
- Text placeholders (
{?}
) are replaced after attributes:
emel("?{?}#?", {
placeholders: ["tag", "text", "id"]
});
emel("?{?}#?", {
placeholders: ["tag", "id", "text"]
});
- Placeholders on multiplied elements are not copied to every element:
emel("div{?}*2+span{?}", {
placeholders: ["div", "span"]
});
emel("div{?}*2+span{?}", {
placeholders: ["div", "div", "span"]
});
Placeholders can also be an object with keys that refer to any value:
emel("this#is.replaced[with=placeholders]{placeholders}", {
placeholders: {
this: "tag",
is: "id",
replaced: "class",
with: "attr",
placeholders: ["value", "text"],
}
});
Placeholder keys must match the entire value to be replaced:
emel("#this.is[not='replaced with placholders']", {
placeholders: {
this: "id",
is: "class",
replaced: ["attr"],
with: ["value"],
placeholders: "text",
}
});
Text placeholders may also be replaced by a DOM Node:
const span = emel("span{text}")
emel("div{?}", {
placeholders: span
});
Attribute name placeholders that equal false
, null
, or undefined
will not be set.
emel("div[attr=val]", {
placeholders: {
attr: undefined
}
});
emel("div[attr=val]", {
placeholders: {
attr: null
}
});
emel("div[attr.]", {
placeholders: {
attr: false
}
});
Attribute name placeholders that equal true
will not be changed.
emel("input[type='checkbox' checked.]", {
placeholders: {
checked: false
}
});
emel("input[type='checkbox' checked.]", {
placeholders: {
checked: true
}
});