\Tale\Jade\ParserNode

Represents a node in the AST the parser generates.

A node has children and always tries to reference its parents

It also has some utility methods to work with those nodes

Summary

Methods
Properties
Constants
__construct()
indexOf()
prev()
next()
append()
prepend()
remove()
insertAfter()
insertBefore()
find()
findArray()
text()
dump()
__isset()
__unset()
__get()
__set()
__toString()
$type
$line
$offset
$parent
$children
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$_data
N/A

Properties

$type

$type : string

The type of the node, can be a string of any kind.

Type

string

$line

$line : integer|null

The line this node was created on.

Type

integer|null

$offset

$offset : integer|null

The offset in a line this node was created on.

Type

integer|null

$parent

$parent : \Tale\Jade\Parser\Node|null

The parent-node of this node.

Type

\Tale\Jade\Parser\Node|null

$children

$children : array<mixed,\Tale\Jade\Parser\Node>

The children of this node.

Type

array<mixed,\Tale\Jade\Parser\Node>

$_data

$_data : array

The data associated with this node.

These get set via get and set

Type

array

Methods

__construct()

__construct(string  $type, integer|null  $line = null, integer|null  $offset = null) 

Creates a new, detached node without children or a parent.

It can be appended to any node after that

The type can be any kind of string

Parameters

string $type

the type of this node

integer|null $line

the line at which we found this node

integer|null $offset

the offset in a line we found this node at

indexOf()

indexOf(\Tale\Jade\Parser\Node  $node) : integer|false

Returns the position of the given node inside this node.

[element:a] (0)[element:b] (1)[element:c] (2)[element:d]

[element:a]->indexOf([element:d]) === 2

Parameters

\Tale\Jade\Parser\Node $node

the child-node to get the index of

Returns

integer|false

prev()

prev() : \Tale\Jade\Parser\Node|null

Returns the previous sibling of this element or null, if if there isn't any.

[element:a] (0)[element:b] (1)[element:c]

[element:c]->prev() === [element:b]

Returns

\Tale\Jade\Parser\Node|null

next()

next() : \Tale\Jade\Parser\Node|null

Returns the next sibling of this element or null, if if there isn't any.

[element:a] (0)[element:b] (1)[element:c]

[element:b]->next() === [element:c]

Returns

\Tale\Jade\Parser\Node|null

append()

append(\Tale\Jade\Parser\Node  $node) : $this

Appends the given node to this node's children.

This also sets the parent of the given child to this node

[element:a] (0)[element:b] (1)[element:c]

[element:a]->append([element:d])

[element:a] (0)[element:b] (1)[element:c] (2)[element:d]

Parameters

\Tale\Jade\Parser\Node $node

the new child node to be appended

Returns

$this

prepend()

prepend(\Tale\Jade\Parser\Node  $node) : $this

Prepends the given node to this node's children.

This also sets the parent of the given child to this node

[element:a] (0)[element:b] (1)[element:c]

[element:a]->prepend([element:d])

[element:a] (0)[element:d] (1)[element:b] (2)[element:c]

Parameters

\Tale\Jade\Parser\Node $node

the new child node to be prepended

Returns

$this

remove()

remove(\Tale\Jade\Parser\Node  $node) : $this

Removes the given child node from this node's children.

The parent of the given child node will be set to null

[element:a] (0)[element:b] (1)[element:c] (2)[element:d]

[element:a]->remove([element:c])

[element:a] (0)[element:b] (1)[element:d]

Parameters

\Tale\Jade\Parser\Node $node

the node to remove from this node's children

Returns

$this

insertAfter()

insertAfter(\Tale\Jade\Parser\Node  $node, \Tale\Jade\Parser\Node  $newNode) : $this

Inserts the second given node after the first given node inside this node's children.

This allows fine control over the node's children

The new nodes parent will be set to this node

[element:a] (0)[element:b] (1)[element:c]

[element:a]->insertAfter([element:b], [element:d])

[element:a] (0)[element:b] (1)[element:d] (2)[element:c]

Parameters

\Tale\Jade\Parser\Node $node

the child node of this node's children the new node will be inserted after

\Tale\Jade\Parser\Node $newNode

the new node that will be inserted after the first node

Returns

$this

insertBefore()

insertBefore(\Tale\Jade\Parser\Node  $node, \Tale\Jade\Parser\Node  $newNode) : $this

Inserts the second given node before the first given node inside this node's children.

This allows fine control over the node's children

The new nodes parent will be set to this node

[element:a] (0)[element:b] (1)[element:c]

[element:a]->insertBefore([element:c], [element:d])

[element:a] (0)[element:b] (1)[element:d] (2)[element:c]

Parameters

\Tale\Jade\Parser\Node $node

the child node of this node's children the new node will be inserted before

\Tale\Jade\Parser\Node $newNode

the new node that will be inserted before the first node

Returns

$this

find()

find(string  $type) : \Generator

Finds nodes with the given type inside this nodes children.

Plus all it's children-children recursively and returns a generator providing them

This is used to collect all blocks, imports and mixins and handle them in a special way

If you need a normal array, use ->findArray() instead instead

Parameters

string $type

the node type to search for

Returns

\Generator —

a generator of the found children

findArray()

findArray(string  $type) : array

Finds nodes with the given type inside this nodes children.

Plus all it's children-children recursively and returns an array with all of them

I you want to do further searching on it, you should rather use the Generator-version ->find() to improve memory-usage

Parameters

string $type

the node type to search for

Returns

array —

an array containing all found children

text()

text(string  $indentStyle = '  ', string  $newLine = "\n", integer  $level) : string

Returns all text and child-texts in a single text.

You can control the text-style with the arguments

Parameters

string $indentStyle

the indentation to use (multiplies with level)

string $newLine

the new-line style to use

integer $level

the initial indentation level

Returns

string —

the compiled text-block

dump()

dump(integer  $level) : string

Dumps the node as a string to ease up debugging.

This is also the default-action for __toString on every node

The result will look like this:

[element tag=a expands=[element tag=b]] [element tag=c attributes={[attribute name=d name=e]}]

Parameters

integer $level

the initial indentation level

Returns

string —

the string to debug the node-tree

__isset()

__isset(string  $key) : boolean

Gets called when isset() is called on this node's properties.

Redirects the call to the $_data storage

Parameters

string $key

the name of the property to check

Returns

boolean

__unset()

__unset(string  $key) 

Gets called when unset() is called on this node's properties.

Redirects the call to $_data storage

Parameters

string $key

the name of the property to unset

__get()

__get(string  $key) : mixed

Gets called when a property is read from this node.

Redirects to the $_data storage and returns a reference to that value

Parameters

string $key

the name of the property that is read

Returns

mixed —

a reference to the value of the property

__set()

__set(string  $key, mixed  $value) 

Gets called when a property is written on this node instance.

Redirects to the $_data storage and sets a key with that value there

Parameters

string $key

the name of the property to be written

mixed $value

the value of that property

__toString()

__toString() : string

Gets called when this node instance is casted to a string in any way

(echo, (string), strval, string operations, ., etc.)

Calls ->dump() and dumps a debuggable text-representation of this node and all of its child-nodes

Returns

string —

a debuggable text-representation of this node tree