diff --git a/python/dom/Node.py b/python/dom/Node.py index 5f6fdc4..c3ec01a 100644 --- a/python/dom/Node.py +++ b/python/dom/Node.py @@ -6,7 +6,7 @@ from python.dom.DOMException import DOMException from python.dom.DOMString import DOMString from python.dom.NotImplemented.NamedNodeMap import NamedNodeMap -from python.dom.NotImplemented.NodeList import NodeList +from python.dom.NodeList import NodeList from python.dom.type_checking import AnyNode diff --git a/python/dom/NodeList.py b/python/dom/NodeList.py new file mode 100644 index 0000000..7e23ea4 --- /dev/null +++ b/python/dom/NodeList.py @@ -0,0 +1,34 @@ +from __future__ import absolute_import + +from typing import Optional + +from python.dom.type_checking import AnyNode + + +class NodeList(list): + """Interface NodeList + + The `NodeList` interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. + + The items in the `NodeList` are accessible via an integral index, starting from 0. + """ + + @property + def length(self) -> int: + """The number of nodes in the list. The range of valid child node indices is 0 to `length`-1 inclusive. + """ + return len(self) + + def item(self, index: int) -> Optional[AnyNode]: + """Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns `None`. + + Args: + index: Index into the collection. + + Returns: + The node at the `index`th position in the NodeList, or `None` if that is not a valid index. + """ + if 0 <= index < self.length: + return self[index] + else: + return None diff --git a/python/dom/NotImplemented/NodeList.py b/python/dom/NotImplemented/NodeList.py deleted file mode 100644 index ff5b1cd..0000000 --- a/python/dom/NotImplemented/NodeList.py +++ /dev/null @@ -1,2 +0,0 @@ -class NodeList: - pass \ No newline at end of file diff --git a/python/dom/NotImplemented/__init__.py b/python/dom/NotImplemented/__init__.py index 0e362b7..fe65252 100644 --- a/python/dom/NotImplemented/__init__.py +++ b/python/dom/NotImplemented/__init__.py @@ -12,7 +12,6 @@ from python.dom.NotImplemented.Entity import Entity from python.dom.NotImplemented.EntityReference import EntityReference from python.dom.NotImplemented.NamedNodeMap import NamedNodeMap -from python.dom.NotImplemented.NodeList import NodeList from python.dom.NotImplemented.Notation import Notation from python.dom.NotImplemented.ProcessingInstruction import ProcessingInstruction from python.dom.NotImplemented.Text import Text diff --git a/python/dom/__init__.py b/python/dom/__init__.py index 4afc3ec..b5c2a8c 100644 --- a/python/dom/__init__.py +++ b/python/dom/__init__.py @@ -3,3 +3,4 @@ from python.dom.DOMException import DOMException from python.dom.DOMString import DOMString from python.dom.Node import Node +from python.dom.NodeList import NodeList diff --git a/python/dom/type_checking.py b/python/dom/type_checking.py index be5b87a..456f736 100644 --- a/python/dom/type_checking.py +++ b/python/dom/type_checking.py @@ -7,6 +7,7 @@ from python.dom.DOMException import DOMException from python.dom.DOMString import DOMString from python.dom.Node import Node + from python.dom.NodeList import NodeList AnyNode = Union[ Node,