Permalink
Browse files

Fix bug: incorrect device pixel ratio

Fixes test case for goog.dom.devicePixelRatio and corrects order of
pixel ratio checks.

This fixes the bug that 0.75 will always be the device pixel ratio if
window.devicePixelRatio is not provided by the execution environment.

This bug did not come up earlier because the test-case for this was
incorrect.

Adds mash to the list of authors as stated in the guidelines for
contributing to this repository.
  • Loading branch information...
mashedcode committed Aug 15, 2016
1 parent 6f639fb commit 58dc0cfb3db2fc80be2063f09d57f3166f27e3bc
Showing with 35 additions and 19 deletions.
  1. +1 −0 AUTHORS
  2. +18 −6 closure/goog/dom/dom.js
  3. +16 −13 closure/goog/dom/dom_test.js
@@ -24,3 +24,4 @@ Peter Lu <peterlu83+github@gmail.com>
Filipe Catraia <filipe.catraia@deliveryhero.com>
Dan Rubalsky <drubalsky@plentakill.com>
Michael Zhou <zhoumotongxue008@gmail.com>
mash <mashedcode@users.noreply.github.com>
@@ -2236,9 +2236,11 @@ goog.dom.getPixelRatio = function() {
if (goog.isDef(win.devicePixelRatio)) {
return win.devicePixelRatio;
} else if (win.matchMedia) {
return goog.dom.matchesPixelRatio_(.75) ||
goog.dom.matchesPixelRatio_(1.5) || goog.dom.matchesPixelRatio_(2) ||
goog.dom.matchesPixelRatio_(3) || 1;
// Should be for IE10 and FF6-17 (this basically clamps to lower)
// Note that the order of these statements is important
return goog.dom.matchesPixelRatio_(3) || goog.dom.matchesPixelRatio_(2) ||
goog.dom.matchesPixelRatio_(1.5) || goog.dom.matchesPixelRatio_(1) ||
.75;
}
return 1;
};
@@ -2253,10 +2255,20 @@ goog.dom.getPixelRatio = function() {
*/
goog.dom.matchesPixelRatio_ = function(pixelRatio) {
var win = goog.dom.getWindow();
/**
* Due to the 1:96 fixed ratio of CSS in to CSS px, 1dppx is equivalent to
* 96dpi.
* @const {number}
*/
var dpiPerDppx = 96;
var query =
('(-webkit-min-device-pixel-ratio: ' + pixelRatio + '),' +
'(min--moz-device-pixel-ratio: ' + pixelRatio + '),' +
'(min-resolution: ' + pixelRatio + 'dppx)');
// FF16-17
'(min-resolution: ' + pixelRatio + 'dppx),' +
// FF6-15
'(min--moz-device-pixel-ratio: ' + pixelRatio + '),' +
// IE10 (this works for the two browsers above too but I don't want to
// trust the 1:96 fixed ratio magic)
'(min-resolution: ' + (pixelRatio * dpiPerDppx) + 'dpi)';
return win.matchMedia(query).matches ? pixelRatio : 0;
};

@@ -1819,22 +1819,25 @@ function isIE8OrHigher() {
return goog.userAgent.IE && goog.userAgent.product.isVersion('8');
}

function setWindow(win) {
stubs.set(goog.dom, 'getWindow', goog.functions.constant(win));
}

function testDevicePixelRatio() {
stubs.set(goog.dom, 'getWindow', goog.functions.constant({
matchMedia: function(query) { return {matches: query.indexOf('1.5') >= 0}; }
}));

stubs.set(goog.functions, 'CACHE_RETURN_VALUE', false);
var devicePixelRatio = 1.5;
setWindow({
'matchMedia': function(query) {
return {
'matches': devicePixelRatio >= parseFloat(query.split(': ')[1], 10)
};
}
});

assertEquals(goog.dom.getPixelRatio(), 1.5);
assertEquals(devicePixelRatio, goog.dom.getPixelRatio());

stubs.set(
goog.dom, 'getWindow', goog.functions.constant({devicePixelRatio: 2.0}));
goog.dom.devicePixelRatio_ = null;
assertEquals(goog.dom.getPixelRatio(), 2);
setWindow({'devicePixelRatio': 2.0});
assertEquals(2, goog.dom.getPixelRatio());

stubs.set(goog.dom, 'getWindow', goog.functions.constant({}));
goog.dom.devicePixelRatio_ = null;
assertEquals(goog.dom.getPixelRatio(), 1);
setWindow({});
assertEquals(1, goog.dom.getPixelRatio());
}

0 comments on commit 58dc0cf

Please sign in to comment.