Element

Element

Return a new Element Node.

Constructor

new Element(settings, …argsopt)

Source:
Examples
new Element({ name: 'p' }) // returns an element representing <p></p>

new Element({
  name: 'input',
  attrs: [{ name: 'type', value: 'search' }],
  isVoid: true
}) // returns an element representing <input type="search">
new Element('p') // returns an element representing <p></p>

new Element('p', null,
  new Element(
    'input',
    [{ name: 'type', value: 'search' }]
  )
) // returns an element representing <p><input type="search"></p>
Parameters:
Name Type Attributes Description
settings ElementSettings | string

Custom settings applied to the Element or its tag name.

args Array.<(Array|AttributeList), NodeList> <optional>
<repeatable>

Custom settings applied to the Element or its tag name.

Extends

Members

attrs :AttributeList

Source:

Attributes applied to the Element

Type:

elements

Source:
Overrides:

Return a child Element NodeList of the current Container.

Example
container.elements // returns an array of Elements

first

Source:
Overrides:

Return the first child Node of the current Container, or null if there is none.

Example
container.first // returns a Node or null

firstElement

Source:
Overrides:

Return the first child Element of the current Container, or null if there is none.

Example
container.firstElement // returns an Element or null

index

Source:
Overrides:

Position of the current Node from its parent.

Example
node.index // returns the index of the node or -1

innerHTML

Source:
Overrides:

Return the innerHTML of the current Container as a String.

Example
container.innerHTML // returns a string of innerHTML

isSelfClosing :boolean

Source:

Whether the Element is self-closing

Type:
  • boolean

isVoid :boolean

Source:

Whether the Element is self-closing

Type:
  • boolean

isWithoutEndTag :boolean

Source:

Whether the Element includes a closing tag

Type:
  • boolean

last

Source:
Overrides:

Return the last child Node of the current Container, or null if there is none.

Example
container.last // returns a Node or null

lastElement

Source:
Overrides:

Return the last child Element of the current Container, or null if there is none.

Example
container.lastElement // returns an Element or null

name :string

Source:

Tag name of the Element

Type:
  • string

next

Source:
Overrides:

Next Node after the current Node, or null if there is none.

Example
node.next // returns null

nextElement

Source:
Overrides:

Next Element after the current Node, or null if there is none.

Example
node.nextElement // returns an element or null

nodes :NodeList

Source:

Nodes appended to the Element

Type:

outerHTML

Source:
Overrides:

Return the outerHTML of the current Element as a String.

Example
element.outerHTML // returns a string of outerHTML

outerHTML

Source:
Overrides:

Replace the current Element from a String.

Example
element.outerHTML = 'Hello <strong>world</strong>';

previous

Source:
Overrides:

Previous Node before the current Node, or null if there is none.

Example
node.previous // returns a node or null

previousElement

Source:
Overrides:

Previous Element before the current Node, or null if there is none.

Example
node.previousElement // returns an element or null

result :Result

Source:

Current result applied to the Element

Type:

root

Source:
Overrides:

Top-most ancestor from the current Node.

Example
node.root // returns the top-most node or the current node itself

source :ElementSource

Source:

Source mapping of the Element

Type:
  • ElementSource

sourceInnerHTML

Source:
Overrides:

Return the stringified innerHTML from the source input.

sourceOuterHTML

Source:
Overrides:

Return the stringified outerHTML from the source input.

textContent

Source:
Overrides:

Return the text content of the current Container as a String.

type :'element'

Source:

Type identifier of the Element

Type:
  • 'element'

Methods

after(…nodes)

Source:
Overrides:

Insert one or more Nodes after the current Node, returning the current Node.

Example
node.after(new Text({ data: 'Hello World' }))
Parameters:
Name Type Attributes Description
nodes Node | string <repeatable>

Any nodes to be inserted after the current Node.

append(…nodes)

Source:
Overrides:

Append Nodes or new Text Nodes to the current Node, returning the current Node.

Example
node.append(someOtherNode)
Parameters:
Name Type Attributes Description
nodes Node | string <repeatable>

Any nodes to be inserted after the last child of the current Node.

appendTo(parent)

Source:
Overrides:

Append the current Node to another Node, returning the current Node.

Parameters:
Name Type Description
parent Container

Container for the current Node.

before(…nodes)

Source:
Overrides:

Insert Nodes or new Text Nodes before the Node if it has a parent, returning the current Node.

Example
node.before(new Text({ data: 'Hello World' })) // returns the current node
Parameters:
Name Type Attributes Description
nodes Node | string <repeatable>

Any nodes to be inserted before the current Node.

clone(settings, isDeep)

Source:

Return a clone of the current Element.

Parameters:
Name Type Description
settings ElementSettings

Custom settings applied to the cloned Element.

isDeep boolean

Whether the descendants of the current Element should also be cloned.

lastNth() → {Node|null}

Source:
Overrides:

Return a child Node of the current Container by last index, or null if there is none.

Example
container.lastNth(0) // returns a Node or null
Returns:
Type
Node | null

lastNthElement() → {Element|null}

Source:
Overrides:

Return a child Element of the current Container by last index, or null if there is none.

Example
container.lastNthElement(0) // returns an Element or null
Returns:
Type
Element | null

nth() → {Node|null}

Source:
Overrides:

Return a child Node of the current Container by index, or null if there is none.

Example
container.nth(0) // returns a Node or null
Returns:
Type
Node | null

nthElement() → {Element|null}

Source:
Overrides:

Return an Element child of the current Container by index, or null if there is none.

Example
container.nthElement(0) // returns an Element or null
Returns:
Type
Element | null

prepend(…nodes)

Source:
Overrides:

Prepend Nodes or new Text Nodes to the current Node, returning the current Node.

Example
node.prepend(someOtherNode)
Parameters:
Name Type Attributes Description
nodes Node | string <repeatable>

Any nodes inserted before the first child of the current Node.

remove()

Source:
Overrides:

Remove the current Node from its parent, returning the current Node.

Example
node.remove() // returns the current node

replaceAll(…nodes)

Source:
Overrides:

Replace all of the children of the current Container, returning the current Container.

Example
container.replaceAll(new Text({ data: 'Hello World' }))
Parameters:
Name Type Attributes Description
nodes Array.<Node> <repeatable>

Any nodes replacing the current children of the Container.

replaceWith(…nodes)

Source:
Overrides:

Replace the current Node with another Node or Nodes, returning the current Node.

Example
node.replaceWith(someOtherNode) // returns the current node
Parameters:
Name Type Attributes Description
nodes Node <repeatable>

Any nodes replacing the current Node.

toJSON() → {ElementJSON}

Source:

Return the Element as a unique Object.

Returns:
Type
ElementJSON

toString()

Source:

Return the stringified Element.

visit(result, overrideVisitorsopt) → {ResultPromise}

Source:
Overrides:

Transform the current Node and any descendants using visitors.

Examples
await node.visit(result)
await node.visit() // visit using the result of the current node
await node.visit(result, {
  Element () {
    // do something to an element
  }
})
Parameters:
Name Type Attributes Description
result Result

Result to be used by visitors.

overrideVisitors Object <optional>

Alternative visitors to be used in place of Result visitors.

Returns:
Type
ResultPromise

walk(callback_or_filter, callback)

Source:
Overrides:

Traverse the descendant Nodes of the current Container with a callback function, returning the current Container.

Examples
container.walk(node => {
  console.log(node);
})
container.walk('*', node => {
  console.log(node);
})

Walk only "section" Elements.

container.walk('section', node => {
  console.log(node); // logs only Section Elements
})
container.walk(/^section$/, node => {
  console.log(node); // logs only Section Elements
})
container.walk(
  node => node.name.toLowerCase() === 'section',
  node => {
  console.log(node); // logs only Section Elements
})

Walk only Text.

container.walk('#text', node => {
  console.log(node); // logs only Text Nodes
})
Parameters:
Name Type Description
callback_or_filter function | string | RegExp

Callback function, or a filter to reduce Nodes the callback is applied to.

callback function | string | RegExp

Callback function when a filter is also specified.

warn(result, text, optsopt)

Source:
Overrides:

Add a warning from the current Node.

Examples
node.warn(result, 'Something went wrong')
node.warn(result, 'Something went wrong', {
  node: someOtherNode,
  plugin: someOtherPlugin
})
Parameters:
Name Type Attributes Description
result Result

Result the warning is being added to.

text string

Message being sent as the warning.

opts Object <optional>

Additional information about the warning.