kjs_events.cpp 16.4 KB
Newer Older
kocienda's avatar
kocienda committed
1 2
/*
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
3
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007 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
 */
darin's avatar
darin committed
19

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

darin's avatar
darin committed
23
#include "CString.h"
weinig's avatar
weinig committed
24
#include "Chrome.h"
weinig's avatar
 
weinig committed
25 26
#include "Clipboard.h"
#include "ClipboardEvent.h"
weinig's avatar
weinig committed
27
#include "DOMWindow.h"
darin's avatar
darin committed
28
#include "Document.h"
weinig's avatar
 
weinig committed
29
#include "Event.h"
darin's avatar
darin committed
30 31
#include "EventNames.h"
#include "Frame.h"
darin's avatar
darin committed
32
#include "FrameLoader.h"
weinig's avatar
 
weinig committed
33 34
#include "HTMLImageElement.h"
#include "HTMLNames.h"
ggaren's avatar
ggaren committed
35
#include "JSEvent.h"
weinig's avatar
weinig committed
36
#include "JSEventTargetNode.h"
andersca's avatar
andersca committed
37
#include "Page.h"
darin's avatar
darin committed
38 39
#include "kjs_proxy.h"
#include "kjs_window.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
40 41
#include <kjs/array_object.h>
#include <kjs/function_object.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

69
    KJS::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

ggaren@apple.com's avatar
ggaren@apple.com committed
83 84
    JSGlobalObject* globalObject = proxy->globalObject();
    ExecState* exec = globalObject->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
        window->setCurrentEvent(event);
weinig's avatar
weinig committed
101

darin's avatar
darin committed
102
        JSValue* retval;
andersca's avatar
andersca committed
103
        if (handleEventFunc) {
ggaren@apple.com's avatar
ggaren@apple.com committed
104
            globalObject->startTimeoutCheck();
darin's avatar
darin committed
105
            retval = handleEventFunc->call(exec, listener, args);
andersca's avatar
andersca committed
106
        } else {
darin's avatar
darin committed
107 108 109 110
            JSObject* thisObj;
            if (isWindowEvent)
                thisObj = window;
            else
darin's avatar
darin committed
111
                thisObj = static_cast<JSObject*>(toJS(exec, event->currentTarget()));
ggaren@apple.com's avatar
ggaren@apple.com committed
112
            globalObject->startTimeoutCheck();
darin's avatar
darin committed
113 114
            retval = listener->call(exec, thisObj, args);
        }
ggaren@apple.com's avatar
ggaren@apple.com committed
115
        globalObject->stopTimeoutCheck();
darin's avatar
darin committed
116 117 118 119 120

        window->setCurrentEvent(0);

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

darin's avatar
darin committed
139
        Document::updateDocumentsRendering();
darin's avatar
darin committed
140 141
        deref();
    }
kocienda's avatar
kocienda committed
142 143
}

darin's avatar
darin committed
144
bool JSAbstractEventListener::isHTMLEventListener() const
kocienda's avatar
kocienda committed
145
{
weinig's avatar
weinig committed
146
    return m_html;
kocienda's avatar
kocienda committed
147 148
}

mjs's avatar
mjs committed
149 150
// -------------------------------------------------------------------------

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

JSUnprotectedEventListener::~JSUnprotectedEventListener()
{
weinig's avatar
weinig committed
165
    if (m_listener && m_win) {
166
        KJS::Window::UnprotectedListenersMap& listeners = isHTMLEventListener()
weinig's avatar
weinig committed
167 168
            ? m_win->jsUnprotectedHTMLEventListeners() : m_win->jsUnprotectedEventListeners();
        listeners.remove(m_listener);
darin's avatar
darin committed
169
    }
mjs's avatar
mjs committed
170 171
}

darin's avatar
darin committed
172
JSObject* JSUnprotectedEventListener::listenerObj() const
weinig's avatar
weinig committed
173 174
{
    return m_listener;
mjs's avatar
mjs committed
175 176
}

177
KJS::Window* JSUnprotectedEventListener::windowObj() const
mjs's avatar
mjs committed
178
{
weinig's avatar
weinig committed
179
    return m_win;
mjs's avatar
mjs committed
180 181
}

sullivan's avatar
sullivan committed
182 183
void JSUnprotectedEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
184
    m_win = 0;
sullivan's avatar
sullivan committed
185 186
}

187
void JSUnprotectedEventListener::mark()
mjs's avatar
mjs committed
188
{
189 190
    if (m_listener && !m_listener->marked())
        m_listener->mark();
mjs's avatar
mjs committed
191 192
}

weinig's avatar
weinig committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#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
211 212
// -------------------------------------------------------------------------

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

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

darin's avatar
darin committed
240
JSObject* JSEventListener::listenerObj() const
weinig's avatar
weinig committed
241 242
{
    return m_listener;
mjs's avatar
mjs committed
243 244
}

245
KJS::Window* JSEventListener::windowObj() const
mjs's avatar
mjs committed
246
{
weinig's avatar
weinig committed
247
    return m_win;
mjs's avatar
mjs committed
248 249
}

sullivan's avatar
sullivan committed
250 251
void JSEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
252
    m_win = 0;
sullivan's avatar
sullivan committed
253 254
}

mjs's avatar
mjs committed
255 256
// -------------------------------------------------------------------------

257
JSLazyEventListener::JSLazyEventListener(const String& functionName, const String& code, KJS::Window* win, Node* node, int lineNumber)
weinig's avatar
weinig committed
258 259 260 261 262 263
    : JSEventListener(0, win, true)
    , m_functionName(functionName)
    , m_code(code)
    , m_parsed(false)
    , m_lineNumber(lineNumber)
    , m_originalNode(node)
mjs's avatar
mjs committed
264
{
darin's avatar
darin committed
265
    // We don't retain the original node because we assume it
mjs's avatar
mjs committed
266 267 268 269
    // 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
270 271
}

darin's avatar
darin committed
272
JSObject* JSLazyEventListener::listenerObj() const
mjs's avatar
mjs committed
273
{
darin's avatar
darin committed
274
    parseCode();
weinig's avatar
weinig committed
275
    return m_listener;
mjs's avatar
mjs committed
276 277
}

darin's avatar
darin committed
278
JSValue* JSLazyEventListener::eventParameterName() const
eseidel's avatar
eseidel committed
279 280 281 282 283
{
    static ProtectedPtr<JSValue> eventString = jsString("event");
    return eventString.get();
}

mjs's avatar
mjs committed
284 285
void JSLazyEventListener::parseCode() const
{
weinig's avatar
weinig committed
286
    if (m_parsed)
darin's avatar
darin committed
287
        return;
weinig's avatar
weinig committed
288
    m_parsed = true;
darin's avatar
darin committed
289

weinig's avatar
weinig committed
290
    Frame* frame = windowObj()->impl()->frame();
weinig's avatar
weinig committed
291
    KJSProxy* proxy = 0;
mjs's avatar
mjs committed
292
    if (frame)
darin's avatar
darin committed
293
        proxy = frame->scriptProxy();
mjs's avatar
mjs committed
294 295

    if (proxy) {
ggaren@apple.com's avatar
ggaren@apple.com committed
296
        ExecState* exec = proxy->globalObject()->globalExec();
darin's avatar
darin committed
297 298

        JSLock lock;
ggaren@apple.com's avatar
ggaren@apple.com committed
299
        JSObject* constr = proxy->globalObject()->functionConstructor();
darin's avatar
darin committed
300 301
        List args;

weinig@apple.com's avatar
weinig@apple.com committed
302
        UString sourceURL(frame->loader()->url().string());
darin's avatar
darin committed
303
        args.append(eventParameterName());
weinig's avatar
weinig committed
304 305
        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
306

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

darin's avatar
darin committed
309 310 311 312
        if (exec->hadException()) {
            exec->clearException();

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

weinig's avatar
weinig committed
319
            JSValue* thisObj = toJS(exec, m_originalNode);
darin's avatar
darin committed
320
            if (thisObj->isObject()) {
weinig's avatar
weinig committed
321
                static_cast<JSEventTargetNode*>(thisObj)->pushEventHandlerScope(exec, scope);
mjs's avatar
mjs committed
322
                listenerAsFunction->setScope(scope);
darin's avatar
darin committed
323
            }
mjs's avatar
mjs committed
324
        }
mjs's avatar
mjs committed
325 326 327
    }

    // no more need to keep the unparsed code around
darin's avatar
darin committed
328
    m_functionName = String();
weinig's avatar
weinig committed
329
    m_code = String();
darin's avatar
darin committed
330

weinig's avatar
weinig committed
331
    if (m_listener) {
332
        KJS::Window::ListenersMap& listeners = isHTMLEventListener()
mjs's avatar
mjs committed
333
            ? windowObj()->jsHTMLEventListeners() : windowObj()->jsEventListeners();
weinig's avatar
weinig committed
334
        listeners.set(m_listener, const_cast<JSLazyEventListener*>(this));
darin's avatar
darin committed
335
    }
mjs's avatar
mjs committed
336 337
}

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

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

weinig@apple.com's avatar
weinig@apple.com committed
349
const ClassInfo JSClipboard::info = { "Clipboard", 0, &JSClipboardTable };
trey's avatar
trey committed
350

weinig's avatar
weinig committed
351 352
/* Source for JSClipboardTable. Use "make hashtables" to regenerate.
@begin JSClipboardTable 3
weinig@apple.com's avatar
weinig@apple.com committed
353 354 355
  dropEffect    WebCore::JSClipboard::DropEffect                           DontDelete
  effectAllowed WebCore::JSClipboard::EffectAllowed                        DontDelete
  types         WebCore::JSClipboard::Types                                DontDelete|ReadOnly
trey's avatar
trey committed
356
@end
weinig's avatar
weinig committed
357
@begin JSClipboardPrototypeTable 4
weinig@apple.com's avatar
weinig@apple.com committed
358 359 360 361
  clearData     WebCore::jsClipboardPrototypeFunctionClearData    DontDelete|Function 0
  getData       WebCore::jsClipboardPrototypeFunctionGetData      DontDelete|Function 1
  setData       WebCore::jsClipboardPrototypeFunctionSetData      DontDelete|Function 2
  setDragImage  WebCore::jsClipboardPrototypeFunctionSetDragImage DontDelete|Function 3
trey's avatar
trey committed
362 363 364
@end
*/

weinig's avatar
weinig committed
365
KJS_DEFINE_PROTOTYPE(JSClipboardPrototype)
weinig@apple.com's avatar
weinig@apple.com committed
366
KJS_IMPLEMENT_PROTOTYPE("Clipboard", JSClipboardPrototype)
trey's avatar
trey committed
367

weinig@apple.com's avatar
weinig@apple.com committed
368 369 370
JSClipboard::JSClipboard(JSObject* prototype, Clipboard* clipboard)
    : DOMObject(prototype)
    , m_impl(clipboard)
trey's avatar
trey committed
371
{
trey's avatar
trey committed
372 373
}

weinig's avatar
weinig committed
374
JSClipboard::~JSClipboard()
trey's avatar
trey committed
375
{
weinig's avatar
weinig committed
376
    ScriptInterpreter::forgetDOMObject(m_impl.get());
trey's avatar
trey committed
377 378
}

weinig's avatar
weinig committed
379
bool JSClipboard::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
trey's avatar
trey committed
380
{
weinig's avatar
weinig committed
381 382 383 384 385
    return getStaticValueSlot<JSClipboard, DOMObject>(exec, &JSClipboardTable, this, propertyName, slot);
}

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

weinig's avatar
weinig committed
412
void JSClipboard::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)
trey's avatar
trey committed
413
{
weinig's avatar
weinig committed
414
    lookupPut<JSClipboard, DOMObject>(exec, propertyName, value, attr, &JSClipboardTable, this );
trey's avatar
trey committed
415 416
}

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

weinig@apple.com's avatar
weinig@apple.com committed
434
JSValue* jsClipboardPrototypeFunctionClearData(ExecState* exec, JSObject* thisObj, const List& args)
trey's avatar
trey committed
435
{
weinig's avatar
weinig committed
436
    if (!thisObj->inherits(&JSClipboard::info))
darin's avatar
darin committed
437
        return throwError(exec, TypeError);
trey's avatar
trey committed
438

weinig's avatar
weinig committed
439
    Clipboard* clipboard = static_cast<JSClipboard*>(thisObj)->impl();
440

weinig@apple.com's avatar
weinig@apple.com committed
441 442 443 444 445 446 447 448 449
    if (args.size() == 0) {
        clipboard->clearAllData();
        return jsUndefined();
    } else if (args.size() == 1) {
        clipboard->clearData(args[0]->toString(exec));
        return jsUndefined();
    } else
        return throwError(exec, SyntaxError, "clearData: Invalid number of arguments");
}
trey's avatar
trey committed
450

weinig@apple.com's avatar
weinig@apple.com committed
451
JSValue* jsClipboardPrototypeFunctionGetData(ExecState* exec, JSObject* thisObj, const List& args)
weinig@apple.com's avatar
weinig@apple.com committed
452 453 454
{
    if (!thisObj->inherits(&JSClipboard::info))
        return throwError(exec, TypeError);
trey's avatar
trey committed
455

weinig@apple.com's avatar
weinig@apple.com committed
456
    Clipboard* clipboard = static_cast<JSClipboard*>(thisObj)->impl();
weinig's avatar
weinig committed
457

weinig@apple.com's avatar
weinig@apple.com committed
458 459 460 461 462 463 464 465 466
    if (args.size() == 1) {
        bool success;
        String result = clipboard->getData(args[0]->toString(exec), success);
        if (success)
            return jsString(result);
        return jsUndefined();
    } else
        return throwError(exec, SyntaxError, "getData: Invalid number of arguments");
}
467

weinig@apple.com's avatar
weinig@apple.com committed
468
JSValue* jsClipboardPrototypeFunctionSetData(ExecState* exec, JSObject* thisObj, const List& args)
weinig@apple.com's avatar
weinig@apple.com committed
469 470 471 472 473 474 475 476 477 478 479
{
    if (!thisObj->inherits(&JSClipboard::info))
        return throwError(exec, TypeError);

    Clipboard* clipboard = static_cast<JSClipboard*>(thisObj)->impl();

    if (args.size() == 2)
        return jsBoolean(clipboard->setData(args[0]->toString(exec), args[1]->toString(exec)));
    return throwError(exec, SyntaxError, "setData: Invalid number of arguments");
}

weinig@apple.com's avatar
weinig@apple.com committed
480
JSValue* jsClipboardPrototypeFunctionSetDragImage(ExecState* exec, JSObject* thisObj, const List& args)
weinig@apple.com's avatar
weinig@apple.com committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
{
    if (!thisObj->inherits(&JSClipboard::info))
        return throwError(exec, TypeError);

    Clipboard* clipboard = static_cast<JSClipboard*>(thisObj)->impl();

    if (!clipboard->isForDragging())
        return jsUndefined();

    if (args.size() != 3)
        return throwError(exec, SyntaxError, "setDragImage: Invalid number of arguments");

    int x = args[1]->toInt32(exec);
    int y = args[2]->toInt32(exec);

    // See if they passed us a node
    Node* node = toNode(args[0]);
    if (!node)
        return throwError(exec, TypeError);

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

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

darin's avatar
darin committed
510
    return jsUndefined();
trey's avatar
trey committed
511 512
}

weinig's avatar
weinig committed
513
JSValue* toJS(ExecState* exec, Clipboard* obj)
weinig's avatar
weinig committed
514
{
weinig@apple.com's avatar
weinig@apple.com committed
515
    return cacheDOMObject<Clipboard, JSClipboard, JSClipboardPrototype>(exec, obj);
weinig's avatar
weinig committed
516 517
}

weinig's avatar
weinig committed
518
Clipboard* toClipboard(JSValue* val)
weinig's avatar
weinig committed
519 520 521 522
{
    return val->isObject(&JSClipboard::info) ? static_cast<JSClipboard*>(val)->impl() : 0;
}

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