Shaka Player Embedded
element.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_ELEMENT_H_
16 #define SHAKA_EMBEDDED_JS_DOM_ELEMENT_H_
17 
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "shaka/optional.h"
24 
25 namespace shaka {
26 namespace js {
27 namespace dom {
28 class Attr;
29 
34 class Element : public ContainerNode {
35  DECLARE_TYPE_INFO(Element);
36 
37  public:
38  Element(RefPtr<Document> document, const std::string& local_name,
41 
42  void Trace(memory::HeapTracer* tracer) const override;
43 
46  const std::string local_name;
47  const std::string id;
48 
49  std::string tag_name() const;
50 
51  std::string node_name() const override;
52  optional<std::string> NodeValue() const override;
53  optional<std::string> TextContent() const override;
54 
55  bool has_attributes() const {
56  return !attributes_.empty();
57  }
58  optional<std::string> GetAttribute(const std::string& name) const;
59  optional<std::string> GetAttributeNS(const std::string& ns,
60  const std::string& name) const;
61  void SetAttribute(const std::string& key, const std::string& value);
62  void SetAttributeNS(const std::string& ns, const std::string& key,
63  const std::string& value);
64  bool HasAttribute(const std::string& name) const;
65  bool HasAttributeNS(const std::string& ns, const std::string& name) const;
66  virtual void RemoveAttribute(const std::string& attr);
67  void RemoveAttributeNS(const std::string& ns, const std::string& attr);
68 
69  std::vector<RefPtr<Attr>> attributes() const;
70 
71  private:
72  using attr_iter = std::vector<Member<Attr>>::iterator;
73  using const_attr_iter = std::vector<Member<Attr>>::const_iterator;
74 
75  attr_iter FindAttribute(const std::string& name);
76  const_attr_iter FindAttribute(const std::string& name) const {
77  return const_cast<Element*>(this)->FindAttribute(name);
78  }
79 
80  attr_iter FindAttributeNS(const std::string& ns, const std::string& name);
81  const_attr_iter FindAttributeNS(const std::string& ns,
82  const std::string& name) const {
83  return const_cast<Element*>(this)->FindAttributeNS(ns, name);
84  }
85 
86  std::vector<Member<Attr>> attributes_;
87 };
88 
89 class ElementFactory : public BackingObjectFactory<Element, ContainerNode> {
90  public:
92 };
93 
94 } // namespace dom
95 } // namespace js
96 } // namespace shaka
97 
98 #endif // SHAKA_EMBEDDED_JS_DOM_ELEMENT_H_
bool HasAttribute(const std::string &name) const
Definition: element.cc:86
void RemoveAttributeNS(const std::string &ns, const std::string &attr)
Definition: element.cc:129
std::vector< RefPtr< Attr > > attributes() const
Definition: element.cc:155
bool has_attributes() const
Definition: element.h:55
optional< std::string > NodeValue() const override
Definition: element.cc:53
const std::string id
Definition: element.h:47
const optional< std::string > namespace_uri
Definition: element.h:44
bool HasAttributeNS(const std::string &ns, const std::string &name) const
Definition: element.cc:90
virtual void RemoveAttribute(const std::string &attr)
Definition: element.cc:123
optional< std::string > TextContent() const override
Definition: element.cc:57
Element(RefPtr< Document > document, const std::string &local_name, optional< std::string > namespace_uri, optional< std::string > namespace_prefix)
Definition: element.cc:26
void Trace(memory::HeapTracer *tracer) const override
Definition: element.cc:38
RefPtr< Document > document() const
Definition: node.cc:47
optional< std::string > GetAttribute(const std::string &name) const
Definition: element.cc:71
std::string node_name() const override
Definition: element.cc:49
void SetAttributeNS(const std::string &ns, const std::string &key, const std::string &value)
Definition: element.cc:105
void SetAttribute(const std::string &key, const std::string &value)
Definition: element.cc:95
const std::string local_name
Definition: element.h:46
optional< std::string > GetAttributeNS(const std::string &ns, const std::string &name) const
Definition: element.cc:78
std::string tag_name() const
Definition: element.cc:43
const optional< std::string > namespace_prefix
Definition: element.h:45
std::string name() const