1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "config.h"
21 #include "JSImageConstructor.h"
22
23 #include "HTMLImageElement.h"
24 #include "HTMLNames.h"
25 #include "JSHTMLImageElement.h"
26 #include "JSNode.h"
27 #include "ScriptExecutionContext.h"
28
29 using namespace JSC;
30
31 namespace WebCore {
32
33 ASSERT_CLASS_FITS_IN_CELL(JSImageConstructor);
34
35 const ClassInfo JSImageConstructor::s_info = { "ImageConstructor", 0, 0, 0 };
36
JSImageConstructor(ExecState * exec,JSDOMGlobalObject * globalObject)37 JSImageConstructor::JSImageConstructor(ExecState* exec, JSDOMGlobalObject* globalObject)
38 : DOMConstructorWithDocument(JSImageConstructor::createStructure(globalObject->objectPrototype()), globalObject)
39 {
40 putDirect(exec->propertyNames().prototype, JSHTMLImageElementPrototype::self(exec, globalObject), None);
41 }
42
constructImage(ExecState * exec,JSObject * constructor,const ArgList & args)43 static JSObject* constructImage(ExecState* exec, JSObject* constructor, const ArgList& args)
44 {
45 bool widthSet = false;
46 bool heightSet = false;
47 int width = 0;
48 int height = 0;
49 if (args.size() > 0) {
50 widthSet = true;
51 width = args.at(0).toInt32(exec);
52 }
53 if (args.size() > 1) {
54 heightSet = true;
55 height = args.at(1).toInt32(exec);
56 }
57
58 JSImageConstructor* jsConstructor = static_cast<JSImageConstructor*>(constructor);
59 Document* document = jsConstructor->document();
60 if (!document)
61 return throwError(exec, ReferenceError, "Image constructor associated document is unavailable");
62
63 // Calling toJS on the document causes the JS document wrapper to be
64 // added to the window object. This is done to ensure that JSDocument::mark
65 // will be called (which will cause the image element to be marked if necessary).
66 toJS(exec, jsConstructor->globalObject(), document);
67
68 RefPtr<HTMLImageElement> image = new HTMLImageElement(HTMLNames::imgTag, document);
69 if (widthSet)
70 image->setWidth(width);
71 if (heightSet)
72 image->setHeight(height);
73 return asObject(toJS(exec, jsConstructor->globalObject(), image.release()));
74 }
75
getConstructData(ConstructData & constructData)76 ConstructType JSImageConstructor::getConstructData(ConstructData& constructData)
77 {
78 constructData.native.function = constructImage;
79 return ConstructTypeHost;
80 }
81
82 } // namespace WebCore
83