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 static v8::Handle<v8::Function> eventListenerEffectiveFunction(v8::Isolate* isolate, v8::Handle<v8::Object> listenerObject)
99 v8::Handle<v8::Function> function;
100 if (listenerObject->IsFunction()) {
101 function = v8::Handle<v8::Function>::Cast(listenerObject);
102 } else if (listenerObject->IsObject()) {
103 // Try the "handleEvent" method (EventListener interface).
104 v8::Handle<v8::Value> property = listenerObject->Get(v8AtomicString(isolate, "handleEvent"));
105 if (property.IsEmpty() || !property->IsFunction()) {
106 // Fall back to the "constructor" property.
107 property = listenerObject->Get(v8AtomicString(isolate, "constructor"));
109 if (!property.IsEmpty() && property->IsFunction())
110 function = v8::Handle<v8::Function>::Cast(property);
115 String eventListenerHandlerBody(Document* document, EventListener* listener)
117 if (listener->type() != EventListener::JSEventListenerType)
120 v8::HandleScope scope(toIsolate(document));
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> object = v8Listener->getListenerObject(document);
125 if (object.IsEmpty())
127 v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
128 if (function.IsEmpty())
131 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, functionString, function, "");
132 return functionString;
135 ScriptValue eventListenerHandler(Document* document, EventListener* listener)
137 if (listener->type() != EventListener::JSEventListenerType)
138 return ScriptValue();
140 v8::Isolate* isolate = toIsolate(document);
141 v8::HandleScope scope(isolate);
142 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
143 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
144 v8::Context::Scope contextScope(context);
145 v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
146 if (function.IsEmpty())
147 return ScriptValue();
148 return ScriptValue(function, isolate);
151 ScriptState* eventListenerHandlerScriptState(Frame* frame, EventListener* listener)
153 if (listener->type() != EventListener::JSEventListenerType)
155 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
156 v8::HandleScope scope(toIsolate(frame));
157 v8::Handle<v8::Context> v8Context = frame->script().windowShell(v8Listener->world())->context();
158 return ScriptState::forContext(v8Context);
161 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber)
163 if (listener->type() != EventListener::JSEventListenerType)
166 v8::HandleScope scope(toIsolate(document));
167 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
168 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
169 v8::Context::Scope contextScope(context);
170 v8::Local<v8::Object> object = v8Listener->getListenerObject(document);
171 if (object.IsEmpty())
173 v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
174 if (function.IsEmpty())
176 v8::Handle<v8::Function> originalFunction = getBoundFunction(function);
177 int scriptIdValue = originalFunction->ScriptId();
178 scriptId = String::number(scriptIdValue);
179 v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
180 if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
181 sourceName = toCoreString(origin.ResourceName().As<v8::String>());
184 lineNumber = originalFunction->GetScriptLineNumber();
188 } // namespace WebCore