kjs_events.cpp 16.2 KB
Newer Older
kocienda's avatar
kocienda committed
1 2 3
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
darin's avatar
darin committed
4
 *  Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
kocienda's avatar
kocienda committed
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  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
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
kocienda's avatar
kocienda committed
19
 */
darin's avatar
darin committed
20

mjs's avatar
mjs committed
21
#include "config.h"
kocienda's avatar
kocienda committed
22
#include "kjs_events.h"
darin's avatar
darin committed
23

darin's avatar
darin committed
24
#include "CString.h"
weinig's avatar
weinig committed
25
#include "Chrome.h"
weinig's avatar
 
weinig committed
26 27
#include "Clipboard.h"
#include "ClipboardEvent.h"
weinig's avatar
weinig committed
28
#include "DOMWindow.h"
darin's avatar
darin committed
29
#include "Document.h"
weinig's avatar
 
weinig committed
30
#include "Event.h"
darin's avatar
darin committed
31 32
#include "EventNames.h"
#include "Frame.h"
darin's avatar
darin committed
33
#include "FrameLoader.h"
weinig's avatar
 
weinig committed
34 35
#include "HTMLImageElement.h"
#include "HTMLNames.h"
ggaren's avatar
ggaren committed
36
#include "JSEvent.h"
weinig's avatar
weinig committed
37
#include "JSEventTargetNode.h"
darin's avatar
darin committed
38
#include "KURL.h"
andersca's avatar
andersca committed
39
#include "Page.h"
darin's avatar
darin committed
40 41
#include "kjs_proxy.h"
#include "kjs_window.h"
kocienda's avatar
kocienda committed
42

darin's avatar
darin committed
43
#include "kjs_events.lut.h"
44

weinig's avatar
weinig committed
45 46 47
namespace WebCore {

using namespace KJS;
darin's avatar
darin committed
48
using namespace EventNames;
49
using namespace HTMLNames;
darin's avatar
darin committed
50

weinig's avatar
weinig committed
51 52
JSAbstractEventListener::JSAbstractEventListener(bool html)
    : m_html(html)
kocienda's avatar
kocienda committed
53 54 55
{
}

darin's avatar
darin committed
56
void JSAbstractEventListener::handleEvent(Event* ele, bool isWindowEvent)
kocienda's avatar
kocienda committed
57
{
58
#ifdef KJS_DEBUGGER
darin's avatar
darin committed
59 60
    if (KJSDebugWin::instance() && KJSDebugWin::instance()->inSession())
        return;
61
#endif
62

weinig's avatar
weinig committed
63
    Event* event = ele;
64

darin's avatar
darin committed
65 66 67
    JSObject* listener = listenerObj();
    if (!listener)
        return;
mjs's avatar
mjs committed
68

darin's avatar
darin committed
69
    Window* window = windowObj();
andrew's avatar
andrew committed
70 71 72 73
    // Null check as clearWindowObj() can clear this and we still get called back by
    // xmlhttprequest objects. See http://bugs.webkit.org/show_bug.cgi?id=13275
    if (!window)
        return;
weinig's avatar
weinig committed
74
    Frame *frame = window->impl()->frame();
mjs's avatar
mjs committed
75
    if (!frame)
darin's avatar
darin committed
76
        return;
darin's avatar
darin committed
77
    KJSProxy* proxy = frame->scriptProxy();
darin's avatar
darin committed
78 79
    if (!proxy)
        return;
darin's avatar
darin committed
80

darin's avatar
darin committed
81
    JSLock lock;
weinig's avatar
weinig committed
82

darin's avatar
darin committed
83 84
    ScriptInterpreter* interpreter = proxy->interpreter();
    ExecState* exec = interpreter->globalExec();
weinig's avatar
weinig committed
85

darin's avatar
darin committed
86 87
    JSValue* handleEventFuncValue = listener->get(exec, "handleEvent");
    JSObject* handleEventFunc = 0;
weinig's avatar
weinig committed
88
    if (handleEventFuncValue->isObject()) {
darin's avatar
darin committed
89 90 91 92
        handleEventFunc = static_cast<JSObject*>(handleEventFuncValue);
        if (!handleEventFunc->implementsCall())
            handleEventFunc = 0;
    }
weinig's avatar
weinig committed
93

darin's avatar
darin committed
94 95
    if (handleEventFunc || listener->implementsCall()) {
        ref();
weinig's avatar
weinig committed
96

darin's avatar
darin committed
97
        List args;
darin's avatar
darin committed
98
        args.append(toJS(exec, event));
weinig's avatar
weinig committed
99

darin's avatar
darin committed
100 101 102 103
        // Set the event we're handling in the Window object
        window->setCurrentEvent(event);
        // ... and in the interpreter
        interpreter->setCurrentEvent(event);
weinig's avatar
weinig committed
104

darin's avatar
darin committed
105
        JSValue* retval;
andersca's avatar
andersca committed
106 107
        if (handleEventFunc) {
            interpreter->startTimeoutCheck();
darin's avatar
darin committed
108
            retval = handleEventFunc->call(exec, listener, args);
andersca's avatar
andersca committed
109
        } else {
darin's avatar
darin committed
110 111 112 113
            JSObject* thisObj;
            if (isWindowEvent)
                thisObj = window;
            else
darin's avatar
darin committed
114
                thisObj = static_cast<JSObject*>(toJS(exec, event->currentTarget()));
andersca's avatar
andersca committed
115
            interpreter->startTimeoutCheck();
darin's avatar
darin committed
116 117
            retval = listener->call(exec, thisObj, args);
        }
andersca's avatar
andersca committed
118
        interpreter->stopTimeoutCheck();
darin's avatar
darin committed
119 120 121 122 123 124

        window->setCurrentEvent(0);
        interpreter->setCurrentEvent(0);

        if (exec->hadException()) {
            JSObject* exception = exec->exception()->toObject(exec);
mjs's avatar
mjs committed
125
            String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
darin's avatar
darin committed
126
            int lineNumber = exception->get(exec, "line")->toInt32(exec);
darin's avatar
darin committed
127
            String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
darin's avatar
darin committed
128
            if (Interpreter::shouldPrintExceptions())
129
                printf("(event handler):%s\n", message.utf8().data());
andersca's avatar
andersca committed
130
            if (Page* page = frame->page())
131
                page->chrome()->addMessageToConsole(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
darin's avatar
darin committed
132 133 134
            exec->clearException();
        } else {
            if (!retval->isUndefinedOrNull() && event->storesResultAsString())
darin's avatar
darin committed
135
                event->storeResult(retval->toString(exec));
weinig's avatar
weinig committed
136
            if (m_html) {
darin's avatar
darin committed
137 138
                bool retvalbool;
                if (retval->getBoolean(retvalbool) && !retvalbool)
darin's avatar
darin committed
139
                    event->preventDefault();
140
            }
darin's avatar
darin committed
141 142
        }

darin's avatar
darin committed
143
        Document::updateDocumentsRendering();
darin's avatar
darin committed
144 145
        deref();
    }
kocienda's avatar
kocienda committed
146 147
}

darin's avatar
darin committed
148
bool JSAbstractEventListener::isHTMLEventListener() const
kocienda's avatar
kocienda committed
149
{
weinig's avatar
weinig committed
150
    return m_html;
kocienda's avatar
kocienda committed
151 152
}

mjs's avatar
mjs committed
153 154
// -------------------------------------------------------------------------

weinig's avatar
weinig committed
155 156 157 158
JSUnprotectedEventListener::JSUnprotectedEventListener(JSObject* listener, Window* win, bool html)
    : JSAbstractEventListener(html)
    , m_listener(listener)
    , m_win(win)
mjs's avatar
mjs committed
159
{
weinig's avatar
weinig committed
160 161 162 163
    if (m_listener) {
        Window::UnprotectedListenersMap& listeners = html
            ? m_win->jsUnprotectedHTMLEventListeners() : m_win->jsUnprotectedEventListeners();
        listeners.set(m_listener, this);
darin's avatar
darin committed
164
    }
mjs's avatar
mjs committed
165 166 167 168
}

JSUnprotectedEventListener::~JSUnprotectedEventListener()
{
weinig's avatar
weinig committed
169
    if (m_listener && m_win) {
darin's avatar
darin committed
170
        Window::UnprotectedListenersMap& listeners = isHTMLEventListener()
weinig's avatar
weinig committed
171 172
            ? m_win->jsUnprotectedHTMLEventListeners() : m_win->jsUnprotectedEventListeners();
        listeners.remove(m_listener);
darin's avatar
darin committed
173
    }
mjs's avatar
mjs committed
174 175
}

darin's avatar
darin committed
176
JSObject* JSUnprotectedEventListener::listenerObj() const
weinig's avatar
weinig committed
177 178
{
    return m_listener;
mjs's avatar
mjs committed
179 180
}

darin's avatar
darin committed
181
Window* JSUnprotectedEventListener::windowObj() const
mjs's avatar
mjs committed
182
{
weinig's avatar
weinig committed
183
    return m_win;
mjs's avatar
mjs committed
184 185
}

sullivan's avatar
sullivan committed
186 187
void JSUnprotectedEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
188
    m_win = 0;
sullivan's avatar
sullivan committed
189 190
}

mjs's avatar
mjs committed
191 192
void JSUnprotectedEventListener::mark()
{
weinig's avatar
weinig committed
193 194
    if (m_listener && !m_listener->marked())
        m_listener->mark();
mjs's avatar
mjs committed
195 196
}

weinig's avatar
weinig committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#ifndef NDEBUG
#ifndef LOG_CHANNEL_PREFIX
#define LOG_CHANNEL_PREFIX Log
#endif
WTFLogChannel LogWebCoreEventListenerLeaks = { 0x00000000, "", WTFLogChannelOn };

struct EventListenerCounter {
    static unsigned count;
    ~EventListenerCounter()
    {
        if (count)
            LOG(WebCoreEventListenerLeaks, "LEAK: %u EventListeners\n", count);
    }
};
unsigned EventListenerCounter::count = 0;
static EventListenerCounter eventListenerCounter;
#endif

mjs's avatar
mjs committed
215 216
// -------------------------------------------------------------------------

weinig's avatar
weinig committed
217 218 219 220
JSEventListener::JSEventListener(JSObject* listener, Window* win, bool html)
    : JSAbstractEventListener(html)
    , m_listener(listener)
    , m_win(win)
mjs's avatar
mjs committed
221
{
weinig's avatar
weinig committed
222 223 224 225
    if (m_listener) {
        Window::ListenersMap& listeners = html
            ? m_win->jsHTMLEventListeners() : m_win->jsEventListeners();
        listeners.set(m_listener, this);
darin's avatar
darin committed
226
    }
weinig's avatar
weinig committed
227 228 229
#ifndef NDEBUG
    ++eventListenerCounter.count;
#endif
mjs's avatar
mjs committed
230 231 232 233
}

JSEventListener::~JSEventListener()
{
weinig's avatar
weinig committed
234
    if (m_listener && m_win) {
darin's avatar
darin committed
235
        Window::ListenersMap& listeners = isHTMLEventListener()
weinig's avatar
weinig committed
236 237
            ? m_win->jsHTMLEventListeners() : m_win->jsEventListeners();
        listeners.remove(m_listener);
darin's avatar
darin committed
238
    }
weinig's avatar
weinig committed
239 240 241
#ifndef NDEBUG
    --eventListenerCounter.count;
#endif
mjs's avatar
mjs committed
242
}
mjs's avatar
mjs committed
243

darin's avatar
darin committed
244
JSObject* JSEventListener::listenerObj() const
weinig's avatar
weinig committed
245 246
{
    return m_listener;
mjs's avatar
mjs committed
247 248
}

darin's avatar
darin committed
249
Window* JSEventListener::windowObj() const
mjs's avatar
mjs committed
250
{
weinig's avatar
weinig committed
251
    return m_win;
mjs's avatar
mjs committed
252 253
}

sullivan's avatar
sullivan committed
254 255
void JSEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
256
    m_win = 0;
sullivan's avatar
sullivan committed
257 258
}

mjs's avatar
mjs committed
259 260
// -------------------------------------------------------------------------

weinig's avatar
weinig committed
261 262 263 264 265 266 267
JSLazyEventListener::JSLazyEventListener(const String& functionName, const String& code, Window* win, Node* node, int lineNumber)
    : JSEventListener(0, win, true)
    , m_functionName(functionName)
    , m_code(code)
    , m_parsed(false)
    , m_lineNumber(lineNumber)
    , m_originalNode(node)
mjs's avatar
mjs committed
268
{
darin's avatar
darin committed
269
    // We don't retain the original node because we assume it
mjs's avatar
mjs committed
270 271 272 273
    // will stay alive as long as this handler object is around
    // and we need to avoid a reference cycle. If JS transfers
    // this handler to another node, parseCode will be called and
    // then originalNode is no longer needed.
mjs's avatar
mjs committed
274 275
}

darin's avatar
darin committed
276
JSObject* JSLazyEventListener::listenerObj() const
mjs's avatar
mjs committed
277
{
darin's avatar
darin committed
278
    parseCode();
weinig's avatar
weinig committed
279
    return m_listener;
mjs's avatar
mjs committed
280 281
}

darin's avatar
darin committed
282
JSValue* JSLazyEventListener::eventParameterName() const
eseidel's avatar
eseidel committed
283 284 285 286 287
{
    static ProtectedPtr<JSValue> eventString = jsString("event");
    return eventString.get();
}

mjs's avatar
mjs committed
288 289
void JSLazyEventListener::parseCode() const
{
weinig's avatar
weinig committed
290
    if (m_parsed)
darin's avatar
darin committed
291
        return;
weinig's avatar
weinig committed
292
    m_parsed = true;
darin's avatar
darin committed
293

weinig's avatar
weinig committed
294
    Frame* frame = windowObj()->impl()->frame();
weinig's avatar
weinig committed
295
    KJSProxy* proxy = 0;
mjs's avatar
mjs committed
296
    if (frame)
darin's avatar
darin committed
297
        proxy = frame->scriptProxy();
mjs's avatar
mjs committed
298 299

    if (proxy) {
darin's avatar
darin committed
300 301 302 303 304 305 306
        ScriptInterpreter* interpreter = proxy->interpreter();
        ExecState* exec = interpreter->globalExec();

        JSLock lock;
        JSObject* constr = interpreter->builtinFunction();
        List args;

darin's avatar
darin committed
307
        UString sourceURL(frame->loader()->url().url());
darin's avatar
darin committed
308
        args.append(eventParameterName());
weinig's avatar
weinig committed
309 310
        args.append(jsString(m_code));
        m_listener = constr->construct(exec, args, m_functionName, sourceURL, m_lineNumber); // FIXME: is globalExec ok ?
darin's avatar
darin committed
311

weinig's avatar
weinig committed
312
        FunctionImp* listenerAsFunction = static_cast<FunctionImp*>(m_listener.get());
mjs's avatar
mjs committed
313

darin's avatar
darin committed
314 315 316 317
        if (exec->hadException()) {
            exec->clearException();

            // failed to parse, so let's just make this listener a no-op
weinig's avatar
weinig committed
318 319
            m_listener = 0;
        } else if (m_originalNode) {
darin's avatar
darin committed
320
            // Add the event's home element to the scope
darin's avatar
darin committed
321
            // (and the document, and the form - see JSHTMLElement::eventHandlerScope)
mjs's avatar
mjs committed
322
            ScopeChain scope = listenerAsFunction->scope();
darin's avatar
darin committed
323

weinig's avatar
weinig committed
324
            JSValue* thisObj = toJS(exec, m_originalNode);
darin's avatar
darin committed
325
            if (thisObj->isObject()) {
weinig's avatar
weinig committed
326
                static_cast<JSEventTargetNode*>(thisObj)->pushEventHandlerScope(exec, scope);
mjs's avatar
mjs committed
327
                listenerAsFunction->setScope(scope);
darin's avatar
darin committed
328
            }
mjs's avatar
mjs committed
329
        }
mjs's avatar
mjs committed
330 331 332
    }

    // no more need to keep the unparsed code around
darin's avatar
darin committed
333
    m_functionName = String();
weinig's avatar
weinig committed
334
    m_code = String();
darin's avatar
darin committed
335

weinig's avatar
weinig committed
336
    if (m_listener) {
darin's avatar
darin committed
337
        Window::ListenersMap& listeners = isHTMLEventListener()
mjs's avatar
mjs committed
338
            ? windowObj()->jsHTMLEventListeners() : windowObj()->jsEventListeners();
weinig's avatar
weinig committed
339
        listeners.set(m_listener, const_cast<JSLazyEventListener*>(this));
darin's avatar
darin committed
340
    }
mjs's avatar
mjs committed
341 342
}

darin's avatar
darin committed
343
JSValue* getNodeEventListener(EventTargetNode* n, const AtomicString& eventType)
kocienda's avatar
kocienda committed
344
{
weinig's avatar
weinig committed
345
    if (JSAbstractEventListener* listener = static_cast<JSAbstractEventListener*>(n->getHTMLEventListener(eventType))) {
darin's avatar
darin committed
346 347
        if (JSValue* obj = listener->listenerObj())
            return obj;
weinig's avatar
weinig committed
348
    }
darin's avatar
darin committed
349
    return jsNull();
kocienda's avatar
kocienda committed
350 351 352 353
}

// -------------------------------------------------------------------------

weinig's avatar
weinig committed
354
const ClassInfo JSClipboard::info = { "Clipboard", 0, &JSClipboardTable, 0 };
trey's avatar
trey committed
355

weinig's avatar
weinig committed
356 357
/* Source for JSClipboardTable. Use "make hashtables" to regenerate.
@begin JSClipboardTable 3
weinig's avatar
weinig committed
358 359 360
  dropEffect    WebCore::JSClipboard::DropEffect   DontDelete
  effectAllowed WebCore::JSClipboard::EffectAllowed        DontDelete
  types         WebCore::JSClipboard::Types        DontDelete|ReadOnly
trey's avatar
trey committed
361
@end
weinig's avatar
weinig committed
362
@begin JSClipboardPrototypeTable 4
weinig's avatar
weinig committed
363 364 365 366
  clearData     WebCore::JSClipboard::ClearData    DontDelete|Function 0
  getData       WebCore::JSClipboard::GetData      DontDelete|Function 1
  setData       WebCore::JSClipboard::SetData      DontDelete|Function 2
  setDragImage  WebCore::JSClipboard::SetDragImage DontDelete|Function 3
trey's avatar
trey committed
367 368 369
@end
*/

weinig's avatar
weinig committed
370 371 372
KJS_DEFINE_PROTOTYPE(JSClipboardPrototype)
KJS_IMPLEMENT_PROTOTYPE_FUNCTION(JSClipboardPrototypeFunction)
KJS_IMPLEMENT_PROTOTYPE("Clipboard", JSClipboardPrototype, JSClipboardPrototypeFunction)
trey's avatar
trey committed
373

weinig's avatar
weinig committed
374 375
JSClipboard::JSClipboard(ExecState* exec, Clipboard* clipboard)
    : m_impl(clipboard)
trey's avatar
trey committed
376
{
weinig's avatar
weinig committed
377
    setPrototype(JSClipboardPrototype::self(exec));
trey's avatar
trey committed
378 379
}

weinig's avatar
weinig committed
380
JSClipboard::~JSClipboard()
trey's avatar
trey committed
381
{
weinig's avatar
weinig committed
382
    ScriptInterpreter::forgetDOMObject(m_impl.get());
trey's avatar
trey committed
383 384
}

weinig's avatar
weinig committed
385
bool JSClipboard::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
trey's avatar
trey committed
386
{
weinig's avatar
weinig committed
387 388 389 390 391
    return getStaticValueSlot<JSClipboard, DOMObject>(exec, &JSClipboardTable, this, propertyName, slot);
}

JSValue* JSClipboard::getValueProperty(ExecState* exec, int token) const
{
weinig's avatar
weinig committed
392
    Clipboard* clipboard = impl();
trey's avatar
trey committed
393 394
    switch (token) {
        case DropEffect:
ggaren's avatar
ggaren committed
395
            ASSERT(clipboard->isForDragging() || clipboard->dropEffect().isNull());
darin's avatar
darin committed
396
            return jsStringOrUndefined(clipboard->dropEffect());
trey's avatar
trey committed
397
        case EffectAllowed:
ggaren's avatar
ggaren committed
398
            ASSERT(clipboard->isForDragging() || clipboard->effectAllowed().isNull());
darin's avatar
darin committed
399
            return jsStringOrUndefined(clipboard->effectAllowed());
400 401
        case Types:
        {
402 403
            HashSet<String> types = clipboard->types();
            if (types.isEmpty())
weinig's avatar
weinig committed
404
                return jsNull();
405
            else {
406
                List list;
407
                HashSet<String>::const_iterator end = types.end();
weinig's avatar
weinig committed
408
                for (HashSet<String>::const_iterator it = types.begin(); it != end; ++it)
darin's avatar
darin committed
409
                    list.append(jsString(UString(*it)));
darin's avatar
darin committed
410
                return exec->lexicalInterpreter()->builtinArray()->construct(exec, list);
411 412
            }
        }
trey's avatar
trey committed
413
        default:
weinig's avatar
weinig committed
414
            return 0;
trey's avatar
trey committed
415 416 417
    }
}

weinig's avatar
weinig committed
418
void JSClipboard::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)
trey's avatar
trey committed
419
{
weinig's avatar
weinig committed
420
    lookupPut<JSClipboard, DOMObject>(exec, propertyName, value, attr, &JSClipboardTable, this );
trey's avatar
trey committed
421 422
}

weinig's avatar
weinig committed
423
void JSClipboard::putValueProperty(ExecState* exec, int token, JSValue* value, int /*attr*/)
trey's avatar
trey committed
424
{
weinig's avatar
weinig committed
425
    Clipboard* clipboard = impl();
trey's avatar
trey committed
426 427
    switch (token) {
        case DropEffect:
428 429
            // can never set this when not for dragging, thus getting always returns NULL string
            if (clipboard->isForDragging())
darin's avatar
darin committed
430
                clipboard->setDropEffect(value->toString(exec));
trey's avatar
trey committed
431
            break;
trey's avatar
trey committed
432
        case EffectAllowed:
433 434
            // can never set this when not for dragging, thus getting always returns NULL string
            if (clipboard->isForDragging())
darin's avatar
darin committed
435
                clipboard->setEffectAllowed(value->toString(exec));
trey's avatar
trey committed
436 437 438 439
            break;
    }
}

weinig's avatar
weinig committed
440
JSValue* JSClipboardPrototypeFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
trey's avatar
trey committed
441
{
weinig's avatar
weinig committed
442
    if (!thisObj->inherits(&JSClipboard::info))
darin's avatar
darin committed
443
        return throwError(exec, TypeError);
trey's avatar
trey committed
444

weinig's avatar
weinig committed
445
    Clipboard* clipboard = static_cast<JSClipboard*>(thisObj)->impl();
trey's avatar
trey committed
446
    switch (id) {
weinig's avatar
weinig committed
447
        case JSClipboard::ClearData:
trey's avatar
trey committed
448
            if (args.size() == 0) {
weinig's avatar
weinig committed
449
                clipboard->clearAllData();
darin's avatar
darin committed
450
                return jsUndefined();
trey's avatar
trey committed
451
            } else if (args.size() == 1) {
weinig's avatar
weinig committed
452
                clipboard->clearData(args[0]->toString(exec));
darin's avatar
darin committed
453
                return jsUndefined();
454
            } else
darin's avatar
darin committed
455
                return throwError(exec, SyntaxError, "clearData: Invalid number of arguments");
weinig's avatar
weinig committed
456
        case JSClipboard::GetData:
trey's avatar
trey committed
457 458 459
        {
            if (args.size() == 1) {
                bool success;
weinig's avatar
weinig committed
460
                String result = clipboard->getData(args[0]->toString(exec), success);
461
                if (success)
darin's avatar
darin committed
462
                    return jsString(result);
weinig's avatar
weinig committed
463
                return jsUndefined();
464
            } else
darin's avatar
darin committed
465
                return throwError(exec, SyntaxError, "getData: Invalid number of arguments");
trey's avatar
trey committed
466
        }
weinig's avatar
weinig committed
467
        case JSClipboard::SetData:
468
            if (args.size() == 2)
weinig's avatar
weinig committed
469 470 471
                return jsBoolean(clipboard->setData(args[0]->toString(exec), args[1]->toString(exec)));
            return throwError(exec, SyntaxError, "setData: Invalid number of arguments");
        case JSClipboard::SetDragImage:
trey's avatar
trey committed
472
        {
weinig's avatar
weinig committed
473
            if (!clipboard->isForDragging())
darin's avatar
darin committed
474
                return jsUndefined();
475

darin's avatar
darin committed
476 477
            if (args.size() != 3)
                return throwError(exec, SyntaxError, "setDragImage: Invalid number of arguments");
trey's avatar
trey committed
478

weinig's avatar
weinig committed
479 480
            int x = args[1]->toInt32(exec);
            int y = args[2]->toInt32(exec);
trey's avatar
trey committed
481 482

            // See if they passed us a node
weinig's avatar
weinig committed
483
            Node* node = toNode(args[0]);
484
            if (!node)
andersca's avatar
andersca committed
485
                return throwError(exec, TypeError);
weinig's avatar
weinig committed
486

487 488 489
            if (!node->isElementNode())
                return throwError(exec, SyntaxError, "setDragImageFromElement: Invalid first argument");

weinig's avatar
weinig committed
490
            if (static_cast<Element*>(node)->hasLocalName(imgTag) &&
491
                !node->inDocument())
weinig's avatar
weinig committed
492
                clipboard->setDragImage(static_cast<HTMLImageElement*>(node)->cachedImage(), IntPoint(x, y));
493
            else
weinig's avatar
weinig committed
494 495
                clipboard->setDragImageElement(node, IntPoint(x, y));

496
            return jsUndefined();
trey's avatar
trey committed
497
        }
trey's avatar
trey committed
498
    }
darin's avatar
darin committed
499
    return jsUndefined();
trey's avatar
trey committed
500 501
}

weinig's avatar
weinig committed
502
JSValue* toJS(ExecState* exec, Clipboard* obj)
weinig's avatar
weinig committed
503
{
weinig's avatar
weinig committed
504
    return cacheDOMObject<Clipboard, JSClipboard>(exec, obj);
weinig's avatar
weinig committed
505 506
}

weinig's avatar
weinig committed
507
Clipboard* toClipboard(JSValue* val)
weinig's avatar
weinig committed
508 509 510 511
{
    return val->isObject(&JSClipboard::info) ? static_cast<JSClipboard*>(val)->impl() : 0;
}

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