Bug 123293 - MediaQueryList listeners blocked by CSS media queries
Summary: MediaQueryList listeners blocked by CSS media queries
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac All
: P2 Major
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-24 15:31 PDT by Andrew Leung
Modified: 2022-02-10 03:58 PST (History)
2 users (show)

See Also:


Attachments
A standalone HTML test page that illustrates the bug (2.18 KB, text/html)
2013-10-24 15:31 PDT, Andrew Leung
no flags Details
A standalone HTML test page that illustrates the bug (2.18 KB, text/html)
2013-10-24 15:34 PDT, Andrew Leung
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Leung 2013-10-24 15:31:10 PDT
Created attachment 215111 [details]
A standalone HTML test page that illustrates the bug

# Overview

MediaQueryList listeners created using window.matchMedia and addListener() are blocked when certain CSS media queries are present.

# Steps to Reproduce

The standalone test page (attached) responds to changes to the media query "max-width: 768px", both in JS as a MediaQueryList and CSS as a media query.

1. Load the test page in a browser that is 768px wide.
2. Widen the browser by 1px, and observe the result.
3. Reload the page in place.
4. Narrow the browser by 1px, and observe the result.

# Actual Results

When the browser width goes from 768px to 769px, the answer was NO + YES. The counter incremented. When widening the browser by one extra pixel to 770px, the answer became YES + YES. The counter did not increment.

When the browser width goes from 769px to 768px, the answer was YES + NO. The counter incremented. When narrowing the browser by one extra pixel to 767px, the answer became NO + NO. The counter did not increment.

These results were obtained in these browsers:

* Safari 6.1 (8537.71) on OS X 10.8.5
* WebKit nightly build 538.4+
* MobileSafari on iPad running iOS 7.0.3

# Expected Results

When the browser width goes from 768px to 769px, the answer should be YES + YES. The counter should increment.

When the browser width goes from 769px to 768px, the answer should be NO + NO. The counter should increment.

# Additional Info

The actual results show that while the CSS media query immediately updated the page when the browser width crosses the threshold, the MediaQueryList listener did not, until the browser width changed by another pixel.

On the iPad, rotating between portrait and landscape orientations essentially changes the browser width between 768px and 1024px respectively. The same results were observed, except the browser width cannot be changed by another pixel to unblock the event/listener. In a way, this bug has a much larger impact on the iPad because of this.
Comment 1 Andrew Leung 2013-10-24 15:34:27 PDT
Created attachment 215113 [details]
A standalone HTML test page that illustrates the bug
Comment 2 Andrew Leung 2014-03-27 12:53:07 PDT
Just tested this on WebKit r166355, and the problem still exists.
Comment 3 Andrew Leung 2014-09-19 14:12:31 PDT
Just tested this on Safari 7.1 and WebKit r173755, and the problem still exists.
Comment 4 gcgabfest 2014-11-11 11:19:08 PST
The ipad is exactly where you want matchmedia to work for resoponsive design! Yes you can work around this with a resize listener, but this bug took me a while to track down. Looks like you can't vote on bugs here, but this one certainly would have mine!
Comment 5 Andrew Leung 2015-04-10 17:09:07 PDT
Just tested this on Safari 8.05 and WebKit r182629, and the problem still exists.
Comment 6 Andrew Leung 2016-09-21 11:52:45 PDT
Tested this again on Safari 10.0 and WebKit r206206, and the problem still exists.

To reiterate, this bug can be replicated on all Safari browsers so far, but its effect is mostly felt by iOS devices because they don't have resizable windows. On a page that uses window.matchMedia to listen to width changes, if the threshold is between the device's portrait and landscape widths, the event won't fire when orientation changes. That's the incorrect behavior.
Comment 7 Ryan Fitzer 2018-11-28 16:23:42 PST
I was *not* able to reproduce this issue on Safari 12.

OS: macOS Sierra 10.12.6
Safari: 12.0.1 (12606.2.104.1.2)
Comment 8 Andrew Leung 2018-11-29 11:47:17 PST
Same here. This is no longer reproducible in Safari running on:

* macOS 10.13.6 [Safari 12.0.1 (13606.2.104.1.2)]
* iOS 10.3.3 (iPad)
* iOS 12.0.1 (iPad)

Maybe it's fixed?