Skip to content

Commit 6edfd99

Browse files
committed
Create readonly attribute attributes #16
TODO: Implement interface `NamedNodeMap` #19
1 parent e7e7f94 commit 6edfd99

File tree

1 file changed

+19
-1
lines changed
  • w3/python/core/fundamental_interface

1 file changed

+19
-1
lines changed

w3/python/core/fundamental_interface/Node.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import enum
4-
from typing import Iterable, Optional
4+
from typing import Dict, Iterable, Optional
55

66
from w3.python.core.fundamental_interface.DOMException import DOMException
77
from w3.python.core.fundamental_interface.NodeList import NodeList
@@ -67,6 +67,7 @@ def __init__(self,
6767
node_value: Optional[DOMString] = None,
6868
parent_node: Optional[_AnyNode] = None,
6969
child_nodes: Optional[Iterable[_AnyNode]] = None,
70+
attributes: Optional[Iterable[_AnyNode]] = None,
7071
read_only: bool = False) -> None:
7172
if node_value is None:
7273
node_value = ''
@@ -76,13 +77,15 @@ def __init__(self,
7677
self._set_node_value(node_value)
7778
self._set_parent_node(parent_node)
7879
self._init_child_nodes(child_nodes)
80+
self._init_attributes(attributes)
7981
self._read_only = bool(read_only)
8082
# Attributes
8183
self._node_type: NodeType
8284
self._node_name: DOMString
8385
self._node_value: DOMString
8486
self._parent_node: _AnyNode
8587
self._child_nodes: NodeList
88+
self._attributes: _NamedNodeMap
8689
self._read_only: bool
8790

8891
def _check_modifiable(self) -> None:
@@ -220,5 +223,20 @@ def _nth_child_of_parent(self) -> Optional[int]:
220223
return None
221224
return self.parent_node.child_nodes.index(self)
222225

226+
@property
227+
def attributes(self) -> _NamedNodeMap:
228+
"""A `NamedNodeMap` containing the attributes of this node (if it is an `Element`) or `None` otherwise.
229+
"""
230+
return self._attributes
231+
232+
def _init_attributes(self,
233+
attributes: Optional[Iterable[_AnyNode]] = None) -> None:
234+
self._attributes: _NamedNodeMap = {} # TODO: Replace with real NamedNodeMap #19
235+
if attributes is None:
236+
return
237+
for attr in iter(attributes):
238+
self._attributes.set_named_item(attr)
239+
223240

224241
_AnyNode = Node
242+
_NamedNodeMap = Dict[str, _AnyNode] # TODO: Implement NamedNodeMap #19

0 commit comments

Comments
 (0)