Skip to content

Latest commit

 

History

History
1643 lines (1195 loc) · 61.4 KB

twig.md

File metadata and controls

1643 lines (1195 loc) · 61.4 KB

Classes

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

Constants

Root : RootHandler
Any : AnyHandler

Functions

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

Typedefs

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 or Twig.Any
You can specify a function or a event 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
HandlerFunction : function

Handler function for Twig objects, i.e. the way you like to use the XML element.

HandlerConditionFilterboolean

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
ElementConditionFilterboolean

Custom filter function to select desired elements

Parsersax | 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
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

Twig

Kind: global class

new Twig()

Generic class modeling a XML Node

new Twig(name, [parent], [attributes], [index])

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'

twig.attributes : object

XML attribute { <attribute 1>: <value 1>, <attribute 2>: <value 2>, ... }

Kind: instance property of Twig
Access: private

twig.text : string | number

Content of XML Element

Kind: instance property of Twig
Access: private

twig.name : string

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

twig.pinned : boolean

Determines whether twig is needed in partial load

Kind: instance property of Twig
Access: private

twig.purge

Purges the current, typically used after element has been processed.
The root object cannot be purged.

Kind: instance property of Twig

twig.purgeUpTo

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

twig.escapeEntity

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

twig.isEmpty ⇒ boolean

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

twig.level ⇒ number

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.

twig.isRoot ⇒ boolean

Returns true if element is the root object

Kind: instance property of Twig
Returns: boolean - true if root element

twig.hasChildren ⇒ boolean

Returns true if element has child elements

Kind: instance property of Twig
Returns: boolean - true if has child elements exists

twig.index ⇒ number

The position in #children array. For root object 0

Kind: instance property of Twig
Returns: number - Position of element in parent

twig.name ⇒ string

Returns the name of the element.

Kind: instance property of Twig
Returns: string - Element name

twig.tag ⇒ string

Returns the name of the element. Synonym for twig.name

Kind: instance property of Twig
Returns: string - Element name

twig.text ⇒ string

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

twig.text

Update the text of the element

Kind: instance property of Twig
Throws:

Param Type Description
value string | number | bigint | boolean New text of the element

twig.pin

Pins the current element. Used for partial reading.

Kind: instance property of Twig

twig.pinned ⇒ boolean

Checks if element is pinned

Kind: instance property of Twig
Returns: boolean - true when the element is pinned

twig.close

Closes the element

Kind: instance property of Twig

twig.debug ⇒ string

XML-Twig for dummies :-)

Kind: instance property of Twig
Returns: string - The XML-Tree which is currently available in RAM - no valid XML Structure

twig.toString ⇒ string

Returns XML string of the element

Kind: instance property of Twig
Returns: string - The XML-Element as string

twig.addChild ℗

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

twig.attr ⇒ string | number | object

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

twig.attributes ⇒ object

Returns all attributes of the element

Kind: instance property of Twig
Returns: object - All XML Attributes

twig.hasAttribute ⇒ boolean

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

twig.attribute ⇒ object

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)

twig.deleteAttribute

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

twig.isFirstChild ⇒ boolean

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

twig.isLastChild ⇒ boolean

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

twig.delete

Deletes the current element from tree, same as purge(). The root object cannot be deleted.

Kind: instance property of Twig

twig.setRoot(name) ℗

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

twig.testElement(element, [condition]) ⇒ boolean

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

Twig

Kind: global class

new Twig()

Generic class modeling a XML Node

new Twig(name, [parent], [attributes], [index])

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'

twig.attributes : object

XML attribute { <attribute 1>: <value 1>, <attribute 2>: <value 2>, ... }

Kind: instance property of Twig
Access: private

twig.text : string | number

Content of XML Element

Kind: instance property of Twig
Access: private

twig.name : string

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

twig.pinned : boolean

Determines whether twig is needed in partial load

Kind: instance property of Twig
Access: private

twig.purge

Purges the current, typically used after element has been processed.
The root object cannot be purged.

Kind: instance property of Twig

twig.purgeUpTo

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

twig.escapeEntity

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

twig.isEmpty ⇒ boolean

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

twig.level ⇒ number

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.

twig.isRoot ⇒ boolean

Returns true if element is the root object

Kind: instance property of Twig
Returns: boolean - true if root element

twig.hasChildren ⇒ boolean

Returns true if element has child elements

Kind: instance property of Twig
Returns: boolean - true if has child elements exists

twig.index ⇒ number

The position in #children array. For root object 0

Kind: instance property of Twig
Returns: number - Position of element in parent

twig.name ⇒ string

Returns the name of the element.

Kind: instance property of Twig
Returns: string - Element name

twig.tag ⇒ string

Returns the name of the element. Synonym for twig.name

Kind: instance property of Twig
Returns: string - Element name

twig.text ⇒ string

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

twig.text

Update the text of the element

Kind: instance property of Twig
Throws:

Param Type Description
value string | number | bigint | boolean New text of the element

twig.pin

Pins the current element. Used for partial reading.

Kind: instance property of Twig

twig.pinned ⇒ boolean

Checks if element is pinned

Kind: instance property of Twig
Returns: boolean - true when the element is pinned

twig.close

Closes the element

Kind: instance property of Twig

twig.debug ⇒ string

XML-Twig for dummies :-)

Kind: instance property of Twig
Returns: string - The XML-Tree which is currently available in RAM - no valid XML Structure

twig.toString ⇒ string

Returns XML string of the element

Kind: instance property of Twig
Returns: string - The XML-Element as string

twig.addChild ℗

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

twig.attr ⇒ string | number | object

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

twig.attributes ⇒ object

Returns all attributes of the element

Kind: instance property of Twig
Returns: object - All XML Attributes

twig.hasAttribute ⇒ boolean

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

twig.attribute ⇒ object

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)

twig.deleteAttribute

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

twig.isFirstChild ⇒ boolean

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

twig.isLastChild ⇒ boolean

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

twig.delete

Deletes the current element from tree, same as purge(). The root object cannot be deleted.

Kind: instance property of Twig

twig.setRoot(name) ℗

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

twig.testElement(element, [condition]) ⇒ boolean

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

NotImplementedYet

Generic error for non implemented feature

Kind: global class

UnsupportedParser

Error for unsupported data types

Kind: global class

new UnsupportedParser(t)

Throws:

  • UnsupportedParser
Param Type Description
t string Parser type

UnsupportedType

Generic error for unsupported data types

Kind: global class

new UnsupportedType(t)

Throws:

  • UnsupportedType
Param Type Description
t * Parameter which was used

UnsupportedCondition

Generic error for unsupported data types

Kind: global class

new UnsupportedCondition(condition, t)

Throws:

  • UnsupportedCondition
Param Type Description
condition * The condition value
t Array.<string> List of supported data types

Root : RootHandler

Kind: global constant

Any : AnyHandler

Kind: global constant

createParser(handler, [options]) ⇒ Parser

Create a new Twig parser

Kind: global function
Returns: Parser - The parser Object
Throws:

  • UnsupportedParser - For an unsupported parser. Currently expat and sax (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

onStart(binds, node, attrs)

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

onClose(handler, options, name)

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

ParserOptions

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 }

TwigHandler

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

Kind: global typedef

HandlerFunction : function

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.

HandlerConditionFilter ⇒ boolean

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

Kind: global typedef

ElementConditionFilter ⇒ boolean

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

Kind: global typedef

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

#PI : object

XML Processing Instruction object, exist only on root

Kind: global typedef

#declaration : object

XML Declaration object, exist only on root

Kind: global typedef

#namespace : object

XML namespace of element. Exist onl when parsed with xmlns: true

Kind: global typedef

#comment : string | Array.<string>

Comment or array of comments inside the XML Elements

Kind: global typedef