Fragment

Fragment

Return a new Fragment Node.

Constructor

new Fragment(settings)

Source:
Example
new Fragment() // returns an empty fragment

new Fragment({ nodes: [ new Element('span') ] }) // returns a fragment with a <span>
Parameters:
Name Type Description
settings FragmentSettings

Custom settings applied to the Fragment.

Extends

Members

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

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 :'#document-fragment'

Source:

Node name of the Fragment

Type:
  • '#document-fragment'

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 :Array.<Node>

Source:

Nodes appended to the Fragment

Type:

outerHTML

Source:
Overrides:

Return the outerHTML of the current Container as a String.

Example
container.outerHTML // returns a string of outerHTML

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 Fragment

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 :FragmentSource

Source:

Source mapping of the Fragment

Type:

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 :'fragment'

Source:

Type identifer of the Fragment

Type:
  • 'fragment'

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(isDeep)

Source:

Return a clone of the current Fragment.

Parameters:
Name Type Description
isDeep boolean

Whether the descendants of the current Fragment 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() → {Array.<(Node|string)>}

Source:

Return the current Fragment as an Array.

Example
fragment.toJSON() // returns []
Returns:
Type
Array.<(Node|string)>

toString()

Source:

Return the current Fragment as a String.

Example
fragment.toJSON() // returns ''

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.