1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file has been auto-generated from the Jinja2 template
6 // third_party/blink/renderer/bindings/templates/union_container.cc.tmpl
7 // by the script code_generator_v8.py.
8 // DO NOT MODIFY!
9
10 // clang-format off
11 #include "third_party/blink/renderer/bindings/tests/results/core/node_or_node_list.h"
12
13 #include "base/stl_util.h"
14 #include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
15 #include "third_party/blink/renderer/bindings/core/v8/to_v8_for_core.h"
16 #include "third_party/blink/renderer/bindings/core/v8/v8_node.h"
17 #include "third_party/blink/renderer/bindings/core/v8/v8_node_list.h"
18 #include "third_party/blink/renderer/core/dom/name_node_list.h"
19 #include "third_party/blink/renderer/core/dom/node_list.h"
20 #include "third_party/blink/renderer/core/dom/static_node_list.h"
21 #include "third_party/blink/renderer/core/html/forms/labels_node_list.h"
22
23 namespace blink {
24
NodeOrNodeList()25 NodeOrNodeList::NodeOrNodeList() : type_(SpecificType::kNone) {}
26
GetAsNode() const27 Node* NodeOrNodeList::GetAsNode() const {
28 DCHECK(IsNode());
29 return node_;
30 }
31
SetNode(Node * value)32 void NodeOrNodeList::SetNode(Node* value) {
33 DCHECK(IsNull());
34 node_ = value;
35 type_ = SpecificType::kNode;
36 }
37
FromNode(Node * value)38 NodeOrNodeList NodeOrNodeList::FromNode(Node* value) {
39 NodeOrNodeList container;
40 container.SetNode(value);
41 return container;
42 }
43
GetAsNodeList() const44 NodeList* NodeOrNodeList::GetAsNodeList() const {
45 DCHECK(IsNodeList());
46 return node_list_;
47 }
48
SetNodeList(NodeList * value)49 void NodeOrNodeList::SetNodeList(NodeList* value) {
50 DCHECK(IsNull());
51 node_list_ = value;
52 type_ = SpecificType::kNodeList;
53 }
54
FromNodeList(NodeList * value)55 NodeOrNodeList NodeOrNodeList::FromNodeList(NodeList* value) {
56 NodeOrNodeList container;
57 container.SetNodeList(value);
58 return container;
59 }
60
61 NodeOrNodeList::NodeOrNodeList(const NodeOrNodeList&) = default;
62 NodeOrNodeList::~NodeOrNodeList() = default;
63 NodeOrNodeList& NodeOrNodeList::operator=(const NodeOrNodeList&) = default;
64
Trace(Visitor * visitor) const65 void NodeOrNodeList::Trace(Visitor* visitor) const {
66 visitor->Trace(node_);
67 visitor->Trace(node_list_);
68 }
69
ToImpl(v8::Isolate * isolate,v8::Local<v8::Value> v8_value,NodeOrNodeList & impl,UnionTypeConversionMode conversion_mode,ExceptionState & exception_state)70 void V8NodeOrNodeList::ToImpl(
71 v8::Isolate* isolate,
72 v8::Local<v8::Value> v8_value,
73 NodeOrNodeList& impl,
74 UnionTypeConversionMode conversion_mode,
75 ExceptionState& exception_state) {
76 if (v8_value.IsEmpty())
77 return;
78
79 if (conversion_mode == UnionTypeConversionMode::kNullable && IsUndefinedOrNull(v8_value))
80 return;
81
82 if (V8Node::HasInstance(v8_value, isolate)) {
83 Node* cpp_value = V8Node::ToImpl(v8::Local<v8::Object>::Cast(v8_value));
84 impl.SetNode(cpp_value);
85 return;
86 }
87
88 if (V8NodeList::HasInstance(v8_value, isolate)) {
89 NodeList* cpp_value = V8NodeList::ToImpl(v8::Local<v8::Object>::Cast(v8_value));
90 impl.SetNodeList(cpp_value);
91 return;
92 }
93
94 exception_state.ThrowTypeError("The provided value is not of type '(Node or NodeList)'");
95 }
96
ToV8(const NodeOrNodeList & impl,v8::Local<v8::Object> creationContext,v8::Isolate * isolate)97 v8::Local<v8::Value> ToV8(const NodeOrNodeList& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate) {
98 switch (impl.type_) {
99 case NodeOrNodeList::SpecificType::kNone:
100 return v8::Null(isolate);
101 case NodeOrNodeList::SpecificType::kNode:
102 return ToV8(impl.GetAsNode(), creationContext, isolate);
103 case NodeOrNodeList::SpecificType::kNodeList:
104 return ToV8(impl.GetAsNodeList(), creationContext, isolate);
105 default:
106 NOTREACHED();
107 }
108 return v8::Local<v8::Value>();
109 }
110
NativeValue(v8::Isolate * isolate,v8::Local<v8::Value> value,ExceptionState & exception_state)111 NodeOrNodeList NativeValueTraits<NodeOrNodeList>::NativeValue(
112 v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exception_state) {
113 NodeOrNodeList impl;
114 V8NodeOrNodeList::ToImpl(isolate, value, impl, UnionTypeConversionMode::kNotNullable, exception_state);
115 return impl;
116 }
117
118 } // namespace blink
119
120