Shaka Player Embedded
container_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 
16 
17 #include <cctype>
18 
19 #include "src/js/dom/document.h"
20 #include "src/js/dom/element.h"
21 #include "src/js/js_error.h"
22 #include "src/memory/heap_tracer.h"
23 
24 namespace shaka {
25 namespace js {
26 namespace dom {
27 
28 namespace {
29 
30 RefPtr<Element> ToElement(RefPtr<Node> node) {
31  if (!node->is_element())
32  return nullptr;
33  return static_cast<Element*>(node.get());
34 }
35 
36 } // namespace
37 
39  : Node(type, document) {}
40 
41 // \cond Doxygen_Skip
42 ContainerNode::~ContainerNode() {}
43 // \endcond Doxygen_Skip
44 
45 std::vector<RefPtr<Element>> ContainerNode::GetElementsByTagName(
46  const std::string& name) const {
47  std::vector<RefPtr<Element>> ret;
48  for (auto& child : child_nodes()) {
49  RefPtr<Element> elem = ToElement(child);
50  if (elem) {
51  if (elem->tag_name() == name) {
52  ret.push_back(elem);
53  }
54 
55  auto temp = elem->GetElementsByTagName(name);
56  ret.insert(ret.end(), temp.begin(), temp.end());
57  }
58  }
59 
60  return ret;
61 }
62 
64  const std::string& query) const {
65  // This only supports an identifier (e.g. 'video'). This will search the
66  // recursive children for a node with that tag name.
67  for (char c : query) {
68  if (!std::isalnum(c)) {
69  return JsError::DOMException(NotSupportedError, "Unsupported query");
70  }
71  }
72 
73  std::vector<RefPtr<Element>> elems = GetElementsByTagName(query);
74  return elems.empty() ? nullptr : elems[0];
75 }
76 
77 
79  AddMemberFunction("getElementsByTagName",
81  AddMemberFunction("querySelector", &ContainerNode::QuerySelector);
82 
83  NotImplemented("children");
84  NotImplemented("firstElementChild");
85  NotImplemented("lastElementChild");
86  NotImplemented("childElementCount");
87 
88  NotImplemented("getElementsByTagNameNS");
89  NotImplemented("getElementsByClassName");
90 
91  NotImplemented("prepend");
92  NotImplemented("append");
93  NotImplemented("querySelectorAll");
94 }
95 
96 } // namespace dom
97 } // namespace js
98 } // namespace shaka
ContainerNode(NodeType type, RefPtr< Document > document)
ExceptionCode type
virtual std::vector< RefPtr< Element > > GetElementsByTagName(const std::string &name) const
ExceptionOr< RefPtr< Element > > QuerySelector(const std::string &query) const
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
std::vector< RefPtr< Node > > child_nodes() const
Definition: node.cc:55
std::string name() const