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#include <runtime/Error.h>
29
30using namespace JSC;
31
32namespace WebCore {
33
34ASSERT_CLASS_FITS_IN_CELL(JSImageConstructor);
35
36const ClassInfo JSImageConstructor::s_info = { "ImageConstructor", 0, 0, 0 };
37
38JSImageConstructor::JSImageConstructor(ExecState* exec, JSDOMGlobalObject* globalObject)
39    : DOMConstructorWithDocument(JSImageConstructor::createStructure(globalObject->objectPrototype()), globalObject)
40{
41    putDirect(exec->propertyNames().prototype, JSHTMLImageElementPrototype::self(exec, globalObject), None);
42}
43
44static JSObject* constructImage(ExecState* exec, JSObject* constructor, const ArgList& args)
45{
46    bool widthSet = false;
47    bool heightSet = false;
48    int width = 0;
49    int height = 0;
50    if (args.size() > 0) {
51        widthSet = true;
52        width = args.at(0).toInt32(exec);
53    }
54    if (args.size() > 1) {
55        heightSet = true;
56        height = args.at(1).toInt32(exec);
57    }
58
59    JSImageConstructor* jsConstructor = static_cast<JSImageConstructor*>(constructor);
60    Document* document = jsConstructor->document();
61    if (!document)
62        return throwError(exec, ReferenceError, "Image constructor associated document is unavailable");
63
64    // Calling toJS on the document causes the JS document wrapper to be
65    // added to the window object. This is done to ensure that JSDocument::mark
66    // will be called (which will cause the image element to be marked if necessary).
67    toJS(exec, jsConstructor->globalObject(), document);
68
69    RefPtr<HTMLImageElement> image = new HTMLImageElement(HTMLNames::imgTag, document);
70    if (widthSet)
71        image->setWidth(width);
72    if (heightSet)
73        image->setHeight(height);
74    return asObject(toJS(exec, jsConstructor->globalObject(), image.release()));
75}
76
77ConstructType JSImageConstructor::getConstructData(ConstructData& constructData)
78{
79    constructData.native.function = constructImage;
80    return ConstructTypeHost;
81}
82
83} // namespace WebCore
84