Skip to content

k.utils.dom ​

HTML parsing and image extraction

Execution ContextPrerequisitesTypical Use
KScript API, Page / View server scriptsCall 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 ​

js
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 / PropertyDescriptionReturns
parse(html)Parse an HTML string and return a populated DocumentObjectModelDocumentObjectModel
getText(html)Extract plain text from an HTML stringstring
hasImageWhether parsed HTML has an <img> with a non-empty src attribute (returns false before parse())boolean
imagesExtracted { src, alt }[] list for <img> tags with non-whitespace src (throws before parse())DomImage[]
documentParsed 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.

ParameterTypeRequiredDescription
htmlstringYesHTML string to parse

Returns: DocumentObjectModel; read document, hasImage, or images from the returned instance.

js
const dom = k.utils.dom.parse("<html><body><img src=\"/a.png\" alt=\"a\" /></body></html>")

getText() ​

Extract plain text from an HTML string.

ParameterTypeRequiredDescription
htmlstringYesHTML string to extract text from

Returns: string.

js
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.

js
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 }[]).

js
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.

js
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 : null

TypeScript Definitions ​

ts
interface DocumentObjectModel {
  document: Kooboo.Dom.Document;
  hasImage: boolean;
  images: DomImage[];
  parse(html: string): DocumentObjectModel;
  getText(html: string): string;
}

interface DomImage {
  src: string;
  alt: string;
}