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", &DOMConstructorWithDocument::s_info, 0, 0 };
37 
JSOptionConstructor(ExecState * exec,Structure * structure,JSDOMGlobalObject * globalObject)38 JSOptionConstructor::JSOptionConstructor(ExecState* exec, Structure* structure, JSDOMGlobalObject* globalObject)
39     : DOMConstructorWithDocument(structure, globalObject)
40 {
41     ASSERT(inherits(&s_info));
42     putDirect(exec->globalData(), exec->propertyNames().prototype, JSHTMLOptionElementPrototype::self(exec, globalObject), None);
43     putDirect(exec->globalData(), exec->propertyNames().length, jsNumber(4), ReadOnly | DontDelete | DontEnum);
44 }
45 
constructHTMLOptionElement(ExecState * exec)46 static EncodedJSValue JSC_HOST_CALL constructHTMLOptionElement(ExecState* exec)
47 {
48     JSOptionConstructor* jsConstructor = static_cast<JSOptionConstructor*>(exec->callee());
49     Document* document = jsConstructor->document();
50     if (!document)
51         return throwVMError(exec, createReferenceError(exec, "Option constructor associated document is unavailable"));
52 
53     String data;
54     if ((exec->argumentCount() >= 1) && !exec->argument(0).isUndefined())
55         data = ustringToString(exec->argument(0).toString(exec));
56 
57     String value;
58     if ((exec->argumentCount() >= 2)  && !exec->argument(1).isUndefined())
59         value = ustringToString(exec->argument(1).toString(exec));
60     bool defaultSelected = (exec->argumentCount() >= 3) && exec->argument(2).toBoolean(exec);
61     bool selected = (exec->argumentCount() >= 4) && exec->argument(3).toBoolean(exec);
62 
63     ExceptionCode ec = 0;
64     RefPtr<HTMLOptionElement> element = HTMLOptionElement::createForJSConstructor(document, data, value, defaultSelected, selected, ec);
65     if (ec) {
66         setDOMException(exec, ec);
67         return JSValue::encode(JSValue());
68     }
69 
70     return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(), element.release())));
71 }
72 
getConstructData(ConstructData & constructData)73 ConstructType JSOptionConstructor::getConstructData(ConstructData& constructData)
74 {
75     constructData.native.function = constructHTMLOptionElement;
76     return ConstructTypeHost;
77 }
78 
79 
80 } // namespace WebCore
81