kjs_html.cpp 4.13 KB
Newer Older
kocienda's avatar
kocienda committed
1 2
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
weinig@apple.com's avatar
weinig@apple.com committed
3
 *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
kocienda's avatar
kocienda committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
ddkilzer's avatar
ddkilzer committed
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
kocienda's avatar
kocienda committed
18 19
 */

mjs's avatar
mjs committed
20
#include "config.h"
21 22
#include "kjs_html.h"

darin's avatar
darin committed
23
#include "Frame.h"
darin's avatar
darin committed
24
#include "FrameLoader.h"
darin's avatar
darin committed
25
#include "HTMLDocument.h"
weinig@apple.com's avatar
weinig@apple.com committed
26 27
#include "HTMLNames.h"
#include "JSHTMLElement.h"
darin@apple.com's avatar
darin@apple.com committed
28
#include "ScriptController.h"
weinig@apple.com's avatar
weinig@apple.com committed
29
#include "HTMLPlugInElement.h"
30

weinig@apple.com's avatar
weinig@apple.com committed
31
#if USE(JAVASCRIPTCORE_BINDINGS)
andersca@apple.com's avatar
andersca@apple.com committed
32
#include "runtime.h"
weinig@apple.com's avatar
weinig@apple.com committed
33
#endif
34

weinig's avatar
weinig committed
35
using namespace KJS;
36

weinig@apple.com's avatar
weinig@apple.com committed
37 38 39 40
namespace WebCore {

using namespace HTMLNames;

weinig's avatar
weinig committed
41 42
// Runtime object support code for JSHTMLAppletElement, JSHTMLEmbedElement and JSHTMLObjectElement.

weinig@apple.com's avatar
weinig@apple.com committed
43
static JSObject* getRuntimeObject(ExecState* exec, Node* node)
weinig@apple.com's avatar
weinig@apple.com committed
44 45 46 47 48 49 50 51 52
{
    if (!node)
        return 0;

#if USE(JAVASCRIPTCORE_BINDINGS)
    if (node->hasTagName(objectTag) || node->hasTagName(embedTag) || node->hasTagName(appletTag)) {
        HTMLPlugInElement* plugInElement = static_cast<HTMLPlugInElement*>(node);
        if (plugInElement->getInstance() && plugInElement->getInstance()->rootObject())
            // The instance is owned by the PlugIn element.
53
            return KJS::Bindings::Instance::createRuntimeObject(plugInElement->getInstance());
weinig@apple.com's avatar
weinig@apple.com committed
54 55 56 57 58 59 60
    }
#endif

    // If we don't have a runtime object return 0.
    return 0;
}

darin@apple.com's avatar
darin@apple.com committed
61
JSValue* runtimeObjectGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
weinig's avatar
weinig committed
62 63 64 65 66 67
{
    JSHTMLElement* thisObj = static_cast<JSHTMLElement*>(slot.slotBase());
    HTMLElement* element = static_cast<HTMLElement*>(thisObj->impl());
    return getRuntimeObject(exec, element);
}

darin@apple.com's avatar
darin@apple.com committed
68
JSValue* runtimeObjectPropertyGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
weinig's avatar
weinig committed
69 70 71
{
    JSHTMLElement* thisObj = static_cast<JSHTMLElement*>(slot.slotBase());
    HTMLElement* element = static_cast<HTMLElement*>(thisObj->impl());
darin@apple.com's avatar
darin@apple.com committed
72 73 74 75
    JSObject* runtimeObject = getRuntimeObject(exec, element);
    if (!runtimeObject)
        return jsUndefined();
    return runtimeObject->get(exec, propertyName);
weinig's avatar
weinig committed
76 77
}

darin@apple.com's avatar
darin@apple.com committed
78
bool runtimeObjectCustomGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, JSHTMLElement* originalObj, HTMLElement* thisImp)
weinig's avatar
weinig committed
79
{
darin@apple.com's avatar
darin@apple.com committed
80 81 82 83 84 85 86
    JSObject* runtimeObject = getRuntimeObject(exec, thisImp);
    if (!runtimeObject)
        return false;
    if (!runtimeObject->hasProperty(exec, propertyName))
        return false;
    slot.setCustom(originalObj, runtimeObjectPropertyGetter);
    return true;
weinig's avatar
weinig committed
87 88
}

darin@apple.com's avatar
darin@apple.com committed
89
bool runtimeObjectCustomPut(ExecState* exec, const Identifier& propertyName, JSValue* value, HTMLElement* thisImp)
weinig's avatar
weinig committed
90
{
darin@apple.com's avatar
darin@apple.com committed
91 92 93 94 95 96 97
    JSObject* runtimeObject = getRuntimeObject(exec, thisImp);
    if (!runtimeObject)
        return 0;
    if (!runtimeObject->hasProperty(exec, propertyName))
        return false;
    runtimeObject->put(exec, propertyName, value);
    return true;
weinig's avatar
weinig committed
98 99 100 101 102 103 104
}

bool runtimeObjectImplementsCall(HTMLElement* thisImp)
{
    Frame* frame = thisImp->document()->frame();
    if (!frame)
        return false;
darin@apple.com's avatar
darin@apple.com committed
105
    ExecState* exec = frame->script()->globalObject()->globalExec();
darin@apple.com's avatar
darin@apple.com committed
106 107 108 109
    JSObject* runtimeObject = getRuntimeObject(exec, thisImp);
    if (!runtimeObject)
        return false;
    return runtimeObject->implementsCall();
weinig's avatar
weinig committed
110 111
}

darin@apple.com's avatar
darin@apple.com committed
112
JSValue* runtimeObjectCallAsFunction(ExecState* exec, JSObject* thisObj, const ArgList& args, HTMLElement* thisImp)
weinig's avatar
weinig committed
113
{
darin@apple.com's avatar
darin@apple.com committed
114 115 116
    JSObject* runtimeObject = getRuntimeObject(exec, thisImp);
    if (!runtimeObject)
        return jsUndefined();
ggaren@apple.com's avatar
ggaren@apple.com committed
117
    return runtimeObject->callAsFunction(exec, thisObj, args);
weinig's avatar
weinig committed
118 119
}

weinig's avatar
weinig committed
120
} // namespace WebCore