15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2009 Google Inc. All rights reserved.
302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
1702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
32d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8HTMLOptionsCollection.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ExceptionState.h"
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/V8Binding.h"
36d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8HTMLOptionElement.h"
37d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8Node.h"
38d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8NodeList.h"
397757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "core/dom/ExceptionCode.h"
407757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "core/html/HTMLOptionElement.h"
417757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "core/html/HTMLOptionsCollection.h"
42591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "core/html/HTMLSelectElement.h"
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
44c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4619cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)void V8HTMLOptionsCollection::addMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
48a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ExceptionState exceptionState(ExceptionState::ExecutionContext, "add", "HTMLOptionsCollection", info.Holder(), info.GetIsolate());
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!V8HTMLOptionElement::hasInstance(info[0], info.GetIsolate())) {
50a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        exceptionState.throwTypeError("The element provided was not an HTMLOptionElement.");
51a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    } else {
527242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        HTMLOptionsCollection* impl = V8HTMLOptionsCollection::toImpl(info.Holder());
537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        HTMLOptionElement* option = V8HTMLOptionElement::toImpl(v8::Handle<v8::Object>(v8::Handle<v8::Object>::Cast(info[0])));
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
55a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        if (info.Length() < 2) {
5643e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)            impl->add(option, exceptionState);
57a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        } else {
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            int index = toInt32(info[1], exceptionState);
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            if (exceptionState.throwIfNeeded())
6009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                return;
6109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6243e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)            impl->add(option, index, exceptionState);
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        }
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    exceptionState.throwIfNeeded();
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
6919cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)void V8HTMLOptionsCollection::lengthAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
717242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    HTMLOptionsCollection* impl = V8HTMLOptionsCollection::toImpl(info.Holder());
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    double v = value->NumberValue();
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    unsigned newLength = 0;
74a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ExceptionState exceptionState(ExceptionState::SetterContext, "length", "HTMLOptionsCollection", info.Holder(), info.GetIsolate());
75926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!std::isnan(v) && !std::isinf(v)) {
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        if (v < 0.0)
77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(v) + ") is negative. Lengths must be greater than or equal to 0.");
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        else if (v > static_cast<double>(UINT_MAX))
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            newLength = UINT_MAX;
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        else
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            newLength = static_cast<unsigned>(v);
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
8451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (exceptionState.throwIfNeeded())
857757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        return;
867757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
8743e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)    impl->setLength(newLength, exceptionState);
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
90c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
91