Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b2ebd54

Browse files
committed
Update interface.py
Add the template of `Node`
1 parent a6e3e28 commit b2ebd54

File tree

1 file changed

+292
-18
lines changed

1 file changed

+292
-18
lines changed

‎w3/python/core/interface.py‎

Lines changed: 292 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

33
from ctypes import c_ushort, c_ulong
4-
from typing import Callable
4+
from typing import Callable, Optional
55

6+
from w3.python.core.exception import DOMException
67
from w3.python.core.type import DOMString
78

89

@@ -73,23 +74,296 @@ class Node:
7374
DOCUMENT_FRAGMENT_NODE: c_ushort = c_ushort(11)
7475
NOTATION_NODE: c_ushort = c_ushort(12)
7576

76-
nodeName: DOMString
77-
nodeValue: DOMString
78-
nodeType: c_ushort
79-
parentNode: Node
80-
childNodes: NodeList
81-
firstChild: Node
82-
lastChild: Node
83-
previousSibling: Node
84-
nextSibling: Node
85-
attributes: NamedNodeMap
86-
ownerDocument: Document
87-
insertBefore: Callable[[Node, Node], Node]
88-
replaceChild: Callable[[Node, Node], Node]
89-
removeChild: Callable[[Node], Node]
90-
appendChild: Callable[[Node], Node]
91-
hasChildNodes: Callable[[], bool]
92-
cloneNodes: Callable[[bool], Node]
77+
def __init__(self) -> None:
78+
# Accessor about this node's modification
79+
self._read_only: bool
80+
# Accessors about this node's properties
81+
self._node_type: c_ushort
82+
self._node_name: DOMString
83+
self._node_value: DOMString
84+
self._attributes: NamedNodeMap
85+
# Accessors about DOM Tree
86+
self._owner_document: Optional[Document]
87+
self._parent_node: Optional[Node]
88+
self._next_sibling_node: Optional[Node]
89+
self._prev_sibling_node: Optional[Node]
90+
self._child_nodes: NodeList
91+
# Methods typing hints
92+
self.insertBefore: Callable[[Node, Node], Node]
93+
self.replaceChild: Callable[[Node, Node], Node]
94+
self.removeChild: Callable[[Node], Node]
95+
self.appendChild: Callable[[Node], Node]
96+
self.hasChildNodes: Callable[[], bool]
97+
self.cloneNodes: Callable[[bool], Node]
98+
99+
def _getNodeName(self) -> DOMString:
100+
"""Indirect accessor to get the 'nodeName' property."""
101+
return self._node_name
102+
103+
def _setNodeName(self, name: DOMString) -> None:
104+
"""Indirect accessor to set the 'nodeName' property."""
105+
self._node_name = DOMString(name)
106+
107+
def _getNodeValue(self) -> DOMString:
108+
"""Indirect accessor to get the 'nodeValue' property.
109+
110+
Raises:
111+
DOMException:
112+
- DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a `DOMString` variable on the implementation platform.
113+
"""
114+
# XXX: DOMException.DOMSTRING_SIZE_ERR was not taken into account.
115+
return self._node_value
116+
117+
def _setNodeValue(self, value: DOMString) -> None:
118+
"""Indirect accessor to set the 'nodeValue' property."""
119+
self._node_value = DOMString(value)
120+
121+
def _getNodeType(self) -> c_ushort:
122+
"""Indirect accessor to get the 'nodeType' property."""
123+
return self._node_type
124+
125+
def _setNodeType(self, type: c_ushort) -> None:
126+
"""Indirect accessor to set the 'nodeType' property."""
127+
self._node_type = c_ushort(type)
128+
129+
def _getParentNode(self) -> Node:
130+
"""Indirect accessor to get the 'parentNode' property."""
131+
return self._parent_node
132+
133+
def _setParentNode(self, node: Optional[Node] = None) -> None:
134+
"""Indirect accessor to set the 'parentNode' property."""
135+
self._parent_node = node
136+
137+
def _getChildNodes(self) -> NodeList:
138+
"""Indirect accessor to get the 'childNodes' property."""
139+
return self._child_nodes
140+
141+
def _getFirstChild(self) -> Node:
142+
"""Indirect accessor to get the 'firstChild' property."""
143+
raise NotImplementedError()
144+
145+
def _getLastChild(self) -> Node:
146+
"""Indirect accessor to get the 'lastChild' property."""
147+
raise NotImplementedError()
148+
149+
def _getPreviousSibling(self) -> Node:
150+
"""Indirect accessor to get the 'previousSibling' property."""
151+
return self._prev_sibling_node
152+
153+
def _getNextSibling(self) -> Node:
154+
"""Indirect accessor to get the 'nextSibling' property."""
155+
return self._next_sibling_node
156+
157+
def _getAttributes(self) -> NamedNodeMap:
158+
"""Indirect accessor to get the 'attributes' property."""
159+
return self._attributes
160+
161+
def _getOwnerDocument(self) -> Document:
162+
"""Indirect accessor to get the 'ownerDocument' property."""
163+
return self._owner_document
164+
165+
def _setOwnerDocument(self, owner_document: Document) -> None:
166+
"""Indirect accessor to set the 'ownerDocument' property."""
167+
self._owner_document = owner_document
168+
169+
@property
170+
def nodeName(self) -> DOMString:
171+
"""The name of this node, depending on its type."""
172+
return self._getNodeName()
173+
174+
@property
175+
def nodeValue(self) -> DOMString:
176+
"""The value of this node, depending on its type.
177+
178+
Raises:
179+
Exceptions on setting
180+
DOMException:
181+
NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
182+
183+
Exceptions on retrieval
184+
DOMException:
185+
DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a `DOMString` variable on the implementation platform.
186+
"""
187+
return self._getNodeValue()
188+
189+
@nodeValue.setter
190+
def nodeValue(self, value: DOMString) -> None:
191+
self._setNodeValue(value)
192+
193+
@property
194+
def nodeType(self) -> c_ushort:
195+
"""A code representing the type of the underlying object."""
196+
return self._getNodeType()
197+
198+
@property
199+
def parentNode(self) -> Optional[Node]:
200+
"""The parent of this node.
201+
202+
All nodes, except `Document`, `DocumentFragment`, and `Attr` may have a parent.
203+
However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is `None`.
204+
"""
205+
return self._getParentNode()
206+
207+
@property
208+
def childNodes(self) -> NodeList:
209+
"""A `NodeList` that contains all children of this node.
210+
211+
If there are no children, this is a `NodeList` containing no nodes.
212+
The content of the returned `NodeList` is "live" in the sense that, for instance, changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the `NodeList` accessors; it is not a static snapshot of the content of the node.
213+
This is true for every `NodeList`, including the ones returned by the `getElementsByTagName` method.
214+
"""
215+
return self._getChildNodes()
216+
217+
@property
218+
def firstChild(self) -> Node:
219+
"""The first child of this node.
220+
221+
If there is no such node, this returns `null`."""
222+
return self._getFirstChild()
223+
224+
@property
225+
def lastChild(self) -> Node:
226+
"""The last child of this node.
227+
228+
If there is no such node, this returns `null`."""
229+
return self._getLastChild()
230+
231+
@property
232+
def previousSibling(self) -> Node:
233+
"""The node immediately preceding this node.
234+
235+
If there is no such node, this returns `null`."""
236+
return self._getPreviousSibling()
237+
238+
@property
239+
def nextSibling(self) -> Node:
240+
"""The node immediately following this node.
241+
242+
If there is no such node, this returns `None`.
243+
"""
244+
return self._getNextSibling()
245+
246+
@property
247+
def attributes(self) -> NamedNodeMap:
248+
"""A `NamedNodeMap` containing the attributes of this node (if it is an `Element`) or `None` otherwise.
249+
"""
250+
return self._getAttributes()
251+
252+
@property
253+
def ownerDocument(self) -> Document:
254+
"""The `Document` object associated with this node.
255+
256+
This is also the `Document` object used to create new nodes.
257+
When this node is a `Document` this is `None`.
258+
"""
259+
return self._getOwnerDocument()
260+
261+
def insertBefore(self, newChild: Node, refChild: Node) -> Node:
262+
"""Inserts the node `newChild` before the existing child node `refChild`.
263+
264+
If `refChild` is `None`, insert `newChild` at the end of the list of children.
265+
If `newChild` is a `DocumentFragment` object, all of its children are inserted, in the same order, before `refChild`.
266+
If the `newChild` is already in the tree, it is first removed.
267+
268+
Args:
269+
newChild: The node to insert.
270+
refChild: The reference node, i.e., the node before which the new node must be inserted.
271+
272+
Returns:
273+
The node being inserted.
274+
275+
Raises:
276+
DOMException:
277+
- HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the `newChild` node, or if the node to insert is one of this node's ancestors.
278+
- WRONG_DOCUMENT_ERR: Raised if `newChild` was created from a different document than the one that created this node.
279+
- NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
280+
- NOT_FOUND_ERR: Raised if `refChild` is not a child of this node.
281+
"""
282+
raise NotImplementedError()
283+
284+
def replaceChild(self, newChild: Node, oldChild: Node) -> Node:
285+
"""Replaces the child node `oldChild` with `newChild` in the list of children, and returns the `oldChild` node.
286+
287+
If the `newChild` is already in the tree, it is first removed.
288+
289+
Args:
290+
newChild: The new node to put in the child list.
291+
oldChild: The node being replaced in the list.
292+
293+
Returns:
294+
The node replaced.
295+
296+
Raises:
297+
DOMException:
298+
- HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the `newChild` node, or it the node to put in is one of this node's ancestors.
299+
- WRONG_DOCUMENT_ERR: Raised if `newChild` was created from a different document than the one that created this node.
300+
- NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
301+
- NOT_FOUND_ERR: Raised if `oldChild` is not a child of this node.
302+
"""
303+
raise NotImplementedError()
304+
305+
def removeChild(self, oldChild: Node) -> Node:
306+
"""Removes the child node indicated by `oldChild` from the list of children, and returns it.
307+
308+
Args:
309+
oldChild: The node being removed.
310+
311+
Returns:
312+
The node removed.
313+
314+
Raises:
315+
DOMException:
316+
- NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
317+
- NOT_FOUND_ERR: Raised if `oldChild` is not a child of this node.
318+
"""
319+
raise NotImplementedError()
320+
321+
def appendChild(self, newChild: Node) -> Node:
322+
"""Adds the node `newChild` to the end of the list of children of this node.
323+
324+
If the `newChild` is already in the tree, it is first removed.
325+
326+
Args:
327+
newChild: The node to add. If it is a `DocumentFragment` object, the entire contents of the document fragment are moved into the child list of this node
328+
329+
Returns:
330+
The node added.
331+
332+
Raises:
333+
DOMException:
334+
- HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the `newChild` node, or if the node to append is one of this node's ancestors.
335+
- WRONG_DOCUMENT_ERR: Raised if `newChild` was created from a different document than the one that created this node.
336+
- NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
337+
"""
338+
raise NotImplementedError()
339+
340+
def hasChildNodes(self) -> bool:
341+
"""This is a convenience method to allow easy determination of whether a node has any children.
342+
343+
Returns:
344+
`True` if the node has any children, `False` if the node has no children.
345+
346+
This method has no parameters.
347+
This method raises no exceptions.
348+
"""
349+
raise NotImplementedError()
350+
351+
def cloneNode(self) -> Node:
352+
"""Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.
353+
354+
The duplicate node has no parent (`parentNode` returns `None`.).
355+
Cloning an `Element` copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child `Text` node.
356+
Cloning any other type of node simply returns a copy of this node.
357+
358+
Args:
359+
deep: If `True`, recursively clone the subtree under the specified node; if `False`, clone only the node itself (and its attributes, if it is an `Element`).
360+
361+
Returns:
362+
The duplicate node.
363+
364+
This method raises no exceptions.
365+
"""
366+
raise NotImplementedError()
93367

94368

95369
class DocumentFragment(Node):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /