From 965cec4a9f5305af6c36dcf99068a6686935dad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=99=EC=A3=BC?= Date: Thu, 27 May 2021 15:17:54 +0900 Subject: [PATCH 1/4] Start implementing NodeList --- python/dom/Node.py | 2 +- python/dom/NodeList.py | 8 ++++++++ python/dom/NotImplemented/NodeList.py | 2 -- python/dom/NotImplemented/__init__.py | 1 - python/dom/__init__.py | 1 + python/dom/type_checking.py | 1 + 6 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 python/dom/NodeList.py delete mode 100644 python/dom/NotImplemented/NodeList.py 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..46cdfe4 --- /dev/null +++ b/python/dom/NodeList.py @@ -0,0 +1,8 @@ +class NodeList: + """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. + """ + pass \ No newline at end of file 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, From 687a8fdb792c87e08763faf254b308c8e38c6eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=99=EC=A3=BC?= Date: Thu, 27 May 2021 23:13:28 +0900 Subject: [PATCH 2/4] Make class `NodeList` more list-like by inheriting `list` --- python/dom/NodeList.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/dom/NodeList.py b/python/dom/NodeList.py index 46cdfe4..b14464f 100644 --- a/python/dom/NodeList.py +++ b/python/dom/NodeList.py @@ -1,4 +1,4 @@ -class NodeList: +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. From 6f4f89d6394558bac76f166d774a4be2cb8fa2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=99=EC=A3=BC?= Date: Thu, 27 May 2021 23:14:11 +0900 Subject: [PATCH 3/4] Add readonly attribute `length` --- python/dom/NodeList.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/dom/NodeList.py b/python/dom/NodeList.py index b14464f..7c27e50 100644 --- a/python/dom/NodeList.py +++ b/python/dom/NodeList.py @@ -5,4 +5,9 @@ class NodeList(list): The items in the `NodeList` are accessible via an integral index, starting from 0. """ - pass \ No newline at end of file + + @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) From 9e04f639c1bb654508ce31ed5fcdc680cd2d76d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=99=EC=A3=BC?= Date: Thu, 27 May 2021 23:15:51 +0900 Subject: [PATCH 4/4] Add method `item` --- python/dom/NodeList.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/dom/NodeList.py b/python/dom/NodeList.py index 7c27e50..7e23ea4 100644 --- a/python/dom/NodeList.py +++ b/python/dom/NodeList.py @@ -1,3 +1,10 @@ +from __future__ import absolute_import + +from typing import Optional + +from python.dom.type_checking import AnyNode + + class NodeList(list): """Interface NodeList @@ -11,3 +18,17 @@ 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