2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/ScriptEventListener.h"
34 #include "bindings/v8/ScriptController.h"
35 #include "bindings/v8/ScriptScope.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/V8AbstractEventListener.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8WindowShell.h"
40 #include "core/dom/Document.h"
41 #include "core/dom/DocumentParser.h"
42 #include "core/events/EventListener.h"
43 #include "core/frame/Frame.h"
47 static const AtomicString& eventParameterName(bool isSVGEvent)
49 DEFINE_STATIC_LOCAL(const AtomicString, eventString, ("event"));
50 DEFINE_STATIC_LOCAL(const AtomicString, evtString, ("evt"));
51 return isSVGEvent ? evtString : eventString;
54 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, const QualifiedName& name, const AtomicString& value)
60 // FIXME: Very strange: we initialize zero-based number with '1'.
61 TextPosition position(OrdinalNumber::fromZeroBasedInt(1), OrdinalNumber::first());
65 if (Frame* frame = node->document().frame()) {
66 isolate = toIsolate(frame);
67 ScriptController& scriptController = frame->script();
68 if (!scriptController.canExecuteScripts(AboutToExecuteScript))
70 position = scriptController.eventHandlerPosition();
71 sourceURL = node->document().url().string();
73 isolate = v8::Isolate::GetCurrent();
76 return V8LazyEventListener::create(name.localName(), eventParameterName(node->isSVGElement()), value, sourceURL, position, node, isolate);
79 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame* frame, const QualifiedName& name, const AtomicString& value)
87 ScriptController& scriptController = frame->script();
88 if (!scriptController.canExecuteScripts(AboutToExecuteScript))
91 TextPosition position = scriptController.eventHandlerPosition();
92 String sourceURL = frame->document()->url().string();
94 return V8LazyEventListener::create(name.localName(), eventParameterName(frame->document()->isSVGDocument()), value, sourceURL, position, 0, toIsolate(frame));
97 String eventListenerHandlerBody(Document* document, EventListener* listener)
99 if (listener->type() != EventListener::JSEventListenerType)
102 v8::HandleScope scope(toIsolate(document));
103 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
104 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
105 v8::Context::Scope contextScope(context);
106 v8::Handle<v8::Value> function = v8Listener->getListenerObject(document);
107 if (function.IsEmpty())
110 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, functionString, function, "");
111 return functionString;
114 ScriptValue eventListenerHandler(Document* document, EventListener* listener)
116 if (listener->type() != EventListener::JSEventListenerType)
117 return ScriptValue();
119 v8::Isolate* isolate = toIsolate(document);
120 v8::HandleScope scope(isolate);
121 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
122 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
123 v8::Context::Scope contextScope(context);
124 v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
125 if (function.IsEmpty())
126 return ScriptValue();
127 return ScriptValue(function, isolate);
130 ScriptState* eventListenerHandlerScriptState(Frame* frame, EventListener* listener)
132 if (listener->type() != EventListener::JSEventListenerType)
134 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
135 v8::HandleScope scope(toIsolate(frame));
136 v8::Handle<v8::Context> v8Context = frame->script().windowShell(v8Listener->world())->context();
137 return ScriptState::forContext(v8Context);
140 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber)
142 if (listener->type() != EventListener::JSEventListenerType)
145 v8::HandleScope scope(toIsolate(document));
146 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
147 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
148 v8::Context::Scope contextScope(context);
149 v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
150 if (object.IsEmpty() || !object->IsFunction())
153 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(object);
154 v8::Handle<v8::Function> originalFunction = getBoundFunction(function);
155 int scriptIdValue = originalFunction->ScriptId();
156 scriptId = String::number(scriptIdValue);
157 v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
158 if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
159 sourceName = toCoreString(origin.ResourceName().As<v8::String>());
162 lineNumber = originalFunction->GetScriptLineNumber();
166 } // namespace WebCore