Bug 26147

Summary: No support of DOM events on a frameless document
Product: WebKit Reporter: Laurentj <laurent@xulfr.org>
Component: XML DOMAssignee: Nobody <webkit-unassigned@lists.webkit.org>
Status: NEW    
Severity: Major CC: ap@webkit.org, cmarcelo@webkit.org, david.rivron@free.fr, dominicc@chromium.org, loic@dachary.org, olivierg@zoomorama.com, tonikitoo@webkit.org
Priority: P2    
Version: 525.x (Safari 3.2)   
Hardware: Macintosh   
OS: Mac OS X 10.5   
Bug Depends on: 27662    
Bug Blocks:    
Attachments:
Description Flags
testcase
none
DOMParse-addEventListener testcase none

Description From 2009-06-02 13:36:31 PST
In a document loaded with DOMParser, events are not propagated.

This example doesn't work (no alert) in Safari (3.2 and 4beta) and in Chrome 2:

      var thatDoc = '<?xml version="1.0" encoding="UTF-8"?>\
<root>\
  <foo>bar</foo>\
</root>';
      var parser = new DOMParser();
      var doc = parser.parseFromString(thatDoc, 'text/xml');
      doc.addEventListener("click", function(evt){ alert('ok');}, true);
      var evt = doc.createEvent("Event");
      evt.initEvent( "click", true, true );
      doc.getElementsByTagName("foo")[0].dispatchEvent(evt);

It works in Gecko and Opera.
------- Comment #1 From 2009-06-04 04:02:18 PST -------
Confirmed. This works fine for XML documents in frames, but not for frameless ones (as created by DOMParser or XMLHttpRequest).
------- Comment #2 From 2011-07-05 00:12:31 PST -------
*** Bug 63905 has been marked as a duplicate of this bug. ***
------- Comment #3 From 2011-09-30 01:22:46 PST -------
Created an attachment (id=109264) [details]
testcase

The attached test case is to be placed in LayoutTests/fast/events/DOMParse-addEventListener.html and will fail as expected with the following output:

 This tests is for https://bugs.webkit.org/show_bug.cgi?id=26147 and shows that in a document loaded with DOMParser, events are propagated.

 On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


 FAIL fired should be true. Was false.
 PASS successfullyParsed is true

 TEST COMPLETE
------- Comment #4 From 2011-10-04 02:50:04 PST -------
In order to figure out where the problem happens, the execution of the proposed test case is run step by step under gdb, comparing what happens with :

document.dispatchEvent(evt);

where document is the document being displayed and

doc.dispatchEvent(evt);

where doc is the result of

var doc = parser.parseFromString(thatDoc, 'text/xml');

It differs in

  void JSEventListener::handleEvent(ScriptExecutionContext* scriptExecutionContext, Event* event)
  {
...
    JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(scriptExecutionContext, m_isolatedWorld.get());
      if (!globalObject)
          return;
...
}

where globalObject is null for the parsed XML document and the handler is therefore not fired. The reason why it is null is because the scriptExecutionContext->frame() function returns null. It matches the diagnostic of the bug report claiming that the problem does not exist when the XML document is loaded using an iframe.
------- Comment #5 From 2011-10-04 06:37:30 PST -------
Created an attachment (id=109614) [details]
DOMParse-addEventListener testcase

The new testcase checks that the document returned by parseFromString supports http://www.w3.org/TR/DOM-Level-2-Events/events.html with

  shouldBeTrue('doc.implementation.hasFeature("Events","2.0")');

It may be worth returning false so that the caller can rely on this assertion to know the events will be ignored.
------- Comment #6 From 2011-10-04 08:46:34 PST -------
In 

Source/WebCore/dom/DOMImplementation.h

static bool hasFeature(const String& feature, const String& version);

being static does not allow for a per-document return value. Making sure hasFeature returns false as suggested above is not a trivial change.
------- Comment #7 From 2011-10-05 07:01:13 PST -------
This is related to bug 27662