k.utils.dom
HTML parsing and image extraction
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, Page / View server scripts | Call parse(html) first before reading hasImage, images, or document on the returned instance (before parse(), hasImage returns false, document is null, and images throws) | Stripping HTML tags, extracting image metadata, and server-side DOM queries |
k.utils.dom (DocumentObjectModel) parses HTML and extracts plain text, image lists, or the underlying DOM document.
Quick Start
k.api.post("extractDom", () => {
const html = "<article><p>Hello Kooboo</p><img src=\"/logo.png\" alt=\"Logo\"></article>"
const dom = k.utils.dom.parse(html)
return {
text: k.utils.dom.getText(html),
hasImage: dom.hasImage,
images: dom.images
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
parse(html) | Parse an HTML string and return a populated DocumentObjectModel | DocumentObjectModel |
getText(html) | Extract plain text from an HTML string | string |
hasImage | Whether parsed HTML has an <img> with a non-empty src attribute (returns false before parse()) | boolean |
images | Extracted { src, alt }[] list for <img> tags with non-whitespace src (throws before parse()) | DomImage[] |
document | Parsed DOM document object (null before parse()) | Kooboo.Dom.Document |
parse()
Parse HTML into an internal Document attached to the returned instance. Accessing k.utils.dom.images directly without calling parse() first throws an exception.
| Parameter | Type | Required | Description |
|---|---|---|---|
html | string | Yes | HTML string to parse |
Returns: DocumentObjectModel; read document, hasImage, or images from the returned instance.
const dom = k.utils.dom.parse("<html><body><img src=\"/a.png\" alt=\"a\" /></body></html>")getText()
Extract plain text from an HTML string.
| Parameter | Type | Required | Description |
|---|---|---|---|
html | string | Yes | HTML string to extract text from |
Returns: string.
const text = k.utils.dom.getText("<p>Hello</p>")hasImage
Whether the parsed HTML contains at least one <img> element with a non-empty src attribute (<img> tags without src or with src="" return false). Read from the instance returned by parse(); reading before parse() returns false without throwing.
Parameters: None.
Returns: boolean.
const dom = k.utils.dom.parse("<p><img src=\"/logo.png\" alt=\"Logo\"></p>")
return { hasImage: dom.hasImage }images
The parsed image list for <img> elements with a non-whitespace src attribute, each item containing src and alt (read from the instance returned by parse(); reading before parse() throws an exception).
Parameters: None.
Returns: DomImage[] ({ src: string, alt: string }[]).
const dom = k.utils.dom.parse("<img src=\"/logo.png\" alt=\"Logo\">")
return dom.images.map((img) => ({
src: img.src,
alt: img.alt
}))document
The parsed DOM document object (read from the instance returned by parse()). Common members include head, body, images, links, getElementById(id), and getElementsByTagName(tagName). Kooboo.Dom.Document has no title property; to read <title>, use dom.document.getElementsByTagName("title").item[0]?.textContent.
Parameters: None.
Returns: Kooboo.Dom.Document.
const dom = k.utils.dom.parse("<html><body><h1 id=\"t\">Hello</h1></body></html>")
const el = dom.document.getElementById("t")
const heading = el ? el.innerHtml : nullTypeScript Definitions
interface DocumentObjectModel {
document: Kooboo.Dom.Document;
hasImage: boolean;
images: DomImage[];
parse(html: string): DocumentObjectModel;
getText(html: string): string;
}
interface DomImage {
src: string;
alt: string;
}