~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/bindings/v8/custom/V8HTMLOptionsCollectionCustom.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "V8NamedNodesCollection.h"
 
43
#include "V8Node.h"
 
44
#include "V8NodeList.h"
 
45
 
 
46
namespace WebCore {
 
47
 
 
48
static v8::Handle<v8::Value> getNamedItems(HTMLOptionsCollection* collection, const AtomicString& name, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
 
49
{
 
50
    Vector<RefPtr<Node> > namedItems;
 
51
    collection->namedItems(name, namedItems);
 
52
 
 
53
    if (!namedItems.size())
 
54
        return v8Undefined();
 
55
 
 
56
    if (namedItems.size() == 1)
 
57
        return toV8(namedItems.at(0).release(), creationContext, isolate);
 
58
 
 
59
    return toV8(V8NamedNodesCollection::create(namedItems), creationContext, isolate);
 
60
}
 
61
 
 
62
v8::Handle<v8::Value> V8HTMLOptionsCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
 
63
{
 
64
    INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
 
65
 
 
66
    if (!info.Holder()->GetRealNamedPropertyInPrototypeChain(name).IsEmpty())
 
67
        return v8Undefined();
 
68
    if (info.Holder()->HasRealNamedCallbackProperty(name))
 
69
        return v8Undefined();
 
70
 
 
71
    HTMLOptionsCollection* imp = V8HTMLOptionsCollection::toNative(info.Holder());
 
72
    return getNamedItems(imp, toWebCoreAtomicString(name), info.Holder(), info.GetIsolate());
 
73
}
 
74
 
 
75
v8::Handle<v8::Value> V8HTMLOptionsCollection::namedItemCallback(const v8::Arguments& args)
 
76
{
 
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());
 
80
 
 
81
    if (result.IsEmpty())
 
82
        return v8::Undefined(args.GetIsolate());
 
83
 
 
84
    return result;
 
85
}
 
86
 
 
87
v8::Handle<v8::Value> V8HTMLOptionsCollection::removeCallback(const v8::Arguments& args)
 
88
{
 
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);
 
93
}
 
94
 
 
95
v8::Handle<v8::Value> V8HTMLOptionsCollection::addCallback(const v8::Arguments& args)
 
96
{
 
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])));
 
102
 
 
103
    ExceptionCode ec = 0;
 
104
    if (args.Length() < 2)
 
105
        imp->add(option, ec);
 
106
    else {
 
107
        bool ok;
 
108
        V8TRYCATCH(int, index, toInt32(args[1], ok));
 
109
        if (!ok)
 
110
            ec = TYPE_MISMATCH_ERR;
 
111
        else
 
112
            imp->add(option, index, ec);
 
113
    }
 
114
 
 
115
    if (ec)
 
116
        return setDOMException(ec, args.GetIsolate());
 
117
 
 
118
    return v8::Undefined();
 
119
}
 
120
 
 
121
void V8HTMLOptionsCollection::lengthAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
 
122
{
 
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)) {
 
129
        if (v < 0.0)
 
130
            ec = INDEX_SIZE_ERR;
 
131
        else if (v > static_cast<double>(UINT_MAX))
 
132
            newLength = UINT_MAX;
 
133
        else
 
134
            newLength = static_cast<unsigned>(v);
 
135
    }
 
136
    if (!ec)
 
137
        imp->setLength(newLength, ec);
 
138
 
 
139
    setDOMException(ec, info.GetIsolate());
 
140
}
 
141
 
 
142
v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
 
143
{
 
144
    INC_STATS("DOM.HTMLOptionsCollection.IndexedPropertyGetter");
 
145
    HTMLOptionsCollection* collection = V8HTMLOptionsCollection::toNative(info.Holder());
 
146
 
 
147
    RefPtr<Node> result = collection->item(index);
 
148
    if (!result)
 
149
        return v8Undefined();
 
150
 
 
151
    return toV8(result.release(), info.Holder(), info.GetIsolate());
 
152
}
 
153
 
 
154
v8::Handle<v8::Value> V8HTMLOptionsCollection::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
 
155
{
 
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());
 
160
}
 
161
 
 
162
} // namespace WebCore