Element

Element

Create a new Element Node.

Constructor

new Element(settings) → {Element}

Source:
Example
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">
Parameters:
Name Type Description
settings Object | String

Custom settings applied to the Element, or the tag name of the Text.

Properties
Name Type Description
name String

Tag name of the Element.

isSelfClosing Boolean

Whether the Element is self-closing.

isVoid Boolean

Whether the Element is void.

isisWithoutEndTagVoid Boolean

Whether the Element uses a closing tag.

attrs Array | AttributeList | Object

Attributes applied to the Element.

nodes Array | NodeList

Nodes appended to the Element.

source Object

Source mapping of the Element.

Returns:

A new Element Node

Type
Element

Extends

Members

elements

Source:
Inherited From:

Return a child Element NodeList of the current Container.

Example
container.elements // returns an array of Elements

first

Source:
Inherited From:

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:
Inherited From:

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:
Inherited From:

The 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

last

Source:
Inherited From:

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:
Inherited From:

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

Example
container.lastElement // returns an Element or null

next

Source:
Inherited From:

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

Example
node.next // returns null

nextElement

Source:
Inherited From:

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

Example
node.nextElement // returns an element or null

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:
Inherited From:

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

Example
node.previous // returns a node or null

previousElement

Source:
Inherited From:

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

Example
node.previousElement // returns an element or null

root

Source:
Inherited From:

The top-most ancestor from the current Node.

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

sourceInnerHTML

Source:
Inherited From:

Return the stringified innerHTML from the source input.

sourceOuterHTML

Source:
Inherited From:

Return the stringified outerHTML from the source input.

textContent

Source:
Overrides:

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

Methods

after(…nodes) → {Node}

Source:
Inherited From:

Insert one ore more Nodes after 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.

Returns:
Type
Node

append(…nodes) → {Node}

Source:
Inherited From:

Append Nodes or new Text Nodes to 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.

Returns:
Type
Node

appendTo(parent) → {Node}

Source:
Inherited From:

Append the current Node to another Node.

Parameters:
Name Type Description
parent Container

The Container for the current Node.

Returns:
Type
Node

before(…nodes) → {Node}

Source:
Inherited From:

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

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.

Returns:
Type
Node

clone(settings, isDeep) → {Element}

Source:

Return a clone of the current Element.

Parameters:
Name Type Description
settings Object

Custom settings applied to the cloned Element.

isDeep Boolean

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

Returns:
  • The cloned Element
Type
Element

lastNth() → {Node|Null}

Source:
Inherited From:

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:
Inherited From:

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:
Inherited From:

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:
Inherited From:

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) → {Node}

Source:
Inherited From:

Prepend Nodes or new Text Nodes to 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.

Returns:
Type
Node

remove() → {Node}

Source:
Inherited From:

Remove the current Node from its parent.

Example
node.remove() // returns the current node
Returns:
Type
Node

replaceAll(…nodes) → {Container}

Source:
Inherited From:

Replace all of the children of the current Container.

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

Any nodes replacing the current children of the Container.

Returns:
Type
Container

replaceWith(…nodes) → {Node}

Source:
Inherited From:

Replace the current Node with another Node or Nodes.

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

Any nodes replacing the current Node.

Returns:
Type
Node

toJSON() → {Object}

Source:

Return the Element as a unique Object.

Returns:
Type
Object

toString() → {String}

Source:

Return the stringified Element.

Returns:
Type
String

visit(result, overrideVisitorsopt) → {ResultPromise}

Source:
Inherited From:

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

The 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) → {Container}

Source:
Inherited From:

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

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

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

callback function | String | RegExp

A callback function when a filter is also specified.

Returns:
Type
Container

warn(result, text, optsopt)

Source:
Inherited From:

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

The Result the warning is being added to.

text String

The message being sent as the warning.

opts Object <optional>

Additional information about the warning.