<?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>234730</bug_id>
          
          <creation_ts>2021-12-28 14:18:08 -0800</creation_ts>
          <short_desc>load and beforeunload window event listeners callback order different in Safari than Chrome and Firefox</short_desc>
          <delta_ts>2023-06-07 08:18:07 -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>DOM</component>
          <version>Safari 15</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>BrowserCompat, InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jeff Johnson">opendarwin</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>cdumez</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1827123</commentid>
    <comment_count>0</comment_count>
      <attachid>448055</attachid>
    <who name="Jeff Johnson">opendarwin</who>
    <bug_when>2021-12-28 14:18:08 -0800</bug_when>
    <thetext>Created attachment 448055
Sample html demonstrating the inconsistency

The EventTarget.addEventListener() method has a boolean useCapture parameter:
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters

This mostly works the same in all browsers. However, it works differently in Safari for some window events such as &quot;load&quot; and &quot;beforeunload&quot;:
https://developer.mozilla.org/en-US/docs/Web/API/Window#events

The target of these events is the window object itself, so technically you could say there are no capturing or bubbling event phases.

Consider the following code:

window.addEventListener(&quot;load&quot;, () =&gt; { console.log(&quot;load false&quot;); }, false);
window.addEventListener(&quot;load&quot;, () =&gt; { console.log(&quot;load true&quot;); }, true);
window.addEventListener(&quot;beforeunload&quot;, () =&gt; { console.log(&quot;beforeunload false&quot;); }, false);
window.addEventListener(&quot;beforeunload&quot;, () =&gt; { console.log(&quot;beforeunload true&quot;); }, true);

In Safari, the above useCapture true listeners are called prior to the above useCapture false listeners. However, in Chrome and Firefox, these listeners are called in the order registered, as if the useCapture parameter were irrelevant.

I won&apos;t argue which behavior is &quot;correct&quot;. I&apos;m just noting that Safari is inconsistent with the other browsers.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1827132</commentid>
    <comment_count>1</comment_count>
    <who name="Jeff Johnson">opendarwin</who>
    <bug_when>2021-12-28 15:02:39 -0800</bug_when>
    <thetext>Tested on macOS 11.6.2 with Safari 15.2, Safari Technology Preview 137, Chrome 96, and Firefox 95. Also tested with Mobile Safari on iOS 15.2.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1828096</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-01-04 14:19:46 -0800</bug_when>
    <thetext>&lt;rdar://problem/87114687&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1828361</commentid>
    <comment_count>3</comment_count>
    <who name="Sam Sneddon [:gsnedders]">gsnedders</who>
    <bug_when>2022-01-05 12:55:10 -0800</bug_when>
    <thetext>useCapture shouldn&apos;t be affecting event dispatch order; dom/events/Event-dispatch-order.html in WPT is meant to test this but clearly doesn&apos;t suffice.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1828373</commentid>
    <comment_count>4</comment_count>
    <who name="Jeff Johnson">opendarwin</who>
    <bug_when>2022-01-05 13:22:11 -0800</bug_when>
    <thetext>(In reply to Sam Sneddon [:gsnedders] from comment #3)
&gt; useCapture shouldn&apos;t be affecting event dispatch order;
&gt; dom/events/Event-dispatch-order.html in WPT is meant to test this but
&gt; clearly doesn&apos;t suffice.

I&apos;m happy to accept Safari&apos;s behavior as correct, and would file bugs against Chromium and Firefox, though perhaps someone more important than me would be more persuasive to them.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1828633</commentid>
    <comment_count>5</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2022-01-06 09:04:08 -0800</bug_when>
    <thetext>(In reply to Sam Sneddon [:gsnedders] from comment #3)
&gt; useCapture shouldn&apos;t be affecting event dispatch order;
&gt; dom/events/Event-dispatch-order.html in WPT is meant to test this but
&gt; clearly doesn&apos;t suffice.

@Sam: Could you clarify why the event dispatch order should not be impacted by useCapture? I am looking at https://dom.spec.whatwg.org/#concept-event-dispatch and our implementation seems to match the spec:
1. First we Invoke with struct, event, &quot;capturing&quot;, and legacyOutputDidListenersThrowFlag if given.
2. Then we Invoke with struct, event, &quot;bubbling&quot;, and legacyOutputDidListenersThrowFlag if given.

From https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke, some listeners are skipped during each phase based on listener&apos;s &quot;capture&quot;.

So it makes sense to me that we&apos;re calling the ones with useCapture=true first and then the ones with useCapture=false.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>448055</attachid>
            <date>2021-12-28 14:18:08 -0800</date>
            <delta_ts>2021-12-28 14:18:08 -0800</delta_ts>
            <desc>Sample html demonstrating the inconsistency</desc>
            <filename>index.html</filename>
            <type>text/html</type>
            <size>560</size>
            <attacher name="Jeff Johnson">opendarwin</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KPGhlYWQ+Cgk8bWV0YSBodHRwLWVxdWl2
PSJjb250ZW50LXR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD11dGYtOCI+Cgk8dGl0
bGU+d2luZG93IGV2ZW50IHRlc3Q8L3RpdGxlPgo8L2hlYWQ+Cjxib2R5Pgo8aDE+d2luZG93IGV2
ZW50IHRlc3Q8L2gxPgo8c2NyaXB0Pgp3aW5kb3cuYWRkRXZlbnRMaXN0ZW5lcigibG9hZCIsICgp
ID0+IHsgY29uc29sZS5sb2coImxvYWQgZmFsc2UiKTsgfSwgZmFsc2UpOwp3aW5kb3cuYWRkRXZl
bnRMaXN0ZW5lcigibG9hZCIsICgpID0+IHsgY29uc29sZS5sb2coImxvYWQgdHJ1ZSIpOyB9LCB0
cnVlKTsKd2luZG93LmFkZEV2ZW50TGlzdGVuZXIoImJlZm9yZXVubG9hZCIsICgpID0+IHsgY29u
c29sZS5sb2coImJlZm9yZXVubG9hZCBmYWxzZSIpOyB9LCBmYWxzZSk7CndpbmRvdy5hZGRFdmVu
dExpc3RlbmVyKCJiZWZvcmV1bmxvYWQiLCAoKSA9PiB7IGNvbnNvbGUubG9nKCJiZWZvcmV1bmxv
YWQgdHJ1ZSIpOyB9LCB0cnVlKTsKPC9zY3JpcHQ+CjwvYm9keT4KPC9odG1sPgo=
</data>

          </attachment>
      

    </bug>

</bugzilla>