Bug 26147

Summary: It should be possible to dispatch events on documents created using DOMParser
Product: WebKit Reporter: Laurentj <laurent>
Component: XML DOMAssignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Major CC: ap, cdumez, cmarcelo, commit-queue, david.rivron, dominicc, esprehn+autocc, kondapallykalyan, loic, olivierg, rniwa, tonikitoo
Priority: P2    
Version: 525.x (Safari 3.2)   
Hardware: Macintosh   
OS: OS X 10.5   
Bug Depends on: 27662, 162620    
Bug Blocks:    
Attachments:
Description Flags
testcase
none
DOMParse-addEventListener testcase
none
Patch none

Description Laurentj 2009-06-02 13:36:31 PDT
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 Alexey Proskuryakov 2009-06-04 04:02:18 PDT
Confirmed. This works fine for XML documents in frames, but not for frameless ones (as created by DOMParser or XMLHttpRequest).
Comment 2 Alexey Proskuryakov 2011-07-05 00:12:31 PDT
*** Bug 63905 has been marked as a duplicate of this bug. ***
Comment 3 Loic Dachary 2011-09-30 01:22:46 PDT
Created attachment 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 Loic Dachary 2011-10-04 02:50:04 PDT
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 Loic Dachary 2011-10-04 06:37:30 PDT
Created attachment 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 Loic Dachary 2011-10-04 08:46:34 PDT
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 Loic Dachary 2011-10-05 07:01:13 PDT
This is related to bug 27662
Comment 8 Chris Dumez 2016-09-27 12:51:18 PDT
Fixing via Bug 162620.

*** This bug has been marked as a duplicate of bug 162620 ***
Comment 9 Chris Dumez 2016-09-27 12:52:47 PDT
I'll try the test case on this bug before duping this in case this is not covered.
Comment 10 Chris Dumez 2016-09-27 12:55:49 PDT
Yes, I don't think the DOMParser.parseFromString() case is covered by my patch yet and will require some more work. I'll use this bug to support documents created this way.
Comment 11 Chris Dumez 2016-09-27 15:22:25 PDT
Created attachment 290015 [details]
Patch
Comment 12 WebKit Commit Bot 2016-09-27 16:35:29 PDT
Comment on attachment 290015 [details]
Patch

Clearing flags on attachment: 290015

Committed r206469: <http://trac.webkit.org/changeset/206469>
Comment 13 WebKit Commit Bot 2016-09-27 16:35:34 PDT
All reviewed patches have been landed.  Closing bug.