2
* Copyright (C) 2009 Google Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are
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
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.
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.
32
#include "V8HTMLOptionsCollection.h"
34
#include "HTMLOptionsCollection.h"
35
#include "HTMLOptionElement.h"
36
#include "ExceptionCode.h"
38
#include "V8Binding.h"
39
#include "V8Collection.h"
40
#include "V8HTMLOptionElement.h"
41
#include "V8HTMLSelectElementCustom.h"
42
#include "V8NamedNodesCollection.h"
44
#include "V8NodeList.h"
48
static v8::Handle<v8::Value> getNamedItems(HTMLOptionsCollection* collection, const AtomicString& name, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
50
Vector<RefPtr<Node> > namedItems;
51
collection->namedItems(name, namedItems);
53
if (!namedItems.size())
56
if (namedItems.size() == 1)
57
return toV8(namedItems.at(0).release(), creationContext, isolate);
59
return toV8(V8NamedNodesCollection::create(namedItems), creationContext, isolate);
62
v8::Handle<v8::Value> V8HTMLOptionsCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
64
INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
66
if (!info.Holder()->GetRealNamedPropertyInPrototypeChain(name).IsEmpty())
68
if (info.Holder()->HasRealNamedCallbackProperty(name))
71
HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(info.Holder());
72
return getNamedItems(imp, toWebCoreAtomicString(name), info.Holder(), info.GetIsolate());
75
v8::Handle<v8::Value> V8HTMLOptionsCollection::namedItemCallback(const v8::Arguments& args)
77
INC_STATS("DOM.HTMLCollection.namedItem()");
78
HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(args.Holder());
79
v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]), args.Holder(), args.GetIsolate());
82
return v8::Undefined(args.GetIsolate());
87
v8::Handle<v8::Value> V8HTMLOptionsCollection::removeCallback(const v8::Arguments& args)
89
INC_STATS("DOM.HTMLOptionsCollection.remove()");
90
HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(args.Holder());
91
HTMLSelectElement* base = static_cast<HTMLSelectElement*>(imp->base());
92
return removeElement(base, args);
95
v8::Handle<v8::Value> V8HTMLOptionsCollection::addCallback(const v8::Arguments& args)
97
INC_STATS("DOM.HTMLOptionsCollection.add()");
98
if (!V8HTMLOptionElement::HasInstance(args[0]))
99
return setDOMException(TYPE_MISMATCH_ERR, args.GetIsolate());
100
HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(args.Holder());
101
HTMLOptionElement* option = V8HTMLOptionElement::toNative(v8::Handle<v8::Object>(v8::Handle<v8::Object>::Cast(args[0])));
103
ExceptionCode ec = 0;
104
if (args.Length() < 2)
105
imp->add(option, ec);
108
V8TRYCATCH(int, index, toInt32(args[1], ok));
110
ec = TYPE_MISMATCH_ERR;
112
imp->add(option, index, ec);
116
return setDOMException(ec, args.GetIsolate());
118
return v8::Undefined();
121
void V8HTMLOptionsCollection::lengthAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
123
INC_STATS("DOM.HTMLOptionsCollection.length._set");
124
HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(info.Holder());
125
double v = value->NumberValue();
126
unsigned newLength = 0;
127
ExceptionCode ec = 0;
128
if (!isnan(v) && !isinf(v)) {
131
else if (v > static_cast<double>(UINT_MAX))
132
newLength = UINT_MAX;
134
newLength = static_cast<unsigned>(v);
137
imp->setLength(newLength, ec);
139
setDOMException(ec, info.GetIsolate());
142
v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
144
INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertyGetter");
145
HTMLOptionsCollection* collection = V8HTMLOptionsCollection::toNative(info.Holder());
147
RefPtr<Node> result = collection->item(index);
149
return v8Undefined();
151
return toV8(result.release(), info.Holder(), info.GetIsolate());
154
v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
156
INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertySetter");
157
HTMLOptionsCollection* collection = V8HTMLOptionsCollection::toNative(info.Holder());
158
HTMLSelectElement* base = static_cast<HTMLSelectElement*>(collection->base());
159
return toOptionsCollectionSetter(index, value, base, info.GetIsolate());
162
} // namespace WebCore