1 /* 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #include "config.h" 21 #include "JSHTMLOptionsCollection.h" 22 23 #include "ExceptionCode.h" 24 #include "HTMLNames.h" 25 #include "HTMLOptionElement.h" 26 #include "HTMLOptionsCollection.h" 27 #include "HTMLSelectElement.h" 28 #include "JSHTMLOptionElement.h" 29 #include "JSHTMLSelectElement.h" 30 #include "JSHTMLSelectElementCustom.h" 31 32 #include <wtf/MathExtras.h> 33 34 using namespace JSC; 35 36 namespace WebCore { 37 38 JSValue JSHTMLOptionsCollection::length(ExecState*) const 39 { 40 HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); 41 return jsNumber(imp->length()); 42 } 43 44 void JSHTMLOptionsCollection::setLength(ExecState* exec, JSValue value) 45 { 46 HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); 47 ExceptionCode ec = 0; 48 unsigned newLength = 0; 49 double lengthValue = value.toNumber(exec); 50 if (!isnan(lengthValue) && !isinf(lengthValue)) { 51 if (lengthValue < 0.0) 52 ec = INDEX_SIZE_ERR; 53 else if (lengthValue > static_cast<double>(UINT_MAX)) 54 newLength = UINT_MAX; 55 else 56 newLength = static_cast<unsigned>(lengthValue); 57 } 58 if (!ec) 59 imp->setLength(newLength, ec); 60 setDOMException(exec, ec); 61 } 62 63 void JSHTMLOptionsCollection::indexSetter(ExecState* exec, unsigned index, JSValue value) 64 { 65 HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); 66 HTMLSelectElement* base = static_cast<HTMLSelectElement*>(imp->base()); 67 selectIndexSetter(base, exec, index, value); 68 } 69 70 JSValue JSHTMLOptionsCollection::add(ExecState* exec) 71 { 72 HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); 73 HTMLOptionElement* option = toHTMLOptionElement(exec->argument(0)); 74 ExceptionCode ec = 0; 75 if (exec->argumentCount() < 2) 76 imp->add(option, ec); 77 else { 78 bool ok; 79 int index = finiteInt32Value(exec->argument(1), exec, ok); 80 if (exec->hadException()) 81 return jsUndefined(); 82 if (!ok) 83 ec = TYPE_MISMATCH_ERR; 84 else 85 imp->add(option, index, ec); 86 } 87 setDOMException(exec, ec); 88 return jsUndefined(); 89 } 90 91 JSValue JSHTMLOptionsCollection::remove(ExecState* exec) 92 { 93 HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); 94 JSHTMLSelectElement* base = static_cast<JSHTMLSelectElement*>(asObject(toJS(exec, globalObject(), imp->base()))); 95 return base->remove(exec); 96 } 97 98 } 99