Home | History | Annotate | Download | only in js
      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 "JSOptionConstructor.h"
     22 
     23 #include "HTMLNames.h"
     24 #include "HTMLOptionElement.h"
     25 #include "JSHTMLOptionElement.h"
     26 #include "ScriptExecutionContext.h"
     27 #include "Text.h"
     28 #include <runtime/Error.h>
     29 
     30 using namespace JSC;
     31 
     32 namespace WebCore {
     33 
     34 ASSERT_CLASS_FITS_IN_CELL(JSOptionConstructor);
     35 
     36 const ClassInfo JSOptionConstructor::s_info = { "OptionConstructor", 0, 0, 0 };
     37 
     38 JSOptionConstructor::JSOptionConstructor(ExecState* exec, JSDOMGlobalObject* globalObject)
     39     : DOMConstructorWithDocument(JSOptionConstructor::createStructure(globalObject->objectPrototype()), globalObject)
     40 {
     41     putDirect(exec->propertyNames().prototype, JSHTMLOptionElementPrototype::self(exec, globalObject), None);
     42     putDirect(exec->propertyNames().length, jsNumber(exec, 4), ReadOnly|DontDelete|DontEnum);
     43 }
     44 
     45 static JSObject* constructHTMLOptionElement(ExecState* exec, JSObject* constructor, const ArgList& args)
     46 {
     47     JSOptionConstructor* jsConstructor = static_cast<JSOptionConstructor*>(constructor);
     48     Document* document = jsConstructor->document();
     49     if (!document)
     50         return throwError(exec, ReferenceError, "Option constructor associated document is unavailable");
     51 
     52     RefPtr<HTMLOptionElement> element = static_pointer_cast<HTMLOptionElement>(document->createElement(HTMLNames::optionTag, false));
     53 
     54     ExceptionCode ec = 0;
     55     RefPtr<Text> text = document->createTextNode("");
     56     if (!args.at(0).isUndefined())
     57         text->setData(args.at(0).toString(exec), ec);
     58     if (ec == 0)
     59         element->appendChild(text.release(), ec);
     60     if (ec == 0 && !args.at(1).isUndefined())
     61         element->setValue(args.at(1).toString(exec));
     62     if (ec == 0)
     63         element->setDefaultSelected(args.at(2).toBoolean(exec));
     64     if (ec == 0)
     65         element->setSelected(args.at(3).toBoolean(exec));
     66 
     67     if (ec) {
     68         setDOMException(exec, ec);
     69         return 0;
     70     }
     71 
     72     return asObject(toJS(exec, jsConstructor->globalObject(), element.release()));
     73 }
     74 
     75 ConstructType JSOptionConstructor::getConstructData(ConstructData& constructData)
     76 {
     77     constructData.native.function = constructHTMLOptionElement;
     78     return ConstructTypeHost;
     79 }
     80 
     81 
     82 } // namespace WebCore
     83