Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 6b6646a

Browse files
committed
changed getWidth helper function of ::matchMedia to mirror native matchMedia function
1 parent 5b7a18e commit 6b6646a

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

src/eCSStender.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,28 +2472,10 @@ License: MIT License (see homepage)
24722472
* @param str query - the media query to test
24732473
*/
24742474
function matchMedia( query ) {
2475-
if( WINDOW.matchMedia ) {
2476-
matchMedia = function( query ) {
2477-
return WINDOW.matchMedia( query ).matches;
2478-
}
2475+
if( defined( WINDOW.matchMedia ) ) {
2476+
console.log(query, WINDOW.matchMedia(query).matches);
2477+
return WINDOW.matchMedia( query ).matches;
24792478
} else {
2480-
var
2481-
getWidth,
2482-
getHeight;
2483-
if( defined( window.innerWidth ) ) {
2484-
getWidth = function () { return window.innerWidth };
2485-
} else if ( defined( document.documentElement ) && defined( document.documentElement.clientWidth ) && document.documentElement.clientWidth ) {
2486-
getWidth = function() { return document.documentElement.clientWidth };
2487-
} else {
2488-
getWidth = function() { return document.getElementsByTagName(BODY)[0].clientWidth; }
2489-
}
2490-
if( defined( window.innerHeight ) ) {
2491-
getHeight = function() { return window.innerHeight; }
2492-
} else if ( defined( document.documentElement ) && defined( document.documentElement.clientHeight ) && document.documentElement.clientHeight ) {
2493-
getHeight = function() { return document.documentElement.clientHeight; }
2494-
} else {
2495-
getHeight = function() { return document.getElementsByTagName(BODY)[0].clientHeight; }
2496-
}
24972479
function convertToPixels( val ) {
24982480
var
24992481
number = parseInt(val.replace(/[^\d]+/g, ''), 10),
@@ -2509,6 +2491,20 @@ License: MIT License (see homepage)
25092491
}
25102492
return number;
25112493
}
2494+
var
2495+
getWidth,
2496+
getHeight;
2497+
getWidth = function() {
2498+
var _body = document.getElementsByTagName(BODY)[0];
2499+
return _body.clientWidth + convertToPixels( getCSSValue( _body, 'margin-left' ) ) + convertToPixels( getCSSValue( _body, 'margin-right' ) );
2500+
}
2501+
if( defined( window.innerHeight ) ) {
2502+
getHeight = function() { return window.innerHeight; }
2503+
} else if ( defined( document.documentElement ) && defined( document.documentElement.clientHeight ) && document.documentElement.clientHeight ) {
2504+
getHeight = function() { return document.documentElement.clientHeight; }
2505+
} else {
2506+
getHeight = function() { return document.getElementsByTagName(BODY)[0].clientHeight; }
2507+
}
25122508

25132509
matchMedia = function( query ) {
25142510
if(query.indexOf(COMMA) > -1) { // handle OR conditions

tests/js/core.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,30 +181,16 @@ eCSStender.onComplete(function(){
181181
module('Media Queries');
182182
test( 'eCSStender::matchMedia', function(){
183183
ok( typeof(eCSStender.matchMedia) == 'function', 'eCSStender::matchMedia() exists' );
184-
var
185-
getWidth,
186-
getHeight;
187-
if( typeof( window.innerWidth ) != 'undefined' ) {
188-
getWidth = function () { return window.innerWidth };
189-
} else if ( typeof( document.documentElement ) != 'undefined' && typeof( document.documentElement.clientWidth ) != 'undefined' && document.documentElement.clientWidth ) {
190-
getWidth = function() { return document.documentElement.clientWidth };
191-
} else {
192-
getWidth = function() { return document.getElementsByTagName('body')[0].clientWidth; }
193-
}
194-
if( typeof( window.innerHeight ) != 'undefined' ) {
195-
getHeight = function() { return window.innerHeight; }
196-
} else if ( typeof( document.documentElement ) != 'undefined' && typeof( document.documentElement.clientHeight ) != 'undefined' && document.documentElement.clientHeight ) {
197-
getHeight = function() { return document.documentElement.clientHeight; }
198-
} else {
199-
getHeight = function() { return document.getElementsByTagName('body')[0].clientHeight; }
184+
if( typeof(window.matchMedia) != 'undefined' && false ) {
185+
ok(true, 'window.matchMedia exists, these tests are using the native method');
200186
}
201187
var
202-
W = getWidth(),
203-
H = getHeight(),
188+
W = $(window).width(),
189+
H = $(window).height(),
204190
DW = screen.width,
205191
DH = screen.height,
206192
O = W > H ? 'landscape' : 'portrait';
207-
193+
208194
// orientation
209195
ok( eCSStender.matchMedia('screen and (orientation:portrait)') == (O == 'portrait'), '(orientation:portrait) ' + (O == 'portrait' ? 'matched' : 'does not match') );
210196
ok( eCSStender.matchMedia('screen and (orientation:landscape)') == (O == 'landscape'), '(orientation:landscape) ' + (O == 'landscape' ? 'matched' : 'does not match') );

0 commit comments

Comments
 (0)