- Twig
- Twig
- NotImplementedYet
Generic error for non implemented feature
- UnsupportedParser
Error for unsupported data types
- UnsupportedType
Generic error for unsupported data types
- UnsupportedCondition
Generic error for unsupported data types
- createParser(handler, [options]) ⇒
Parser
Create a new Twig parser
- onStart(binds, node, attrs)
Common Event hanlder for starting tag
- onClose(handler, options, name)
Common Event hanlder for closing tag
- ParserOptions
Optional settings for the Twig parser
- TwigHandler
Reference to handler functions for Twig objects.
Element can be specified as string, Regular Expression, custom function,Twig.Root
orTwig.Any
You can specify afunction
or aevent
name- HandlerCondition :
string
|Array.<string>
|RegExp
|HandlerConditionFilter
|Root
|Any
Condition to specify when handler shall be called
- If
string
then the element name must be equal to the string - If
string[]
then the element name must be included in string array - If
RegExp
then the element name must match the Regular Expression - If HandlerConditionFilter then function must return
true
- Use
Twig.Root
to call the handler on root element, i.e. when the end of document is reached - Use
Twig.Any
to call the handler on every element
- If
- HandlerFunction :
function
Handler function for Twig objects, i.e. the way you like to use the XML element.
- HandlerConditionFilter ⇒
boolean
Custom filter function to specify when handler shall be called
- ElementCondition :
string
|RegExp
|ElementConditionFilter
|Twig
|undefined
Optional condition to get elements
- If
undefined
, then all elements are returned.
- If
string
then the element name must be equal to the string - If
RegExp
then the element name must match the Regular Expression - If ElementConditionFilter then function must return
true
- Use Twig object to find a specific element
- If
- ElementConditionFilter ⇒
boolean
Custom filter function to select desired elements
- Parser ⇒
sax
|node-expat
|saxophone
- AttributeCondition :
string
|RegExp
|AttributeConditionFilter
Optional condition to get attributes
- If
undefined
, then all attributes are returned.
- If
string
then the attribute name must be equal to the string - If
RegExp
then the attribute name must match the Regular Expression - If AttributeConditionFilter then the attribute must filter function
- If
- AttributeConditionFilter :
function
Custom filter function to get desired attributes
- #PI :
object
XML Processing Instruction object, exist only on root
- #declaration :
object
XML Declaration object, exist only on root
- #namespace :
object
XML namespace of element. Exist onl when parsed with
xmlns: true
- #comment :
string
|Array.<string>
Comment or array of comments inside the XML Elements
Kind: global class
- Twig
- new Twig()
- new Twig(name, [parent], [attributes], [index])
- .attributes :
object
℗ - .text :
string
|number
℗ - .name :
string
℗ - .children :
Array.<Twig>
℗ - .parent :
Twig
|undefined
℗ - .pinned :
boolean
℗ - .purge
- .purgeUpTo
- .escapeEntity
- .isEmpty ⇒
boolean
- .level ⇒
number
- .isRoot ⇒
boolean
- .hasChildren ⇒
boolean
- .index ⇒
number
- .name ⇒
string
- .tag ⇒
string
- .text ⇒
string
- .text
- .pin
- .pinned ⇒
boolean
- .close
- .debug ⇒
string
- .toString ⇒
string
- .addChild ℗
- .writer ⇒
XMLWriter
- .attr ⇒
string
|number
|object
- .attributes ⇒
object
- .hasAttribute ⇒
boolean
- .attribute ⇒
object
- .deleteAttribute
- .root ⇒
Twig
- .parent ⇒
Twig
- .self ⇒
Twig
- .children ⇒
Array.<Twig>
- .next ⇒
Twig
- .previous ⇒
Twig
- .first ⇒
Twig
- .last ⇒
Twig
- .isFirstChild ⇒
boolean
- .isLastChild ⇒
boolean
- .descendant ⇒
Array.<Twig>
- .descendantOrSelf ⇒
Array.<Twig>
- .ancestor ⇒
Array.<Twig>
- .ancestorOrSelf ⇒
Array.<Twig>
- .sibling ⇒
Array.<Twig>
- .siblingOrSelf ⇒
Array.<Twig>
- .followingSibling ⇒
Array.<Twig>
- .precedingSibling ⇒
Array.<Twig>
- .nextSibling ⇒
Twig
- .prevSibling ⇒
Twig
- .find ⇒
Twig
- .addElement ⇒
Twig
- .delete
- .setRoot(name) ℗
- .filterElements(elements, [condition]) ⇒
Array.<Twig>
- .testElement(element, [condition]) ⇒
boolean
Generic class modeling a XML Node
Create a new Twig object
Param | Type | Description |
---|---|---|
name | string |
The name of the XML element |
[parent] | Twig |
The parent object |
[attributes] | object |
Attribute object |
[index] | string | number |
Position name 'first', 'last' or the position in the current children array.Defaults to 'last' |
XML attribute { <attribute 1>: <value 1>, <attribute 2>: <value 2>, ... }
Kind: instance property of Twig
Access: private
Content of XML Element
Kind: instance property of Twig
Access: private
The XML tag name
Kind: instance property of Twig
Access: private
twig.children : Array.<Twig>
℗
Child XML Elements
Kind: instance property of Twig
Access: private
twig.parent : Twig
| undefined
℗
The parent object. Undefined on root element
Kind: instance property of Twig
Access: private
Determines whether twig is needed in partial load
Kind: instance property of Twig
Access: private
Purges the current, typically used after element has been processed.
The root object cannot be purged.
Kind: instance property of Twig
Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.
The elt
object is not purged. If you like to purge including elt
, use .purgeUpTo(elt.previous())
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
[elt] | Twig |
Up to this element the tree will be purged. If undefined then the current element is purged (i.e. purge() ) |
Escapes special XML characters. According W3C specification these are only &, <, >, ", '
- this is a XML parser, not HTML!
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
text | string |
Input text to be escaped |
Returns true
if the element is empty, otherwise false
.
An empty element has no text nor any child elements, however empty elements can have attributes.
Kind: instance property of Twig
Returns: boolean
- true if empty element
Returns the level of the element. Root element has 0, children have 1, grand-children 2 and so on
Kind: instance property of Twig
Returns: number
- The level of the element.
Returns true
if element is the root object
Kind: instance property of Twig
Returns: boolean
- true if root element
Returns true
if element has child elements
Kind: instance property of Twig
Returns: boolean
- true if has child elements exists
The position in #children
array. For root object 0
Kind: instance property of Twig
Returns: number
- Position of element in parent
Returns the name of the element.
Kind: instance property of Twig
Returns: string
- Element name
Returns the name of the element. Synonym for twig.name
Kind: instance property of Twig
Returns: string
- Element name
The text of the element. No matter if given as text or CDATA entity
Kind: instance property of Twig
Returns: string
- Element text or empty string
Update the text of the element
Kind: instance property of Twig
Throws:
UnsupportedType
- If value is not a string, boolean or numeric type
Param | Type | Description |
---|---|---|
value | string | number | bigint | boolean |
New text of the element |
Pins the current element. Used for partial reading.
Kind: instance property of Twig
Checks if element is pinned
Kind: instance property of Twig
Returns: boolean
- true
when the element is pinned
Closes the element
Kind: instance property of Twig
XML-Twig for dummies :-)
Kind: instance property of Twig
Returns: string
- The XML-Tree which is currently available in RAM - no valid XML Structure
Returns XML string of the element
Kind: instance property of Twig
Returns: string
- The XML-Element as string
Internal recursive function used by writer()
Kind: instance property of Twig
Access: private
Param | Type | Description |
---|---|---|
xw | XMLWriter |
The writer object |
childArray | Array.<Twig> |
Array of child elements |
twig.writer ⇒ XMLWriter
Creates xml-writer from current element
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
par | boolean | string | XMLWriter |
true or intention character or an already created XMLWriter |
Returns attribute value or null
if not found.
If more than one matches the condition, then it returns object as attribute()
Kind: instance property of Twig
Returns: string
| number
| object
- - The value of the attribute or null
if the does not exist
Param | Type | Description |
---|---|---|
[condition] | AttributeCondition |
Optional condition to select attribute |
Returns all attributes of the element
Kind: instance property of Twig
Returns: object
- All XML Attributes
Check if the attribute exist or not
Kind: instance property of Twig
Returns: boolean
- - Returns true
if the attribute exists, else false
Param | Type | Description |
---|---|---|
name | string |
The name of the attribute |
Retrieve or update XML attribute. For update, the condition must be a string, i.e. must match to one attribute only.
Kind: instance property of Twig
Returns: object
- Attributes or null
if no matching attribute found
Param | Type | Description |
---|---|---|
[condition] | AttributeCondition |
Optional condition to select attributes |
[value] | string | number | bigint | boolean |
New value of the attribute. If undefined then existing attributes is returned. |
Example
attribute((name, val) => { return name === 'age' && val > 50})
attribute((name) => { return ['firstName', 'lastName'].includes(name) })
attribute('firstName')
attribute(/name/i)
Delete the attribute
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
name | string |
The attribute name |
twig.root ⇒ Twig
Returns the root object
Kind: instance property of Twig
Returns: Twig
- The root element of XML tree
twig.parent ⇒ Twig
Returns the parent element or null if root element
Kind: instance property of Twig
Returns: Twig
- The parament element
twig.self ⇒ Twig
Kind: instance property of Twig
Returns: Twig
- - The current element
twig.children ⇒ Array.<Twig>
All children, optionally matching condition
of the current element or empty array
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.next ⇒ Twig
Returns the next matching element.
Kind: instance property of Twig
Returns: Twig
- - The next element
See: https://www.w3.org/TR/xpath-datamodel-31/#document-order
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.previous ⇒ Twig
Returns the previous matching element.
Kind: instance property of Twig
Returns: Twig
- - The previous element
See: https://www.w3.org/TR/xpath-datamodel-31/#document-order
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.first ⇒ Twig
Returns the first matching element. This is usually the root element
Kind: instance property of Twig
Returns: Twig
- - The first element
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.last ⇒ Twig
Returns the last matching element.
Kind: instance property of Twig
Returns: Twig
- - The last element
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
Check if the element is the first child of the parent
Kind: instance property of Twig
Returns: boolean
- true
if the first child else false
Check if the element is the last child of the parent
Kind: instance property of Twig
Returns: boolean
- true
if the last child else false
twig.descendant ⇒ Array.<Twig>
Returns descendants (children, grandchildren, etc.) of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of descendants or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.descendantOrSelf ⇒ Array.<Twig>
Returns descendants (children, grandchildren, etc.) of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of descendants or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.ancestor ⇒ Array.<Twig>
Returns ancestors (parent, grandparent, etc.) of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of ancestors or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.ancestorOrSelf ⇒ Array.<Twig>
Returns ancestors (parent, grandparent, etc.) of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of ancestors or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.sibling ⇒ Array.<Twig>
Returns all sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.siblingOrSelf ⇒ Array.<Twig>
Returns all sibling element of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.followingSibling ⇒ Array.<Twig>
Returns all following sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.precedingSibling ⇒ Array.<Twig>
Returns all preceding sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.nextSibling ⇒ Twig
Returns next sibling element of the current element
Kind: instance property of Twig
Returns: Twig
- - The next sibling or null
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.prevSibling ⇒ Twig
Returns previous sibling element of the current element
Kind: instance property of Twig
Returns: Twig
- - The previous sibling or null
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.find ⇒ Twig
Find a specific element within current element. Same as .descendant(condition)[0]
Kind: instance property of Twig
Returns: Twig
- - First matching element or null
Param | Type | Description |
---|---|---|
condition | ElementCondition |
Find condition |
twig.addElement ⇒ Twig
Add a new element in the current element
Kind: instance property of Twig
Returns: Twig
- - The appended element
Param | Type | Description |
---|---|---|
name | string |
The tag name |
[text] | string |
Text of the element |
[attributes] | object |
Element attributes |
[position] | name | number |
Position name 'first', 'last' or the position in the children |
Deletes the current element from tree, same as purge()
. The root object cannot be deleted.
Kind: instance property of Twig
Sets the name of root element. In some cases the root is created before the XML-Root element is available
Used internally!
Kind: instance method of Twig
Access: private
Param | Type | Description |
---|---|---|
name | string |
The element name |
twig.filterElements(elements, [condition]) ⇒ Array.<Twig>
Common function to filter Twig elements from array
Kind: instance method of Twig
Returns: Array.<Twig>
- List of matching elements or empty array
Param | Type | Description |
---|---|---|
elements | Twig | Array.<Twig> |
Array of elements you like to filter or a single element |
[condition] | ElementCondition |
The filter condition |
Common function to filter Twig element
Kind: instance method of Twig
Returns: boolean
- true
if the condition matches
Param | Type | Description |
---|---|---|
element | Twig |
Element you like to filter |
[condition] | ElementCondition |
The filter condition |
Kind: global class
- Twig
- new Twig()
- new Twig(name, [parent], [attributes], [index])
- .attributes :
object
℗ - .text :
string
|number
℗ - .name :
string
℗ - .children :
Array.<Twig>
℗ - .parent :
Twig
|undefined
℗ - .pinned :
boolean
℗ - .purge
- .purgeUpTo
- .escapeEntity
- .isEmpty ⇒
boolean
- .level ⇒
number
- .isRoot ⇒
boolean
- .hasChildren ⇒
boolean
- .index ⇒
number
- .name ⇒
string
- .tag ⇒
string
- .text ⇒
string
- .text
- .pin
- .pinned ⇒
boolean
- .close
- .debug ⇒
string
- .toString ⇒
string
- .addChild ℗
- .writer ⇒
XMLWriter
- .attr ⇒
string
|number
|object
- .attributes ⇒
object
- .hasAttribute ⇒
boolean
- .attribute ⇒
object
- .deleteAttribute
- .root ⇒
Twig
- .parent ⇒
Twig
- .self ⇒
Twig
- .children ⇒
Array.<Twig>
- .next ⇒
Twig
- .previous ⇒
Twig
- .first ⇒
Twig
- .last ⇒
Twig
- .isFirstChild ⇒
boolean
- .isLastChild ⇒
boolean
- .descendant ⇒
Array.<Twig>
- .descendantOrSelf ⇒
Array.<Twig>
- .ancestor ⇒
Array.<Twig>
- .ancestorOrSelf ⇒
Array.<Twig>
- .sibling ⇒
Array.<Twig>
- .siblingOrSelf ⇒
Array.<Twig>
- .followingSibling ⇒
Array.<Twig>
- .precedingSibling ⇒
Array.<Twig>
- .nextSibling ⇒
Twig
- .prevSibling ⇒
Twig
- .find ⇒
Twig
- .addElement ⇒
Twig
- .delete
- .setRoot(name) ℗
- .filterElements(elements, [condition]) ⇒
Array.<Twig>
- .testElement(element, [condition]) ⇒
boolean
Generic class modeling a XML Node
Create a new Twig object
Param | Type | Description |
---|---|---|
name | string |
The name of the XML element |
[parent] | Twig |
The parent object |
[attributes] | object |
Attribute object |
[index] | string | number |
Position name 'first', 'last' or the position in the current children array.Defaults to 'last' |
XML attribute { <attribute 1>: <value 1>, <attribute 2>: <value 2>, ... }
Kind: instance property of Twig
Access: private
Content of XML Element
Kind: instance property of Twig
Access: private
The XML tag name
Kind: instance property of Twig
Access: private
twig.children : Array.<Twig>
℗
Child XML Elements
Kind: instance property of Twig
Access: private
twig.parent : Twig
| undefined
℗
The parent object. Undefined on root element
Kind: instance property of Twig
Access: private
Determines whether twig is needed in partial load
Kind: instance property of Twig
Access: private
Purges the current, typically used after element has been processed.
The root object cannot be purged.
Kind: instance property of Twig
Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.
The elt
object is not purged. If you like to purge including elt
, use .purgeUpTo(elt.previous())
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
[elt] | Twig |
Up to this element the tree will be purged. If undefined then the current element is purged (i.e. purge() ) |
Escapes special XML characters. According W3C specification these are only &, <, >, ", '
- this is a XML parser, not HTML!
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
text | string |
Input text to be escaped |
Returns true
if the element is empty, otherwise false
.
An empty element has no text nor any child elements, however empty elements can have attributes.
Kind: instance property of Twig
Returns: boolean
- true if empty element
Returns the level of the element. Root element has 0, children have 1, grand-children 2 and so on
Kind: instance property of Twig
Returns: number
- The level of the element.
Returns true
if element is the root object
Kind: instance property of Twig
Returns: boolean
- true if root element
Returns true
if element has child elements
Kind: instance property of Twig
Returns: boolean
- true if has child elements exists
The position in #children
array. For root object 0
Kind: instance property of Twig
Returns: number
- Position of element in parent
Returns the name of the element.
Kind: instance property of Twig
Returns: string
- Element name
Returns the name of the element. Synonym for twig.name
Kind: instance property of Twig
Returns: string
- Element name
The text of the element. No matter if given as text or CDATA entity
Kind: instance property of Twig
Returns: string
- Element text or empty string
Update the text of the element
Kind: instance property of Twig
Throws:
UnsupportedType
- If value is not a string, boolean or numeric type
Param | Type | Description |
---|---|---|
value | string | number | bigint | boolean |
New text of the element |
Pins the current element. Used for partial reading.
Kind: instance property of Twig
Checks if element is pinned
Kind: instance property of Twig
Returns: boolean
- true
when the element is pinned
Closes the element
Kind: instance property of Twig
XML-Twig for dummies :-)
Kind: instance property of Twig
Returns: string
- The XML-Tree which is currently available in RAM - no valid XML Structure
Returns XML string of the element
Kind: instance property of Twig
Returns: string
- The XML-Element as string
Internal recursive function used by writer()
Kind: instance property of Twig
Access: private
Param | Type | Description |
---|---|---|
xw | XMLWriter |
The writer object |
childArray | Array.<Twig> |
Array of child elements |
twig.writer ⇒ XMLWriter
Creates xml-writer from current element
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
par | boolean | string | XMLWriter |
true or intention character or an already created XMLWriter |
Returns attribute value or null
if not found.
If more than one matches the condition, then it returns object as attribute()
Kind: instance property of Twig
Returns: string
| number
| object
- - The value of the attribute or null
if the does not exist
Param | Type | Description |
---|---|---|
[condition] | AttributeCondition |
Optional condition to select attribute |
Returns all attributes of the element
Kind: instance property of Twig
Returns: object
- All XML Attributes
Check if the attribute exist or not
Kind: instance property of Twig
Returns: boolean
- - Returns true
if the attribute exists, else false
Param | Type | Description |
---|---|---|
name | string |
The name of the attribute |
Retrieve or update XML attribute. For update, the condition must be a string, i.e. must match to one attribute only.
Kind: instance property of Twig
Returns: object
- Attributes or null
if no matching attribute found
Param | Type | Description |
---|---|---|
[condition] | AttributeCondition |
Optional condition to select attributes |
[value] | string | number | bigint | boolean |
New value of the attribute. If undefined then existing attributes is returned. |
Example
attribute((name, val) => { return name === 'age' && val > 50})
attribute((name) => { return ['firstName', 'lastName'].includes(name) })
attribute('firstName')
attribute(/name/i)
Delete the attribute
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
name | string |
The attribute name |
twig.root ⇒ Twig
Returns the root object
Kind: instance property of Twig
Returns: Twig
- The root element of XML tree
twig.parent ⇒ Twig
Returns the parent element or null if root element
Kind: instance property of Twig
Returns: Twig
- The parament element
twig.self ⇒ Twig
Kind: instance property of Twig
Returns: Twig
- - The current element
twig.children ⇒ Array.<Twig>
All children, optionally matching condition
of the current element or empty array
Kind: instance property of Twig
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.next ⇒ Twig
Returns the next matching element.
Kind: instance property of Twig
Returns: Twig
- - The next element
See: https://www.w3.org/TR/xpath-datamodel-31/#document-order
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.previous ⇒ Twig
Returns the previous matching element.
Kind: instance property of Twig
Returns: Twig
- - The previous element
See: https://www.w3.org/TR/xpath-datamodel-31/#document-order
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.first ⇒ Twig
Returns the first matching element. This is usually the root element
Kind: instance property of Twig
Returns: Twig
- - The first element
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.last ⇒ Twig
Returns the last matching element.
Kind: instance property of Twig
Returns: Twig
- - The last element
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
Check if the element is the first child of the parent
Kind: instance property of Twig
Returns: boolean
- true
if the first child else false
Check if the element is the last child of the parent
Kind: instance property of Twig
Returns: boolean
- true
if the last child else false
twig.descendant ⇒ Array.<Twig>
Returns descendants (children, grandchildren, etc.) of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of descendants or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.descendantOrSelf ⇒ Array.<Twig>
Returns descendants (children, grandchildren, etc.) of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of descendants or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.ancestor ⇒ Array.<Twig>
Returns ancestors (parent, grandparent, etc.) of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of ancestors or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.ancestorOrSelf ⇒ Array.<Twig>
Returns ancestors (parent, grandparent, etc.) of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of ancestors or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.sibling ⇒ Array.<Twig>
Returns all sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.siblingOrSelf ⇒ Array.<Twig>
Returns all sibling element of the current element and the current element itself
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.followingSibling ⇒ Array.<Twig>
Returns all following sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.precedingSibling ⇒ Array.<Twig>
Returns all preceding sibling element of the current element
Kind: instance property of Twig
Returns: Array.<Twig>
- - Array of sibling or empty array
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.nextSibling ⇒ Twig
Returns next sibling element of the current element
Kind: instance property of Twig
Returns: Twig
- - The next sibling or null
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.prevSibling ⇒ Twig
Returns previous sibling element of the current element
Kind: instance property of Twig
Returns: Twig
- - The previous sibling or null
Param | Type | Description |
---|---|---|
[condition] | ElementCondition |
Optional condition |
twig.find ⇒ Twig
Find a specific element within current element. Same as .descendant(condition)[0]
Kind: instance property of Twig
Returns: Twig
- - First matching element or null
Param | Type | Description |
---|---|---|
condition | ElementCondition |
Find condition |
twig.addElement ⇒ Twig
Add a new element in the current element
Kind: instance property of Twig
Returns: Twig
- - The appended element
Param | Type | Description |
---|---|---|
name | string |
The tag name |
[text] | string |
Text of the element |
[attributes] | object |
Element attributes |
[position] | name | number |
Position name 'first', 'last' or the position in the children |
Deletes the current element from tree, same as purge()
. The root object cannot be deleted.
Kind: instance property of Twig
Sets the name of root element. In some cases the root is created before the XML-Root element is available
Used internally!
Kind: instance method of Twig
Access: private
Param | Type | Description |
---|---|---|
name | string |
The element name |
twig.filterElements(elements, [condition]) ⇒ Array.<Twig>
Common function to filter Twig elements from array
Kind: instance method of Twig
Returns: Array.<Twig>
- List of matching elements or empty array
Param | Type | Description |
---|---|---|
elements | Twig | Array.<Twig> |
Array of elements you like to filter or a single element |
[condition] | ElementCondition |
The filter condition |
Common function to filter Twig element
Kind: instance method of Twig
Returns: boolean
- true
if the condition matches
Param | Type | Description |
---|---|---|
element | Twig |
Element you like to filter |
[condition] | ElementCondition |
The filter condition |
Generic error for non implemented feature
Error for unsupported data types
Throws:
- UnsupportedParser
Param | Type | Description |
---|---|---|
t | string |
Parser type |
Generic error for unsupported data types
Throws:
- UnsupportedType
Param | Type | Description |
---|---|---|
t | * |
Parameter which was used |
Generic error for unsupported data types
Throws:
- UnsupportedCondition
Param | Type | Description |
---|---|---|
condition | * |
The condition value |
t | Array.<string> |
List of supported data types |
createParser(handler, [options]) ⇒ Parser
Create a new Twig parser
Kind: global function
Returns: Parser
- The parser Object
Throws:
UnsupportedParser
- For an unsupported parser. Currentlyexpat
andsax
(default) are supported.
Param | Type | Description |
---|---|---|
handler | TwigHandler | Array.<TwigHandler> |
Object or array of element specification and function to handle elements |
[options] | ParserOptions |
Object of optional options |
Common Event hanlder for starting tag
Kind: global function
Param | Type | Description |
---|---|---|
binds | object |
Additional parameter object |
node | object | string |
Node or Node name |
attrs | object |
Node Attributes |
Common Event hanlder for closing tag
Kind: global function
Param | Type | Description |
---|---|---|
handler | TwigHandler | Array.<TwigHandler> |
Object or array of element specification and function to handle elements |
options | ParserOptions |
Object of optional options |
name | string |
Event handler parameter |
Optional settings for the Twig parser
Kind: global typedef
Default: { method: 'sax', xmlns: false, trim: true, resumeAfterError: false, partial: false }
Properties
Name | Type | Description |
---|---|---|
[method] | 'sax' | 'expat' | 'saxophone' |
The underlying parser. Either 'sax' , 'expat' or 'saxophone' . |
[xmlns] | boolean |
If true , then namespaces are accessible by namespace property. |
[trim] | boolean |
If true , then turn any whitespace into a single space. Text and comments are trimmed. |
[resumeAfterError] | boolean |
If true then parser continues reading after an error. Otherwise it throws exception. |
[partial] | boolean |
If true then unhandled elements are purged. |
Example
{ method: 'expat', xmlns: true }
Reference to handler functions for Twig objects.
Element can be specified as string, Regular Expression, custom function, Twig.Root
or Twig.Any
You can specify a function
or a event
name
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
tag | HandlerCondition |
Element specification |
[function] | HandlerFunction |
Definition of handler function, either anonymous or explicit function |
[event] | string |
Type of the event to be emitted |
HandlerCondition : string
| Array.<string>
| RegExp
| HandlerConditionFilter
| Root
| Any
Condition to specify when handler shall be called
- If
string
then the element name must be equal to the string - If
string[]
then the element name must be included in string array - If
RegExp
then the element name must match the Regular Expression - If HandlerConditionFilter then function must return
true
- Use
Twig.Root
to call the handler on root element, i.e. when the end of document is reached - Use
Twig.Any
to call the handler on every element
Handler function for Twig objects, i.e. the way you like to use the XML element.
Kind: global typedef
Param | Type | Description |
---|---|---|
elt | Twig |
The current Twig element on which the function was called. |
Custom filter function to specify when handler shall be called
Kind: global typedef
Returns: boolean
- If the function returns true
, then it is included in the filter
Param | Type | Description |
---|---|---|
name | string |
Name of the element |
ElementCondition : string
| RegExp
| ElementConditionFilter
| Twig
| undefined
Optional condition to get elements
- If
undefined
, then all elements are returned. - If
string
then the element name must be equal to the string - If
RegExp
then the element name must match the Regular Expression - If ElementConditionFilter then function must return
true
- Use Twig object to find a specific element
Custom filter function to select desired elements
Kind: global typedef
Returns: boolean
- If the function returns true
, then it is included in the filter
Param | Type | Description |
---|---|---|
name | string |
Name of the element |
elt | Twig |
The Twig object |
Parser ⇒ sax
| node-expat
| saxophone
Kind: global typedef
Returns: sax
| node-expat
| saxophone
- The parser Object
Properties
Name | Type | Description |
---|---|---|
[currentLine] | number |
The currently processed line in the XML-File. Not available on saxophone parser. |
[currentColumn] | number |
The currently processed column in the XML-File. Not available on saxophone parser. |
AttributeCondition : string
| RegExp
| AttributeConditionFilter
Optional condition to get attributes
- If
undefined
, then all attributes are returned. - If
string
then the attribute name must be equal to the string - If
RegExp
then the attribute name must match the Regular Expression - If AttributeConditionFilter then the attribute must filter function
Custom filter function to get desired attributes
Kind: global typedef
Param | Type | Description |
---|---|---|
name | string |
Name of the attribute |
value | string | number |
Value of the attribute |
XML Processing Instruction object, exist only on root
XML Declaration object, exist only on root
XML namespace of element. Exist onl when parsed with xmlns: true
Comment or array of comments inside the XML Elements
Kind: global typedef