Bug 101202 - KHTML improperly propegates scripting context in event handlers
Summary: KHTML improperly propegates scripting context in event handlers
Status: RESOLVED FIXED
Alias: None
Product: konqueror
Classification: Applications
Component: khtml (show other bugs)
Version: unspecified
Platform: Compiled Sources Linux
: NOR normal
Target Milestone: ---
Assignee: Konqueror Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-09 23:12 UTC by Jason Keirstead
Modified: 2005-03-11 11:09 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Test Case (131 bytes, text/html)
2005-03-09 23:12 UTC, Jason Keirstead
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jason Keirstead 2005-03-09 23:12:02 UTC
Version:            (using KDE Devel)
Installed from:    Compiled sources

When a method is added as an event handler in KHTML, the scripting context of the method is passed in as the element who was the target of the event, rather than that of the window itself.

For example - if I do this:

myElement.onclick = function () {
    alert( "Element HTML is " + innerHTML );
}

... it *should not* work. The way to properly access the context element of an event, is by  referencing the 'target' or 'currentTarget' properties of the passed in event, as such:

myElement.onclick = function (e) {
    alert( "Element HTML is " + e.target.innerHTML );
}

... or in IE land ...

myElement.onclick = function () {
    alert( "Element HTML is " + window.event.srcElement.innerHTML );
}

While I cannot find the specification for how this *should* behave, in either the W3C DOM spec or the ECMA specs, this behaviour is different from both Internet Explorer and Gecko, so could break many scripts which expect certain properties to be either global veriables, or properties of the window object. I recently ran into this when trying to access the property 'parent' of the global window object inside a function whose source also had a 'parent' property.

Test case attached. When you click on 'This is the body', you should get a scripting error saying that the variable is undefined. In KHTML, you get an alert of the body innerHTML.
Comment 1 Jason Keirstead 2005-03-09 23:12:27 UTC
Created attachment 10048 [details]
Test Case
Comment 2 David Faure 2005-03-11 11:09:52 UTC
Hmm, my mail to 101202-done@bugs.kde.org didn't seem to close the bug?

CVS commit by faure: 

Move "pushing element/form/doc onto the scope"
 from JSEventListener::handleEvent to JSLazyEventListener::handleEvent so that
it only happens with listeners set from html attributes, not from JS - in which case the function already got a correct scope when created.
Testcase: events/eventhandlerscope.{html,js}


  M +8 -0      ChangeLog   1.398
  M +22 -11    ecma/kjs_events.cpp   1.93