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 "V8HTMLOptionsCollection.h"
33
34#include "HTMLOptionsCollection.h"
35#include "HTMLOptionElement.h"
36#include "ExceptionCode.h"
37
38#include "V8Binding.h"
39#include "V8Collection.h"
40#include "V8HTMLOptionElement.h"
41#include "V8HTMLSelectElementCustom.h"
42#include "V8Node.h"
43#include "V8Proxy.h"
44
45namespace WebCore {
46
47v8::Handle<v8::Value> V8HTMLOptionsCollection::removeCallback(const v8::Arguments& args)
48{
49    INC_STATS("DOM.HTMLOptionsCollection.remove()");
50    HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(args.Holder());
51    HTMLSelectElement* base = static_cast<HTMLSelectElement*>(imp->base());
52    return removeElement(base, args);
53}
54
55v8::Handle<v8::Value> V8HTMLOptionsCollection::addCallback(const v8::Arguments& args)
56{
57    INC_STATS("DOM.HTMLOptionsCollection.add()");
58    if (!V8HTMLOptionElement::HasInstance(args[0])) {
59        V8Proxy::setDOMException(TYPE_MISMATCH_ERR);
60        return v8::Undefined();
61    }
62    HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(args.Holder());
63    HTMLOptionElement* option = V8HTMLOptionElement::toNative(v8::Handle<v8::Object>(v8::Handle<v8::Object>::Cast(args[0])));
64
65    ExceptionCode ec = 0;
66    if (args.Length() < 2)
67        imp->add(option, ec);
68    else {
69        bool ok;
70        v8::TryCatch try_catch;
71        int index = toInt32(args[1], ok);
72
73        if (try_catch.HasCaught())
74            return v8::Undefined();
75
76        if (!ok)
77            ec = TYPE_MISMATCH_ERR;
78        else
79            imp->add(option, index, ec);
80    }
81
82    if (ec != 0)
83        V8Proxy::setDOMException(ec);
84
85    return v8::Undefined();
86}
87
88v8::Handle<v8::Value> V8HTMLOptionsCollection::lengthAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
89{
90    INC_STATS("DOM.HTMLOptionsCollection.length._get");
91    HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(info.Holder());
92    int v = imp->length();
93    return v8::Integer::New(v);
94}
95
96void V8HTMLOptionsCollection::lengthAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
97{
98    INC_STATS("DOM.HTMLOptionsCollection.length._set");
99    HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(info.Holder());
100    double v = value->NumberValue();
101    unsigned newLength = 0;
102    ExceptionCode ec = 0;
103    if (!isnan(v) && !isinf(v)) {
104        if (v < 0.0)
105            ec = INDEX_SIZE_ERR;
106        else if (v > static_cast<double>(UINT_MAX))
107            newLength = UINT_MAX;
108        else
109            newLength = static_cast<unsigned>(v);
110    }
111    if (!ec)
112        imp->setLength(value->Uint32Value(), ec);
113
114    V8Proxy::setDOMException(ec);
115}
116
117v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
118{
119    INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertyGetter");
120    HTMLOptionsCollection* collection = V8HTMLOptionsCollection::toNative(info.Holder());
121
122    RefPtr<Node> result = collection->item(index);
123    if (!result)
124        return notHandledByInterceptor();
125
126    return toV8(result.release());
127}
128
129v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
130{
131    INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertySetter");
132    HTMLOptionsCollection* collection = V8HTMLOptionsCollection::toNative(info.Holder());
133    HTMLSelectElement* base = static_cast<HTMLSelectElement*>(collection->base());
134    return toOptionsCollectionSetter(index, value, base);
135}
136
137} // namespace WebCore
138