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 "ScriptEventListener.h"
34 #include "Attribute.h"
36 #include "EventListener.h"
38 #include "ScriptScope.h"
39 #include "DocumentParser.h"
40 #include "V8AbstractEventListener.h"
41 #include "V8Binding.h"
45 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, Attribute* attr)
52 // FIXME: Very strange: we initialize zero-based number with '1'.
53 TextPosition0 position(WTF::ZeroBasedNumber::fromZeroBasedInt(1), WTF::ZeroBasedNumber::base());
56 if (Frame* frame = node->document()->frame()) {
57 ScriptController* scriptController = frame->script();
58 if (!scriptController->canExecuteScripts(AboutToExecuteScript))
61 position = scriptController->eventHandlerPosition();
62 sourceURL = node->document()->url().string();
65 return V8LazyEventListener::create(attr->localName().string(), node->isSVGElement(), attr->value(), sourceURL, position, WorldContextHandle(UseMainWorld));
68 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame* frame, Attribute* attr)
77 ScriptController* scriptController = frame->script();
78 if (!scriptController->canExecuteScripts(AboutToExecuteScript))
81 TextPosition0 position = scriptController->eventHandlerPosition();
82 String sourceURL = frame->document()->url().string();
83 return V8LazyEventListener::create(attr->localName().string(), frame->document()->isSVGDocument(), attr->value(), sourceURL, position, WorldContextHandle(UseMainWorld));
86 String eventListenerHandlerBody(Document* document, EventListener* listener)
88 if (listener->type() != EventListener::JSEventListenerType)
91 v8::HandleScope scope;
92 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
93 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->worldContext());
94 v8::Context::Scope contextScope(context);
95 v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
96 if (function.IsEmpty())
99 return toWebCoreStringWithNullCheck(function);
102 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, int& lineNumber)
104 if (listener->type() != EventListener::JSEventListenerType)
107 v8::HandleScope scope;
108 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
109 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->worldContext());
110 v8::Context::Scope contextScope(context);
111 v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
112 if (object.IsEmpty() || !object->IsFunction())
115 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(object);
116 v8::ScriptOrigin origin = function->GetScriptOrigin();
117 if (!origin.ResourceName().IsEmpty()) {
118 sourceName = toWebCoreString(origin.ResourceName());
119 lineNumber = function->GetScriptLineNumber() + 1;
125 } // namespace WebCore