Class Node
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
In addition, every kind of DOM node is represented by an interface based onNode. These include Attr, CharacterData
(which Text, Comment, CDATASection and
ProcessingInstruction are all based on), and DocumentType.
[Value("Node")]
public class Node : EventTarget
- Inheritance
-
Node
- Derived
- Inherited Members
Remarks
In some cases, a particular feature of the base Node interface may not
apply to one of its child interfaces; in that case, the inheriting node may
return null or throw an exception, depending on circumstances. For example,
attempting to add children to a node type that cannot have children will throw an
exception.
Constructors
Node()
public Node()
Fields
ATTRIBUTE_NODE
[Value("ATTRIBUTE_NODE")]
public const ushort ATTRIBUTE_NODE = 2
Field Value
CDATA_SECTION_NODE
[Value("CDATA_SECTION_NODE")]
public const ushort CDATA_SECTION_NODE = 4
Field Value
COMMENT_NODE
[Value("COMMENT_NODE")]
public const ushort COMMENT_NODE = 8
Field Value
DOCUMENT_FRAGMENT_NODE
[Value("DOCUMENT_FRAGMENT_NODE")]
public const ushort DOCUMENT_FRAGMENT_NODE = 11
Field Value
DOCUMENT_NODE
[Value("DOCUMENT_NODE")]
public const ushort DOCUMENT_NODE = 9
Field Value
DOCUMENT_POSITION_CONTAINED_BY
[Value("DOCUMENT_POSITION_CONTAINED_BY")]
public const ushort DOCUMENT_POSITION_CONTAINED_BY = 16
Field Value
DOCUMENT_POSITION_CONTAINS
[Value("DOCUMENT_POSITION_CONTAINS")]
public const ushort DOCUMENT_POSITION_CONTAINS = 8
Field Value
DOCUMENT_POSITION_DISCONNECTED
[Value("DOCUMENT_POSITION_DISCONNECTED")]
public const ushort DOCUMENT_POSITION_DISCONNECTED = 1
Field Value
DOCUMENT_POSITION_FOLLOWING
[Value("DOCUMENT_POSITION_FOLLOWING")]
public const ushort DOCUMENT_POSITION_FOLLOWING = 4
Field Value
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
[Value("DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC")]
public const ushort DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32
Field Value
DOCUMENT_POSITION_PRECEDING
[Value("DOCUMENT_POSITION_PRECEDING")]
public const ushort DOCUMENT_POSITION_PRECEDING = 2
Field Value
DOCUMENT_TYPE_NODE
[Value("DOCUMENT_TYPE_NODE")]
public const ushort DOCUMENT_TYPE_NODE = 10
Field Value
ELEMENT_NODE
[Value("ELEMENT_NODE")]
public const ushort ELEMENT_NODE = 1
Field Value
ENTITY_NODE
[Value("ENTITY_NODE")]
public const ushort ENTITY_NODE = 6
Field Value
ENTITY_REFERENCE_NODE
[Value("ENTITY_REFERENCE_NODE")]
public const ushort ENTITY_REFERENCE_NODE = 5
Field Value
NOTATION_NODE
[Value("NOTATION_NODE")]
public const ushort NOTATION_NODE = 12
Field Value
PROCESSING_INSTRUCTION_NODE
[Value("PROCESSING_INSTRUCTION_NODE")]
public const ushort PROCESSING_INSTRUCTION_NODE = 7
Field Value
TEXT_NODE
[Value("TEXT_NODE")]
public const ushort TEXT_NODE = 3
Field Value
Properties
BaseURI
The read-only baseURI property of the Node interface
returns the absolute base URL of the document containing the node.
[Value("baseURI")]
public string BaseURI { get; }
Property Value
Remarks
The base URL is used to resolve relative URLs when the browser needs to obtain an absolute URL, for example when processing the HTML img element's src attribute or the xlink:href {{deprecated_inline}} or href attributes in SVG.
Although this property is read-only, its value is determined by an algorithm each time
the property is accessed, and may change if the conditions changed.
The base URL is determined as follows:
-base element.
ChildNodes
The read-only childNodes property of the Node interface returns a live
NodeList of child Node of the given element where
the first child node is assigned index 0. Child nodes include elements, text and
comments.
[Value("childNodes")]
public NodeList ChildNodes { get; }
Property Value
- NodeList
A live NodeList containing the children of the node.
NOTE
Several calls tochildNodesreturn the same NodeList.
Remarks
NOTE
The NodeList being live means that its content is changed each time
new children are added or removed.Browsers insert text nodes into a document to represent whitespace in the source markup.
Therefore a node obtained, for example, usingNode.childNodes[0]
may refer to a whitespace text node rather than the actual element the author intended to get.See Working with whitespace in the DOM for more information.
The items in the collection of nodes are objects, not strings. To get data from node
objects, use their properties. For example, to get the name of the first
childNode, you can use elementNodeReference.childNodes[0].nodeName.
The Document object itself has two children: the Doctype declaration and the
root element, typically referred to as documentElement. In HTML
documents the latter is the html element.
It is important to keep in mind that childNodes includes all child nodes,
including non-element nodes like text and comment.
To get a collection containing only elements, use Element.Children instead.
-FirstChild
-LastChild
-NextSibling
-PreviousSibling
-Element.Children
FirstChild
The read-only firstChild property of the Node interface
returns the node's first child in the tree,
or null if the node has no children.
[Value("firstChild")]
public Node? FirstChild { get; }
Property Value
Remarks
If the node is a Document,
this property returns the first node in the list of its direct children.
NOTE
This property returns any type of node that is the first child of this one.
It may be a Text or a Comment node.
If you want to get the first Element that is a child of another element,
consider using Element.FirstElementChild.
-Element.FirstElementChild
-LastChild
IsConnected
The read-only isConnected property of the Node interface
returns a boolean indicating whether the node is connected
(directly or indirectly) to a Document object.
[Value("isConnected")]
public bool IsConnected { get; }
Property Value
- bool
A boolean value that is
trueif the node is connected to its relevant context object,
andfalseif not.
Remarks
LastChild
The read-only lastChild property of the Node interface
returns the last child of the node, or null if there are no child nodes.
[Value("lastChild")]
public Node? LastChild { get; }
Property Value
Remarks
NOTE
This property returns any type of node that is the last child of this one.
It may be a Text or a Comment node.
If you want to get the last Element that is a child of another element,
consider using Element.LastElementChild.
-FirstChild
-Element.LastElementChild
NextSibling
The read-only nextSibling property of the Node interface
returns the node immediately following the specified one in their
parent's ChildNodes, or returns null
if the specified node is the last child in the parent element.
[Value("nextSibling")]
public Node? NextSibling { get; }
Property Value
Remarks
NOTE
Browsers insert Text nodes into a document to represent whitespace in the source markup.
Therefore a node obtained, for example, usingNode.firstChild
orNode.previousSibling
may refer to a whitespace text node rather than the actual element the author
intended to get.The section Working with whitespace in the DOM
contains more information about this behavior.You can use Element.NextElementSibling to obtain the next element
skipping any whitespace nodes, other between-element text, or comments.To navigate the opposite way through the child nodes list use Node.previousSibling.
-Element.NextElementSibling
-PreviousSibling
NodeName
The read-only nodeName property of Node returns the name of the current node as a string.
[Value("nodeName")]
public string NodeName { get; }
Property Value
- string
A string. Values for the different types of nodes are:
Remarks
NodeType
The read-only nodeType property of a Node interface is an integer
that identifies what the node is. It distinguishes different kind of nodes from each other,
such as Element, Text and Comment.
[Value("nodeType")]
public ushort NodeType { get; }
Property Value
- ushort
An integer which specifies the type of the node. Possible values are:The following constants have been deprecated and are not in use anymore:
Node.ENTITY_REFERENCE_NODE(5),Node.ENTITY_NODE(6), andNode.NOTATION_NODE(12).
Remarks
NodeValue
The nodeValue property of the Node interface returns or sets the value of the current node.
[Value("nodeValue")]
public string? NodeValue { get; set; }
Property Value
- string
A string containing the value of the current node, if any.
For the document itself,nodeValuereturnsnull.
For text, comment, and CDATA nodes,nodeValuereturns the content of the node.
For attribute nodes, the value of the attribute is returned.The following table shows the return values for different types of nodes.Node Value of nodeValue CDATASection Content of the CDATA section Comment Content of the comment Document nullDocumentFragment nullDocumentType nullElement nullNamedNodeMap nullProcessingInstruction Entire content excluding the target Text Content of the text node NOTE
WhennodeValueis defined to benull, setting it has no effect.
Remarks
OwnerDocument
The read-only ownerDocument property of the Node interface
returns the top-level document object of the node.
[Value("ownerDocument")]
public Document? OwnerDocument { get; }
Property Value
- Document
A Document that is the top-level object in which all the
child nodes are created.If this property is used on a node that is itself a document, the value isnull.
Remarks
ParentElement
The read-only parentElement property of Node interface
returns the DOM node's parent Element, or null if the node either has no
parent, or its parent isn't a DOM Element. ParentNode on the other hand returns any kind of parent, regardless of its type.
[Value("parentElement")]
public Element? ParentElement { get; }
Property Value
Remarks
ParentNode
The read-only parentNode property of the Node interface
returns the parent of the specified node in the DOM tree.
[Value("parentNode")]
public Node? ParentNode { get; }
Property Value
- Node
A Node that is the parent of the current node. The parent of an element is
anElementnode, aDocumentnode, or aDocumentFragmentnode.
Remarks
Document and DocumentFragment nodes can never have a parent, soparentNode will always return null.
It also returns null if the node has just been created
and is not yet attached to the tree. ParentElement on the other hand only returns Element nodes.
-FirstChild
-LastChild
-ChildNodes
-NextSibling
-ParentElement
-PreviousSibling
-RemoveChild(Node)
PreviousSibling
The read-only previousSibling property of the Node interface
returns the node immediately preceding the specified one in its parent's
ChildNodes list,
or null if the specified node is the first in that list.
[Value("previousSibling")]
public Node? PreviousSibling { get; }
Property Value
Remarks
NOTE
Browsers insert text nodes into a document to represent whitespace in the source markup.
Therefore a node obtained, for example, usingNode.firstChild
orNode.previousSibling
may refer to a whitespace text node rather than the actual element the author intended to get.See Working with whitespace in the DOM for more information.
You can use
previousElementSibling
to get the previous element node (skipping text nodes and any other non-element nodes).To navigate the opposite way through the child nodes list use Node.nextSibling.
-NextSibling
-Element.PreviousElementSibling
TextContent
The textContent property of the Node
interface represents the text content of the node and its descendants.
[Value("textContent")]
public string? TextContent { get; set; }
Property Value
- string
A string, or
null. Its value depends on the situation:WARNING
SettingtextContenton a node removes all of the node's children
and replaces them with a single text node with the given string value.
Remarks
NOTE
textContentand InnerText are easily confused,
but the two properties are different in important ways.
Methods
AppendChild(Node)
The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node.
[Value("appendChild")]
public Node AppendChild(Node node)
Parameters
nodeNode
Returns
- Node
A Node that is the appended child (
child), except whenchildis a DocumentFragment, in which case the empty DocumentFragment is returned.
Remarks
NOTE
If the given child is a reference to an existing node in the document,
appendChild()moves it from its current position to the new position.
If the given child is a DocumentFragment, the entire contents of the DocumentFragment are moved into the child list of the specified parent node.
appendChild() returns the newly appended node, or if the child is a DocumentFragment, the emptied fragment.
NOTE
Unlike this method, the Element.Append method supports multiple arguments and appending strings. You can prefer using it if your node is an element.
-RemoveChild(Node)
-ReplaceChild(Node, Node)
-InsertBefore(Node, Node?)
-HasChildNodes()
-InsertAdjacentElement(string, Element)
-Element.Append
CloneNode(bool)
The cloneNode() method of the Node interface
returns a duplicate of the node on which this method was called.
Its parameter controls if the subtree contained in a node is also cloned or not.
[Value("cloneNode")]
public Node CloneNode(bool subtree = false)
Parameters
subtreebool
Returns
- Node
The new Node cloned.
The cloned node has no parent and is not part of the document,
until it is added to another node that is part of the document,
using AppendChild(Node) or a similar method.
Remarks
Cloning a node copies all of its attributes and their values,
including intrinsic (inline) listeners. It does not copy event listeners added
using addEventListener() or
those assigned to element properties (e.g., node.onclick = someFunction).
Additionally, for a canvas element, the painted image is not copied.
WARNING
cloneNode()may lead to duplicate element IDs in a document!If the original node has an
idattribute, and the clone
will be placed in the same document, then you should modify the clone's ID to be
unique.Also,
nameattributes may need to be modified,
depending on whether duplicate names are expected.
To clone a node to insert into a different document, use
ImportNode(Node, bool) instead.
CompareDocumentPosition(Node)
The compareDocumentPosition() method of the Node interface
reports the position of its argument node relative to the node on which it is called.
[Value("compareDocumentPosition")]
public ushort CompareDocumentPosition(Node other)
Parameters
otherNode
Returns
- ushort
An integer value representing
otherNode's position relative tonode
as a bitmask combining the
following constant properties of Node:Zero or more bits can be set, depending on which scenarios apply. For example, ifotherNodeis located earlier in the document and
contains the node on whichcompareDocumentPosition()was
called, then both theDOCUMENT_POSITION_CONTAINSandDOCUMENT_POSITION_PRECEDINGbits would be set, producing a value of10(0x0A).
Remarks
Contains(Node?)
The contains() method of the Node interface
returns a boolean value indicating
whether a node is a descendant of a given node, that is the node itself,
one of its direct children (ChildNodes),
one of the children's direct children, and so on.
[Value("contains")]
public bool Contains(Node? other)
Parameters
otherNode
Returns
- bool
A boolean value that is
trueifotherNodeis contained in the node,falseif not.If theotherNodeparameter isnull,contains()always returnsfalse.
Remarks
NOTE
A node is contained inside itself.
GetRootNode(GetRootNodeOptions)
The getRootNode() method of the Node interface
returns the context object's root,
which optionally includes the shadow root if it is available.
[Value("getRootNode")]
public Node GetRootNode(GetRootNodeOptions options = null)
Parameters
optionsGetRootNodeOptions
Returns
- Node
An object inheriting from 'Node'. This will differ in exact form depending
on where you callgetRootNode(); for example:
Remarks
HasChildNodes()
The hasChildNodes() method of the Node interface
returns a boolean value indicating
whether the given Node has child nodes or not.
[Value("hasChildNodes")]
public bool HasChildNodes()
Returns
- bool
A boolean value that is
trueif the node has child nodes, andfalseotherwise.
Remarks
InsertBefore(Node, Node?)
The insertBefore() method of the Node interface
inserts a node before a reference node as a child of a specified parent node.
[Value("insertBefore")]
public Node InsertBefore(Node node, Node? child)
Parameters
Returns
- Node
Returns the added child (unless
newNodeis a DocumentFragment,
in which case the empty DocumentFragment is returned).
Remarks
If the given node already exists in the document,insertBefore() moves it from its current position to the new position.
(That is, it will automatically be removed from its existing parent
before appending it to the specified new parent.)
This means that a node cannot be in two locations of the document simultaneously.
NOTE
The CloneNode(bool) can be used to make a copy
of the node before appending it under the new parent. Note that the copies made withcloneNode()will not be automatically kept in sync.
If the given child is a DocumentFragment, the entire contents of theDocumentFragment are moved into the child list of the specified parent
node.
-RemoveChild(Node)
-ReplaceChild(Node, Node)
-AppendChild(Node)
-HasChildNodes()
-InsertAdjacentElement(string, Element)
-Element.Prepend
-Element.Before
-Element.After
IsDefaultNamespace(string?)
The isDefaultNamespace() method of the Node interface accepts a namespace URI as an argument.
It returns a boolean value that is true if the namespace is the default namespace on the given node and false if not.
[Value("isDefaultNamespace")]
public bool IsDefaultNamespace(string? namespace_)
Parameters
namespace_string
Returns
- bool
A boolean value that holds the return value
trueorfalse, indicating if the parameter is the default namespace, or not.
Remarks
NOTE
The default namespace of an HTML element is always
"". For a SVG element, it is set by thexmlnsattribute.
IsEqualNode(Node?)
The isEqualNode() method of the Node interface tests whether two nodes are equal.
Two nodes are equal when they have the same type, defining characteristics (for
elements, this would be their ID, number of children, and so forth), its attributes
match, and so on. The specific set of data points that must match varies depending on
the types of the nodes.
[Value("isEqualNode")]
public bool IsEqualNode(Node? otherNode)
Parameters
otherNodeNode
Returns
- bool
A boolean value that is
trueif the two nodes are equals, orfalseif not.
IfotherNodeisnull,isEqualNode()always return false.
Remarks
IsSameNode(Node?)
The isSameNode() method of the Node interface
is a legacy alias the for the === strict equality operator.
That is, it tests whether two nodes are the same
(in other words, whether they reference the same object).
[Value("isSameNode")]
public bool IsSameNode(Node? otherNode)
Parameters
otherNodeNode
Returns
- bool
A boolean value that is
trueif both nodes are strictly equal,falseif not.
Remarks
NOTE
There is no need to use
isSameNode(); instead use the===strict equality operator.
LookupNamespaceURI(string?)
The lookupNamespaceURI() method of the Node interface
takes a prefix as parameter and returns the namespace URI associated with it on the given node if found (andnull if not). This method's existence allows Node objects to be passed as a namespace resolver to XPathEvaluator.CreateExpression and XPathEvaluator.Evaluate.
[Value("lookupNamespaceURI")]
public string? LookupNamespaceURI(string? prefix)
Parameters
prefixstring
Returns
- string
A string containing the namespace URI corresponding to the prefix.
Remarks
-LookupPrefix(string?)
-Node.IsDefaultNameSpace
LookupPrefix(string?)
The lookupPrefix() method of the Node interface
returns a string containing the prefix for a given namespace URI, if present,
and null if not.
When multiple prefixes are possible, the first prefix is returned.
[Value("lookupPrefix")]
public string? LookupPrefix(string? namespace_)
Parameters
namespace_string
Returns
- string
A string containing the corresponding prefix, or
nullif none has been found.
Ifnamespaceis null, or the empty string,lookupPrefix()returnsnull.If the node is a DocumentType or a DocumentFragment,lookupPrefix()always returnsnull.
Remarks
Normalize()
The normalize() method of the Node interface puts the specified node
and all of its sub-tree into a normalized form.
In a normalized sub-tree, no text nodes in the sub-tree are empty and there are no adjacent text nodes.
[Value("normalize")]
public GlobalObject.Undefined Normalize()
Returns
- GlobalObject.Undefined
None.
Remarks
-SplitText(ulong), its opposite.
RemoveChild(Node)
The removeChild() method of the Node interface
removes a child node from the DOM and returns the removed node.
[Value("removeChild")]
public Node RemoveChild(Node child)
Parameters
childNode
Returns
- Node
The removed
childnode.
Remarks
NOTE
As long as a reference is kept on the removed child,
it still exists in memory, but is no longer part of the DOM.
It can still be reused later in the code.If the return value of
removeChild()is not stored, and no other reference is kept,
it will be automatically deleted from memory after a short time.
Unlike CloneNode(bool) the return value preserves the EventListener objects associated with it.
-ReplaceChild(Node, Node)
-ParentNode
-Element.Remove
-CloneNode(bool)
ReplaceChild(Node, Node)
The replaceChild() method of the Node interface replaces a child node within the given (parent) node.
[Value("replaceChild")]
public Node ReplaceChild(Node node, Node child)
Parameters
Returns
Remarks
-RemoveChild(Node)
-Element.ReplaceWith