Element

Classin html-rewriter@fastly/js-compute 4.0.0since 3.35.0View source

Represents an HTML element encountered during rewriting. Passed to the callback registered via HTMLRewritingStream.onElement.

Properties

selector

stringread-onlyView source

The CSS selector that matched this element.

tag

stringread-onlyView source

The tag name of this element.

Methods

after(content: string, options?: ElementRewriterOptions): void

Inserts content after the element's closing tag.

Parameters

content: string

HTML or text to insert

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void
append(content: string, options?: ElementRewriterOptions): void

Inserts content at the end of the element's children.

Parameters

content: string

HTML or text to append

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void
before(content: string, options?: ElementRewriterOptions): void

Inserts content before the element's opening tag.

Parameters

content: string

HTML or text to insert

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void

getAttribute

View source
getAttribute(name: string): string | null

Gets the value of an attribute.

Parameters

name: string

Attribute name

Returns

string | null

The attribute value, or null if the attribute is not present.

prepend(content: string, options?: ElementRewriterOptions): void

Inserts content at the beginning of the element's children.

Parameters

content: string

HTML or text to prepend

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void

removeAttribute

View source
removeAttribute(name: string): void

Removes an attribute from the element.

Parameters

name: string

Attribute name

Returns

void

replaceChildren

View source
replaceChildren(content: string, options?: ElementRewriterOptions): void

Replaces the element's children with new content, keeping the element's tags.

Parameters

content: string

Replacement HTML or text

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void

replaceWith

View source
replaceWith(content: string, options?: ElementRewriterOptions): void

Replaces the element and its children with new content.

Parameters

content: string

Replacement HTML or text

options: ElementRewriterOptionsoptional

Optional rewriting options

Returns

void

setAttribute

View source
setAttribute(name: string, value: string): void

Sets an attribute on the element. If the attribute already exists, its value is updated.

Parameters

name: string

Attribute name

value: string

Attribute value

Returns

void