1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "HTMLOptionsCollection.h"
33
34 #include "HTMLOptionElement.h"
35 #include "ExceptionCode.h"
36
37 #include "V8Binding.h"
38 #include "V8Collection.h"
39 #include "V8CustomBinding.h"
40 #include "V8HTMLOptionElement.h"
41 #include "V8HTMLSelectElementCustom.h"
42 #include "V8Proxy.h"
43
44 namespace WebCore {
45
CALLBACK_FUNC_DECL(HTMLOptionsCollectionRemove)46 CALLBACK_FUNC_DECL(HTMLOptionsCollectionRemove)
47 {
48 INC_STATS("DOM.HTMLOptionsCollection.remove()");
49 HTMLOptionsCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, args.Holder());
50 HTMLSelectElement* base = static_cast<HTMLSelectElement*>(imp->base());
51 return removeElement(base, args);
52 }
53
CALLBACK_FUNC_DECL(HTMLOptionsCollectionAdd)54 CALLBACK_FUNC_DECL(HTMLOptionsCollectionAdd)
55 {
56 INC_STATS("DOM.HTMLOptionsCollection.add()");
57 if (!V8HTMLOptionElement::HasInstance(args[0])) {
58 V8Proxy::setDOMException(TYPE_MISMATCH_ERR);
59 return v8::Undefined();
60 }
61 HTMLOptionsCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, args.Holder());
62 HTMLOptionElement* option = V8DOMWrapper::convertDOMWrapperToNode<HTMLOptionElement>(v8::Handle<v8::Object>(v8::Handle<v8::Object>::Cast(args[0])));
63
64 ExceptionCode ec = 0;
65 if (args.Length() < 2)
66 imp->add(option, ec);
67 else {
68 bool ok;
69 v8::TryCatch try_catch;
70 int index = toInt32(args[1], ok);
71
72 if (try_catch.HasCaught())
73 return v8::Undefined();
74
75 if (!ok)
76 ec = TYPE_MISMATCH_ERR;
77 else
78 imp->add(option, index, ec);
79 }
80
81 if (ec != 0)
82 V8Proxy::setDOMException(ec);
83
84 return v8::Undefined();
85 }
86
ACCESSOR_GETTER(HTMLOptionsCollectionLength)87 ACCESSOR_GETTER(HTMLOptionsCollectionLength)
88 {
89 INC_STATS("DOM.HTMLOptionsCollection.length._get");
90 HTMLOptionsCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, info.Holder());
91 int v = imp->length();
92 return v8::Integer::New(v);
93 }
94
ACCESSOR_SETTER(HTMLOptionsCollectionLength)95 ACCESSOR_SETTER(HTMLOptionsCollectionLength)
96 {
97 INC_STATS("DOM.HTMLOptionsCollection.length._set");
98 HTMLOptionsCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, info.Holder());
99 double v = value->NumberValue();
100 unsigned newLength = 0;
101 ExceptionCode ec = 0;
102 if (!isnan(v) && !isinf(v)) {
103 if (v < 0.0)
104 ec = INDEX_SIZE_ERR;
105 else if (v > static_cast<double>(UINT_MAX))
106 newLength = UINT_MAX;
107 else
108 newLength = static_cast<unsigned>(v);
109 }
110 if (!ec)
111 imp->setLength(value->Uint32Value(), ec);
112
113 V8Proxy::setDOMException(ec);
114 }
115
INDEXED_PROPERTY_GETTER(HTMLOptionsCollection)116 INDEXED_PROPERTY_GETTER(HTMLOptionsCollection)
117 {
118 INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertyGetter");
119 HTMLOptionsCollection* collection = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, info.Holder());
120
121 RefPtr<Node> result = collection->item(index);
122 if (!result)
123 return notHandledByInterceptor();
124
125 return V8DOMWrapper::convertNodeToV8Object(result.release());
126 }
127
INDEXED_PROPERTY_SETTER(HTMLOptionsCollection)128 INDEXED_PROPERTY_SETTER(HTMLOptionsCollection)
129 {
130 INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertySetter");
131 HTMLOptionsCollection* collection = V8DOMWrapper::convertToNativeObject<HTMLOptionsCollection>(V8ClassIndex::HTMLOPTIONSCOLLECTION, info.Holder());
132 HTMLSelectElement* base = static_cast<HTMLSelectElement*>(collection->base());
133 return toOptionsCollectionSetter(index, value, base);
134 }
135
136 } // namespace WebCore
137