Shaka Player Embedded
node.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/js/dom/node.h"
16 
17 #include "src/js/dom/document.h"
18 #include "src/js/dom/element.h"
19 #include "src/js/js_error.h"
20 #include "src/memory/heap_tracer.h"
21 
22 namespace shaka {
23 namespace js {
24 namespace dom {
25 
27  : owner_document_(document), node_type_(type) {
28  DCHECK(!document.empty() || type == DOCUMENT_NODE);
29 }
30 
31 // \cond Doxygen_Skip
32 Node::~Node() {}
33 // \endcond Doxygen_Skip
34 
35 void Node::Trace(memory::HeapTracer* tracer) const {
37  for (auto& child : children_)
38  tracer->Trace(&child);
39  tracer->Trace(&owner_document_);
40  tracer->Trace(&parent_);
41 }
42 
43 bool Node::IsShortLived() const {
44  return true;
45 }
46 
48  return owner_document_;
49 }
50 
52  return parent_;
53 }
54 
55 std::vector<RefPtr<Node>> Node::child_nodes() const {
56  return std::vector<RefPtr<Node>>(children_.begin(), children_.end());
57 }
58 
60  return children_.empty() ? nullptr : children_.front();
61 }
62 
64  return children_.empty() ? nullptr : children_.back();
65 }
66 
68  CHECK(is_element() || node_type_ == DOCUMENT_NODE);
69  CHECK(new_child);
70 
71  if (new_child->parent_node()) {
72  new_child->parent_node()->RemoveChild(new_child);
73  }
74 
75  new_child->parent_ = this;
76  children_.emplace_back(new_child);
77  return new_child;
78 }
79 
81  CHECK(is_element() || node_type_ == DOCUMENT_NODE);
82  CHECK(to_remove);
83  CHECK_EQ(to_remove->parent_node(), this);
84 
85  to_remove->parent_ = nullptr;
86  util::RemoveElement(&children_, to_remove);
87  return to_remove;
88 }
89 
90 
92  AddConstant("ELEMENT_NODE", Node::ELEMENT_NODE);
93  AddConstant("ATTRIBUTE_NODE", Node::ATTRIBUTE_NODE);
94  AddConstant("TEXT_NODE", Node::TEXT_NODE);
95  AddConstant("CDATA_SECTION_NODE", Node::CDATA_SECTION_NODE);
96  AddConstant("ENTITY_REFERENCE_NODE", Node::ENTITY_REFERENCE_NODE);
97  AddConstant("ENTITY_NODE", Node::ENTITY_NODE);
98  AddConstant("PROCESSING_INSTRUCTION_NODE", Node::PROCESSING_INSTRUCTION_NODE);
99  AddConstant("COMMENT_NODE", Node::COMMENT_NODE);
100  AddConstant("DOCUMENT_NODE", Node::DOCUMENT_NODE);
101  AddConstant("DOCUMENT_TYPE_NODE", Node::DOCUMENT_TYPE_NODE);
102  AddConstant("DOCUMENT_FRAGMENT_NODE", Node::DOCUMENT_FRAGMENT_NODE);
103  AddConstant("NOTATION_NODE", Node::NOTATION_NODE);
104 
105  AddConstant("NOTATION_NODE", Node::NOTATION_NODE);
106  AddConstant("DOCUMENT_POSITION_DISCONNECTED",
108  AddConstant("DOCUMENT_POSITION_PRECEDING", Node::DOCUMENT_POSITION_PRECEDING);
109  AddConstant("DOCUMENT_POSITION_FOLLOWING", Node::DOCUMENT_POSITION_FOLLOWING);
110  AddConstant("DOCUMENT_POSITION_CONTAINS", Node::DOCUMENT_POSITION_CONTAINS);
111  AddConstant("DOCUMENT_POSITION_CONTAINED_BY",
113  AddConstant("DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC",
115 
116  AddGenericProperty("ownerDocument", &Node::document);
117  AddGenericProperty("nodeType", &Node::node_type);
118  AddGenericProperty("nodeName", &Node::node_name);
119  AddGenericProperty("parentNode", &Node::parent_node);
120  AddGenericProperty("childNodes", &Node::child_nodes);
121  AddGenericProperty("firstChild", &Node::first_child);
122  AddGenericProperty("lastChild", &Node::last_child);
123  AddGenericProperty("nodeValue", &Node::NodeValue);
124  AddGenericProperty("textContent", &Node::TextContent);
125 
126  AddMemberFunction("appendChild", &Node::AppendChild);
127  AddMemberFunction("removeChild", &Node::RemoveChild);
128 
129  NotImplemented("parentElement");
130  NotImplemented("previousSibling");
131  NotImplemented("nextSibling");
132 
133  NotImplemented("hasChildNodes");
134  NotImplemented("getRootNode");
135  NotImplemented("normalize");
136  NotImplemented("contains");
137 
138  NotImplemented("insertBefore");
139  NotImplemented("replaceChild");
140 
141  NotImplemented("isConnected");
142  NotImplemented("baseURI");
143 
144  NotImplemented("cloneNode");
145  NotImplemented("compareDocumentPosition");
146  NotImplemented("lookupPrefix");
147  NotImplemented("isDefaultNamespace");
148  NotImplemented("isEqualNode");
149  NotImplemented("isSameNode");
150 }
151 
152 } // namespace dom
153 } // namespace js
154 } // namespace shaka
RefPtr< Node > RemoveChild(RefPtr< Node > to_remove)
Definition: node.cc:80
NodeType node_type() const
Definition: node.h:95
RefPtr< Node > first_child() const
Definition: node.cc:59
RefPtr< Node > last_child() const
Definition: node.cc:63
RefPtr< Document > document() const
Definition: node.cc:47
ExceptionCode type
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
bool empty() const
Definition: ref_ptr.h:120
bool IsShortLived() const override
Definition: node.cc:43
bool is_element() const
Definition: node.h:113
void Trace(memory::HeapTracer *tracer) const override
Definition: event_target.cc:31
void RemoveElement(List *list, Elem &&elem)
Definition: utils.h:95
virtual optional< std::string > TextContent() const =0
virtual optional< std::string > NodeValue() const =0
std::vector< RefPtr< Node > > child_nodes() const
Definition: node.cc:55
RefPtr< Node > AppendChild(RefPtr< Node > new_child)
Definition: node.cc:67
Node(NodeType type, RefPtr< Document > document)
Definition: node.cc:26
RefPtr< Node > parent_node() const
Definition: node.cc:51
virtual std::string node_name() const =0
void Trace(memory::HeapTracer *tracer) const override
Definition: node.cc:35