Shaka Player Embedded
node.h
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 #ifndef SHAKA_EMBEDDED_JS_DOM_NODE_H_
16 #define SHAKA_EMBEDDED_JS_DOM_NODE_H_
17 
18 #include <string>
19 #include <vector>
20 
21 #include "shaka/optional.h"
22 #include "src/core/member.h"
23 #include "src/core/ref_ptr.h"
26 #include "src/mapping/enum.h"
27 
28 namespace shaka {
29 namespace js {
30 namespace dom {
31 class Document;
32 class Element;
33 
59 class Node : public events::EventTarget {
60  DECLARE_TYPE_INFO(Node);
61 
62  public:
63  enum NodeType {
66  TEXT_NODE = 3,
68  ENTITY_REFERENCE_NODE = 5, // historical
69  ENTITY_NODE = 6, // historical
75  NOTATION_NODE = 12, // historical
76  };
84  };
85 
87 
88  void Trace(memory::HeapTracer* tracer) const override;
89  bool IsShortLived() const override;
90 
91  // Generic getters.
92  RefPtr<Document> document() const;
93  RefPtr<Node> parent_node() const;
94  std::vector<RefPtr<Node>> child_nodes() const;
95  NodeType node_type() const {
96  return node_type_;
97  }
98  virtual std::string node_name() const = 0;
99  virtual optional<std::string> NodeValue() const = 0;
100  virtual optional<std::string> TextContent() const = 0;
101 
102  RefPtr<Node> first_child() const;
103  RefPtr<Node> last_child() const;
104 
106 
108 
109  // Internal only methods.
110  bool is_document() const {
111  return node_type_ == DOCUMENT_NODE;
112  }
113  bool is_element() const {
114  return node_type_ == ELEMENT_NODE;
115  }
116  bool is_char_data() const {
117  return node_type_ == TEXT_NODE ||
118  node_type_ == PROCESSING_INSTRUCTION_NODE ||
119  node_type_ == COMMENT_NODE;
120  }
121 
122  private:
123  std::vector<Member<Node>> children_;
124  Member<Node> parent_;
125  const Member<Document> owner_document_;
126  const NodeType node_type_;
127 };
128 
129 class NodeFactory : public BackingObjectFactory<Node, events::EventTarget> {
130  public:
131  NodeFactory();
132 };
133 
134 } // namespace dom
135 } // namespace js
136 } // namespace shaka
137 
140 
141 #endif // SHAKA_EMBEDDED_JS_DOM_NODE_H_
RefPtr< Node > RemoveChild(RefPtr< Node > to_remove)
Definition: node.cc:80
NodeType node_type() const
Definition: node.h:95
bool is_document() const
Definition: node.h:110
RefPtr< Node > first_child() const
Definition: node.cc:59
RefPtr< Node > last_child() const
Definition: node.cc:63
CONVERT_ENUM_AS_NUMBER(shaka::js::dom::Node, NodeType)
RefPtr< Document > document() const
Definition: node.cc:47
bool is_char_data() const
Definition: node.h:116
ExceptionCode type
bool IsShortLived() const override
Definition: node.cc:43
bool is_element() const
Definition: node.h:113
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