lexor.core.node module — lexor 0.1.2c0 documentation

# lexor.core.node module

This module defines the basic object of the document object model (DOM).

class `lexor.core.node.` `Node`

  Bases: `object`

  Primary datatype for the entire Document Object Model.

  `__init__`()

    Initializes all data descriptors to `None`. Each descriptor has an associated READ-ONLY property. Read the comment on each property to see what each descriptor represents.

  `node_name`

    Read-Only Property

    The name of this node. Its value depends on the node type. This property is associated with the attribute `name`.

  `owner_document`

    Read-Only Property

    The `Document` in which this node resides. This property is associated with the attribute `owner`.

  `parent_node`

    Read-Only Property

    The parent of this node. If the node has been just created or removed from a `Document` then this property is `None`. This property is associated with the attribute `parent`.

  `node_index`

    Read-Only Property

    The number of preceding siblings.

        >>> x is x.parent_node[x.node_index]
        True

    This property is associated with the attribute `index`.

  `node_level`

    Read-Only Property

    The nodes level of containtment in a `Document` object.

    This property is associated with the attribute `level`.

  `element_index`

    Read-Only Property

    The number of preceding element siblings.

  `previous_sibling`

    Read-Only Property

    The node immediately preceding this node. If this property is not None then

        >>> x.previous_sibling <==> x.parent_node[x.node_index - 1]

    This property is associated with the attribute `prev`.

  `next_sibling`

    Read-Only Property

    The node immediately following this node. If this property is not None then

        >>> x.next_sibling <==> x.parent_node[x.node_index + 1]

    This property is associated with the attribute `next`.

  `previous_element`

    Read-Only Property

    The last sibling `Element` preceding this node.

  `next_element`

    Read-Only Property

    The sibling `Element` after this node.

  `remove_children`()

    Remove all the child nodes.

  `__repr__`()

        >>> x.__repr__() == repr(x)
        True

  `__str__`()

        >>> x.__str__() == str(x)
        True

  `insert_before`(index, new_child)

    Inserts new_child to the list of children just before the child specified by index.

  `extend_before`(index, new_children)

    Inserts the contents of an iterable containing nodes just before the child specified by index. The following are equivalent:

        >>> while doc: node.parent.insert_before(index, doc[0])

        >>> node.extend_before(index, doc)

    The second form, however, has a more efficient reindexing method.

  `append_child`(new_child)

    Adds the node new_child to the end of the list of children of this node. If the node is a `DocumentFragment` then it appends its child nodes. Returns the calling node.

  `extend_children`(new_children)

    Extend the list of children by appending children from an iterable containing nodes.

  `append_after`(new_child)

    Place new_child after the node.

  `append_nodes_after`(new_children)

    Place new_children after the node.

  `prepend_before`(new_child)

    Place new_child before the node.

  `prepend_nodes_before`(new_children)

    Place new_children before the node.

  `normalize`()

    Removes empty `Text` nodes, and joins adjacent `Text` nodes.

  `__len__`()

    Return the number of child nodes.

        >>> x.__len__() == len(x)
        True

  `__getitem__`(i)

    Return the i-th child of this node.

        >>> x.__getitem__(i) <==> x[i]
        >>> x.__getitem__(slice(i, j)) <==> x[i:j]
        >>> x.__getitem__(slice(i, j, dt)) <==> x[i:j:dt]

    When using a slice, the `__getitem__` function will return a list with references to the requested nodes.

  `__delitem__`(index)

    Delete child nodes.

        >>> x.__delitem__(index) <==> del x[index]
        >>> x.__delitem__(slice(i, j)) <==> del x[i:j]
        >>> x.__delitem__(slice(i, j, dt)) <==> del x[i:j:dt]

  `__setitem__`(index, node)

    Replace child nodes.

        >>> x.__setitem__(index) = node <==> x[index] = node
        >>> x.__setitem__(slice(i, j)) = dfrag <==> x[i:j] = dfrag
        >>> x.__setitem__(slice(i, j, dt)) = dfrag <==> x[i:j:dt] = dfrag

    When using slices the nodes to be assigned to the indices need to be contained in a `DocumentFragment` node. This function does not support insertion as the regular slice for list does. To insert use a node use `insert_before()` or `append_after()`.

  `get_nodes_by_name`(name)

    Return a `list` of child nodes that have the given name.

  `set_parent`(parent, index)

    Helper Method

    Modifies the parent node and takes care of the child node levels.

  `disconnect`()

    Helper Method

    Reset its attributes.

  `set_prev`(node)

    Helper Method

    Sets the `prev` attribute.

  `set_next`(node)

    Helper Method

    Sets the `next` attribute.

  `increase_child_level`()

    Helper Method

    Sets the level of the child nodes.

  `append_child_node`(new_child)

    Helper Method

    Use this method to insert a node at a the end of the child list. See `append_child()` and `extend_children()` to see this method in action.

  `insert_node_before`(index, new_child)

    Helper Method

    Insert a new_child at a given index. See `insert_before()` and `extend_before()` to see this method in action.