<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>86543</bug_id>
          
          <creation_ts>2012-05-15 16:21:02 -0700</creation_ts>
          <short_desc>ImageDocuments erroneously trigger beforeload events for the main resource</short_desc>
          <delta_ts>2012-05-19 22:14:22 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Page Loading</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Vicki Pfau">jeffrey+webkit</reporter>
          <assigned_to name="Vicki Pfau">jeffrey+webkit</assigned_to>
          <cc>aestes</cc>
    
    <cc>ap</cc>
    
    <cc>beidson</cc>
    
    <cc>ian</cc>
    
    <cc>japhet</cc>
    
    <cc>webkit.review.bot</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>624711</commentid>
    <comment_count>0</comment_count>
    <who name="Vicki Pfau">jeffrey+webkit</who>
    <bug_when>2012-05-15 16:21:02 -0700</bug_when>
    <thetext>When an ImageDocument is loading, it creates an &lt;img&gt; tag, which triggers a beforeload event. However, the event corresponds to the main resource, and can cause a crash if the event is handled and preventDefault() is called.

&lt;rdar://problem/11309013&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624717</commentid>
    <comment_count>1</comment_count>
      <attachid>142094</attachid>
    <who name="Vicki Pfau">jeffrey+webkit</who>
    <bug_when>2012-05-15 16:25:59 -0700</bug_when>
    <thetext>Created attachment 142094
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624733</commentid>
    <comment_count>2</comment_count>
    <who name="Vicki Pfau">jeffrey+webkit</who>
    <bug_when>2012-05-15 16:49:15 -0700</bug_when>
    <thetext>Committed r117185: &lt;http://trac.webkit.org/changeset/117185&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625634</commentid>
    <comment_count>3</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2012-05-16 10:53:13 -0700</bug_when>
    <thetext>Why is it safe to dispatch beforeload on image documents? This appears to be as dangerous as elsewhere, because a handler can do evil things.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625652</commentid>
    <comment_count>4</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2012-05-16 11:13:55 -0700</bug_when>
    <thetext>(In reply to comment #3)
&gt; Why is it safe to dispatch beforeload on image documents? This appears to be as dangerous as elsewhere, because a handler can do evil things.

I&apos;m a little confused about your question.  I don&apos;t think anyone here said it is safe to dispatch beforeload on image documents.

But the bug here was really that we were dispatching beforeLoad for an &lt;img&gt; element inside the image document... but in reality the resource was already being loaded; It was the main resource for the document.

Logically - since we don&apos;t do beforeLoad for main resources - the resource for an image document shouldn&apos;t be any different, and especially not by representing it as an &lt;img&gt; element which is an implementation detail of image documents.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625659</commentid>
    <comment_count>5</comment_count>
    <who name="Vicki Pfau">jeffrey+webkit</who>
    <bug_when>2012-05-16 11:17:10 -0700</bug_when>
    <thetext>I&apos;m not entirely sure why this happens, but by calling dispatchPendingBeforeLoadEvent() in that branch actually prevents the beforeload event from reaching any handlers. It also triggers logic essential to making images show up in the first place. If the second branch is taken, it triggers the beforeload event after handlers get installed, thus the source of the bug. Skipping both branches misses the logic essential to making the images show up. From an objective standpoint, this means that some code is in the wrong place and needs to be refactored. However, I&apos;ve been unsuccessful at finding where the aforementioned logic takes place, as it&apos;s nowhere obvious.

I&apos;ve filed bug 86658 as a followup.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625667</commentid>
    <comment_count>6</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2012-05-16 11:22:15 -0700</bug_when>
    <thetext>(In reply to comment #5)
&gt; I&apos;m not entirely sure why this happens, but by calling dispatchPendingBeforeLoadEvent() in that branch actually prevents the beforeload event from reaching any handlers. It also triggers logic essential to making images show up in the first place. If the second branch is taken, it triggers the beforeload event after handlers get installed, thus the source of the bug. Skipping both branches misses the logic essential to making the images show up. From an objective standpoint, this means that some code is in the wrong place and needs to be refactored. However, I&apos;ve been unsuccessful at finding where the aforementioned logic takes place, as it&apos;s nowhere obvious.
&gt; 
&gt; I&apos;ve filed bug 86658 as a followup.

I assumed that the very first check in that method - if (!m_hasPendingBeforeLoadEvent) - failed and it bailed out.

But if calling that method actually does other stuff relevant to the image showing up, then my assumption can&apos;t possibly have been the case.

I strongly urge you to explore this soon.  Now both Alexey and I are confused.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625671</commentid>
    <comment_count>7</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2012-05-16 11:26:27 -0700</bug_when>
    <thetext>&gt; I&apos;m a little confused about your question.  I don&apos;t think anyone here said it is safe to dispatch beforeload on image documents.

That&apos;s what the new code says, in a very direct manner. Per Jeffrey&apos;s answer, the code lies about what it does, however we don&apos;t appear to have complete understanding.

&gt; I&apos;m not entirely sure why this happens, but by calling dispatchPendingBeforeLoadEvent() in that branch actually prevents the beforeload event from reaching any handlers.

Perhaps it fires the event earlier, so handlers installed on img element don&apos;t have a chance to run? But handlers installed on ancestors would fire, and in a manner that&apos;s even worse than before?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625678</commentid>
    <comment_count>8</comment_count>
    <who name="Vicki Pfau">jeffrey+webkit</who>
    <bug_when>2012-05-16 11:30:40 -0700</bug_when>
    <thetext>(In reply to comment #7) 
&gt; &gt; I&apos;m not entirely sure why this happens, but by calling dispatchPendingBeforeLoadEvent() in that branch actually prevents the beforeload event from reaching any handlers.
&gt; 
&gt; Perhaps it fires the event earlier, so handlers installed on img element don&apos;t have a chance to run? But handlers installed on ancestors would fire, and in a manner that&apos;s even worse than before?

The event doesn&apos;t bubble, so ancestors never see it.

I&apos;ve stepped through the code somewhat thoroughly and it looks like it bails out without actually doing anything noteworthy, but if I skip that code entirely, then img tags don&apos;t work. So it looks like some constructor somewhere must be doing the relevant work, I&apos;m just not sure which one.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625679</commentid>
    <comment_count>9</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2012-05-16 11:30:58 -0700</bug_when>
    <thetext>&gt; representing it as an &lt;img&gt; element which is an implementation detail of image documents

I&apos;ve been under the impression that HTML5 specifies this, however I cannot find any proof now.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625685</commentid>
    <comment_count>10</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2012-05-16 11:34:29 -0700</bug_when>
    <thetext>&gt; The event doesn&apos;t bubble, so ancestors never see it.

That&apos;s not what I&apos;m seeing in my testing - the below snippet logs both &quot;bl&quot; and &quot;bl2&quot;.

&lt;body&gt;
&lt;script&gt;
document.body.addEventListener(&quot;beforeload&quot;, function() { console.log(&apos;bl&apos;) }, true);
document.addEventListener(&quot;beforeload&quot;, function() { console.log(&apos;bl2&apos;) }, true);
&lt;/script&gt;
&lt;img src=&quot;aaa&quot;&gt;
&lt;img src=&quot;bbb&quot;&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>625687</commentid>
    <comment_count>11</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2012-05-16 11:35:50 -0700</bug_when>
    <thetext>Well, that&apos;s a capturing listener, so it doesn&apos;t actually contradict what you said about bubbling, but it still shows that listeners are invoked on ancestors.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>628546</commentid>
    <comment_count>12</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2012-05-19 22:14:22 -0700</bug_when>
    <thetext>(In reply to comment #11)
&gt; Well, that&apos;s a capturing listener, so it doesn&apos;t actually contradict what you said about bubbling, but it still shows that listeners are invoked on ancestors.

In the capture phase, events are seen by all ancestors. In the bubbling phase, they&apos;re only seen if they bubble.

(In reply to comment #9)
&gt; &gt; representing it as an &lt;img&gt; element which is an implementation detail of image documents
&gt; 
&gt; I&apos;ve been under the impression that HTML5 specifies this, however I cannot find any proof now.

See the navigation section.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>142094</attachid>
            <date>2012-05-15 16:25:59 -0700</date>
            <delta_ts>2012-05-15 16:29:55 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-86543-20120515162602.patch</filename>
            <type>text/plain</type>
            <size>1575</size>
            <attacher name="Vicki Pfau">jeffrey+webkit</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMTE2Nzk2CmRpZmYgLS1naXQgYS9Tb3VyY2UvV2ViQ29yZS9D
aGFuZ2VMb2cgYi9Tb3VyY2UvV2ViQ29yZS9DaGFuZ2VMb2cKaW5kZXggN2FmMzU3ODUzYTRiYjhi
ZTQ0YzZiNjNlN2M1YzI3Y2ExNjVhODEzZC4uYTdmN2Q3NjYxN2E3YmY0MjY0OWQ5NTAxZTNkN2Uy
ZDc2NWM4MzI5NCAxMDA2NDQKLS0tIGEvU291cmNlL1dlYkNvcmUvQ2hhbmdlTG9nCisrKyBiL1Nv
dXJjZS9XZWJDb3JlL0NoYW5nZUxvZwpAQCAtMSwzICsxLDE1IEBACisyMDEyLTA1LTE1ICBKZWZm
cmV5IFBmYXUgIDxqcGZhdUBhcHBsZS5jb20+C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TE5OSw3ICsxOTksNyBAQCB2b2lkIEltYWdlTG9hZGVyOjp1cGRhdGVG
cm9tRWxlbWVudCgpCiAgICAgICAgIG1faW1hZ2VDb21wbGV0ZSA9ICFuZXdJbWFnZTsKIAogICAg
ICAgICBpZiAobmV3SW1hZ2UpIHsKLSAgICAgICAgICAgIGlmICghbV9lbGVtZW50LT5kb2N1bWVu
dCgpLT5oYXNMaXN0ZW5lclR5cGUoRG9jdW1lbnQ6OkJFRk9SRUxPQURfTElTVEVORVIpKQorICAg
ICAgICAgICAgaWYgKCFtX2VsZW1lbnQtPmRvY3VtZW50KCktPmhhc0xpc3RlbmVyVHlwZShEb2N1
bWVudDo6QkVGT1JFTE9BRF9MSVNURU5FUikgfHwgbV9lbGVtZW50LT5kb2N1bWVudCgpLT5pc0lt
YWdlRG9jdW1lbnQoKSkKICAgICAgICAgICAgICAgICBkaXNwYXRjaFBlbmRpbmdCZWZvcmVMb2Fk
RXZlbnQoKTsKICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICBiZWZvcmVMb2FkRXZl
bnRTZW5kZXIoKS5kaXNwYXRjaEV2ZW50U29vbih0aGlzKTsK
</data>
<flag name="review"
          id="148347"
          type_id="1"
          status="+"
          setter="beidson"
    />
          </attachment>
      

    </bug>

</bugzilla>