ScriptEventListener.cpp [plain text]
#include "config.h"
#include "ScriptEventListener.h"
#include "Attribute.h"
#include "Document.h"
#include "EventListener.h"
#include "JSNode.h"
#include "Frame.h"
#include <runtime/Executable.h>
#include <runtime/JSFunction.h>
#include <runtime/JSLock.h>
using namespace JSC;
namespace WebCore {
static const String& eventParameterName(bool isSVGEvent)
{
DEFINE_STATIC_LOCAL(const String, eventString, ("event"));
DEFINE_STATIC_LOCAL(const String, evtString, ("evt"));
return isSVGEvent ? evtString : eventString;
}
PassRefPtr<JSLazyEventListener> createAttributeEventListener(Node* node, Attribute* attr)
{
ASSERT(node);
ASSERT(attr);
if (attr->isNull())
return 0;
TextPosition position = TextPosition::minimumPosition();
String sourceURL;
if (Frame* frame = node->document()->frame()) {
ScriptController* scriptController = frame->script();
if (!scriptController->canExecuteScripts(AboutToExecuteScript))
return 0;
position = scriptController->eventHandlerPosition();
sourceURL = node->document()->url().string();
}
return JSLazyEventListener::create(attr->localName().string(), eventParameterName(node->isSVGElement()), attr->value(), node, sourceURL, position, 0, mainThreadNormalWorld());
}
PassRefPtr<JSLazyEventListener> createAttributeEventListener(Frame* frame, Attribute* attr)
{
if (!frame)
return 0;
ASSERT(attr);
if (attr->isNull())
return 0;
ScriptController* scriptController = frame->script();
if (!scriptController->canExecuteScripts(AboutToExecuteScript))
return 0;
TextPosition position = scriptController->eventHandlerPosition();
String sourceURL = frame->document()->url().string();
JSObject* wrapper = toJSDOMWindow(frame, mainThreadNormalWorld());
return JSLazyEventListener::create(attr->localName().string(), eventParameterName(frame->document()->isSVGDocument()), attr->value(), 0, sourceURL, position, wrapper, mainThreadNormalWorld());
}
String eventListenerHandlerBody(Document* document, EventListener* eventListener)
{
const JSEventListener* jsListener = JSEventListener::cast(eventListener);
ASSERT(jsListener);
if (!jsListener)
return "";
JSLockHolder lock(jsListener->isolatedWorld()->globalData());
JSC::JSObject* jsFunction = jsListener->jsFunction(document);
if (!jsFunction)
return "";
ScriptState* scriptState = scriptStateFromNode(jsListener->isolatedWorld(), document);
return ustringToString(jsFunction->toString(scriptState)->value(scriptState));
}
bool eventListenerHandlerLocation(Document* document, EventListener* eventListener, String& sourceName, int& lineNumber)
{
const JSEventListener* jsListener = JSEventListener::cast(eventListener);
ASSERT(jsListener);
if (!jsListener)
return false;
JSLockHolder lock(jsListener->isolatedWorld()->globalData());
JSC::JSObject* jsObject = jsListener->jsFunction(document);
if (!jsObject)
return false;
JSC::JSFunction* jsFunction = jsDynamicCast<JSFunction*>(jsObject);
if (!jsFunction || jsFunction->isHostFunction())
return false;
JSC::FunctionExecutable* funcExecutable = jsFunction->jsExecutable();
if (!funcExecutable)
return false;
lineNumber = funcExecutable->lineNo();
sourceName = ustringToString(funcExecutable->sourceURL());
return true;
}
}