A repository of HTML-specific client-side JavaScript functions.
A majority of the usage information for logic in this repository is documented in the JSDocs of the methods in HTML.js. Please see that documentation for details.
/**
* @param {Object.<string, any>} source
*/
static Compose(source)Compose a DOM element from a source object or tag string.
Parameter:
- Name: source
- Data Type: A JavaScript object with a single key and whose value is an object or array. The key is interpreted as the tag name, and value is interpreted as specified below.
You can define:
- Element tags (with optional namespace)
- Attributes and properties
- Event listeners
- Children (single or multiple)
- Inline modifier functions
source ::= elementObject
elementObject ::= { tagName: config }
tagName ::= any valid HTML/SVG/MathML tag name
optionally prefixed with a namespace: "prefix:tag"
config ::= object
| array // array shorthand - children
object ::= { key: value, ... }
style - applied via HTML.SetStyle(element, value)
class - space-separated string, added to element.classList
$children - object, Element, Text, or array representing child elements
$inlineModifier - function(element) called after children, attributes, events, style/class applied
any other key:
- value is function - attach as event listener (key = event name)
- value is array - treated as children
- otherwise - apply as property or attribute
Visualizing nested structure:
source
├── Element
├── Text
└── Object
└── tagName
└── config
├── Array
│ └── children[]
│ ├── Element
│ ├── Text
│ └── Object → recursive elementObject
│
└── Object
├── style → style application
├── class → classList.add(...)
├── $children
│ ├── single child
│ └── array of children
├── $inlineModifier → function(element)
├── eventName → function → addEventListener
├── otherKey → attribute/property
└── array value → children shorthand
This shows how Compose() processes the object:
- Creates the element
- Apply attributes and properties
- Attach event listeners
- Append children (from
$childrenor array shorthand) - Call
$inlineModifierfunction (if present) - Return element
HTML.Compose({ div: {} });HTML.Compose({
div: {
id: "root",
click: () => alert("clicked")
}
});HTML.Compose({
div: [
"div",
{ div: { click: () => alert("child") } }
]
});HTML.Compose({
div: {
click: () => console.log("root clicked"),
$children: { div: { click: () => console.log("single child clicked") } }
}
});HTML.Compose({
div: {
id: "root",
$inlineModifier: el => { el.style.border = "2px solid red"; }
}
});HTML.Compose({
div: {
id: "app",
$children: [
{
header: {
$children: {
h1: { innerText: "My App" }
}
}
},
{
main: {
$children: [
{ p: { innerText: "Welcome!" } },
{ button: { click: () => alert("Clicked!") } }
]
}
}
]
}
});const element = HTML.Compose({
div: {
id: "container",
class: "card",
click: () => console.log("container clicked"),
$children: [
{ h2: { innerText: "Title" } },
{ p: { innerText: "This is a paragraph." } },
{
button: {
innerText: "Click Me",
click: () => alert("Button clicked!")
}
}
],
$inlineModifier: el => el.classList.add("initialized")
}
});
document.body.appendChild(element);- Arrays as config - children shorthand
$children- explicit single or multiple children$inlineModifier- called after all children, attributes, and events- Supports HTML, SVG, and MathML elements with namespace detection
- Events are detected automatically when a property value is a function