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

Commit cbfa7f6

Browse files
version skipped a number, rolled back to 1.2.7
Optimized imported code for eCSStender::matchMedia()
1 parent c09871d commit cbfa7f6

File tree

2 files changed

+93
-133
lines changed

2 files changed

+93
-133
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*v1.2.8 (2011-07-14)
1+
*v1.2.7 (2011-07-14)
22
* added eCSStender::matchMedia() method
33

44
*v1.2.6.6* (2010-08-26)

src/eCSStender.js

Lines changed: 92 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ License: MIT License (see homepage)
2424
REGEXP = RegExp,
2525
DOCUMENT = document,
2626
WINDOW = window,
27+
SCREEN = screen,
2728
LOCATION = WINDOW.location.href,
2829
EMPTY_FN = function(){},
2930

@@ -147,7 +148,7 @@ License: MIT License (see homepage)
147148
// eCSStender Object
148149
eCSStender = {
149150
name: ECSSTENDER,
150-
version: '1.2.8',
151+
version: '1.2.7',
151152
fonts: [],
152153
pages: {},
153154
at: {},
@@ -1231,6 +1232,10 @@ License: MIT License (see homepage)
12311232
{
12321233
return DOCUMENT.createElement( el );
12331234
}
1235+
function getElements( tag )
1236+
{
1237+
return DOCUMENT.getElementsByTagName( tag );
1238+
}
12341239
function newRegExp( rxp )
12351240
{
12361241
return new RegExp( rxp );
@@ -2471,12 +2476,17 @@ License: MIT License (see homepage)
24712476
*
24722477
* @param str query - the media query to test
24732478
*/
2474-
function matchMedia( query ) {
2475-
if( defined( WINDOW.matchMedia ) ) {
2476-
console.log(query, WINDOW.matchMedia(query).matches);
2479+
function matchMedia( query )
2480+
{
2481+
if ( defined( WINDOW.matchMedia ) )
2482+
{
24772483
return WINDOW.matchMedia( query ).matches;
2478-
} else {
2479-
function convertToPixels( val ) {
2484+
}
2485+
else
2486+
{
2487+
/* Helpers */
2488+
function convertToPixels( val )
2489+
{
24802490
var
24812491
number = parseInt(val.replace(/[^\d]+/g, ''), 10),
24822492
unit = val.replace(number, '');
@@ -2494,156 +2504,106 @@ License: MIT License (see homepage)
24942504
var
24952505
getWidth,
24962506
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' ) );
2507+
getWidth = function()
2508+
{
2509+
var _body = getElements(BODY)[0];
2510+
return _body.clientWidth +
2511+
convertToPixels( getCSSValue( _body, 'margin-left' ) ) +
2512+
convertToPixels( getCSSValue( _body, 'margin-right' ) );
2513+
}
2514+
if ( defined( WINDOW.innerHeight ) )
2515+
{
2516+
getHeight = function()
2517+
{
2518+
return WINDOW.innerHeight;
2519+
}
25002520
}
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; }
2521+
else if ( defined( DOCUMENT.documentElement ) &&
2522+
defined( DOCUMENT.documentElement.clientHeight ) &&
2523+
DOCUMENT.documentElement.clientHeight )
2524+
{
2525+
getHeight = function()
2526+
{
2527+
return document.documentElement.clientHeight;
2528+
}
25072529
}
2508-
2509-
matchMedia = function( query ) {
2510-
if(query.indexOf(COMMA) > -1) { // handle OR conditions
2511-
var
2512-
queries = query.split(COMMA),
2530+
else
2531+
{
2532+
getHeight = function()
2533+
{
2534+
return getElements(BODY)[0].clientHeight;
2535+
}
2536+
}
2537+
/* Method */
2538+
matchMedia = function( query )
2539+
{
2540+
var queries, matches, mediaQueryRegex, W, DW, H, DH, i, q, prop, val;
2541+
2542+
// handle OR conditions
2543+
if ( query.indexOf(COMMA) > -1 )
2544+
{
2545+
queries = query.split(COMMA);
25132546
i = queries.length;
2514-
while( i ) {
2515-
var q = queries[i - 1];
2516-
q = trim(q);
2517-
if( matchMedia(q) ) { // if any of the conditions match, we can return true and bail
2547+
while( i-- )
2548+
{
2549+
q = trim( queries[i] );
2550+
if ( matchMedia(q) )
2551+
{
2552+
// if any of the conditions match, we can return true and bail
25182553
return TRUE;
25192554
}
2520-
i--;
25212555
}
25222556
}
2523-
var
2557+
//isInheritedProperty( obj, prop )
25242558
queries = query.split(' and '), // split the query into each condition
25252559
matches = TRUE, // optimism
25262560
mediaQueryRegex = newRegExp(REGEXP_MQ_PARENS),
25272561
W = getWidth(),
2528-
DW = screen.width,
2562+
DW = SCREEN.width,
25292563
H = getHeight(),
2530-
DH = screen.height;
2564+
DH = SCREEN.height;
25312565
i = queries.length;
2532-
while ( i ) {
2533-
var q = queries[i - 1];
2534-
if(mediaQueryRegex.test(q)) { // we only test query parts in the style of (property:value)
2535-
var
2536-
q = q.split(COLON),
2537-
prop = q[0].toLowerCase(),
2566+
while ( i-- )
2567+
{
2568+
q = queries[i];
2569+
// we only test query parts in the style of (property:value)
2570+
if ( mediaQueryRegex.test(q) )
2571+
{
2572+
q = q.split(COLON);
2573+
prop = q[0].toLowerCase();
25382574
val = q[1];
25392575

25402576
prop = prop.replace(/^\(/, EMPTY);
25412577
val = val.replace(/\)$/, EMPTY);
25422578

2543-
if( prop != ORIENTATION ) {
2579+
if ( prop != ORIENTATION )
2580+
{
25442581
val = convertToPixels(val);
25452582
}
2546-
switch( prop ) {
2547-
case ORIENTATION:
2548-
switch( val ) {
2549-
case LANDSCAPE:
2550-
if( W < H )
2551-
{
2552-
matches = FALSE;
2553-
}
2554-
break;
2555-
case PORTRAIT:
2556-
if( W > H )
2557-
{
2558-
matches = FALSE;
2559-
}
2560-
break;
2561-
default:
2562-
// we only support landscape and portrait
2563-
break;
2564-
}
2565-
break;
2566-
case WIDTH:
2567-
if( W != val )
2568-
{
2569-
matches = FALSE;
2570-
}
2571-
break;
2572-
case MAXWIDTH:
2573-
if( W > val )
2574-
{
2575-
matches = FALSE;
2576-
}
2577-
break;
2578-
case MINWIDTH:
2579-
if( W < val )
2580-
{
2581-
matches = FALSE;
2582-
}
2583-
break;
2584-
case DEVWIDTH:
2585-
if( DW != val )
2586-
{
2587-
matches = FALSE;
2588-
}
2589-
break;
2590-
case DEVMAXWIDTH:
2591-
if( DW > val )
2592-
{
2593-
matches = FALSE;
2594-
}
2595-
break;
2596-
case DEVMINWIDTH:
2597-
if( DW < val )
2598-
{
2599-
matches = FALSE;
2600-
}
2601-
break;
2602-
case HEIGHT:
2603-
if( H != val )
2604-
{
2605-
matches = FALSE;
2606-
}
2607-
break;
2608-
case MAXHEIGHT:
2609-
if( H > val )
2610-
{
2611-
matches = FALSE;
2612-
}
2613-
break;
2614-
case MINHEIGHT:
2615-
if( H < val )
2616-
{
2617-
matches = FALSE;
2618-
}
2619-
break;
2620-
case DEVHEIGHT:
2621-
if( DH != val )
2622-
{
2623-
matches = FALSE;
2624-
}
2625-
break;
2626-
case DEVMAXHEIGHT:
2627-
if( DH > val )
2628-
{
2629-
matches = FALSE;
2630-
}
2631-
break;
2632-
case DEVMINHEIGHT:
2633-
if( DH < val )
2634-
{
2635-
matches = FALSE;
2636-
}
2637-
break;
2638-
default:
2583+
2584+
switch( TRUE )
2585+
{
2586+
case ( prop == ORIENTATION && val == LANDSCAPE && W < H ):
2587+
case ( prop == ORIENTATION && val == PORTRAIT && W > H ):
2588+
case ( prop == WIDTH && W != val ):
2589+
case ( prop == MAXWIDTH && W > val ):
2590+
case ( prop == MINWIDTH && W < val ):
2591+
case ( prop == DEVWIDTH && DW != val ):
2592+
case ( prop == DEVMAXWIDTH && DW > val ):
2593+
case ( prop == DEVMINWIDTH && DW < val ):
2594+
case ( prop == HEIGHT && H != val ):
2595+
case ( prop == MAXHEIGHT && H > val ):
2596+
case ( prop == MINHEIGHT && H < val ):
2597+
case ( prop == DEVHEIGHT && DH != val ):
2598+
case ( prop == DEVMAXHEIGHT && DH > val ):
2599+
case ( prop == DEVMINHEIGHT && DH < val ):
2600+
matches = FALSE;
26392601
break;
26402602
}
26412603
}
2642-
i--;
2643-
};
2644-
2604+
}
26452605
return matches;
2646-
}
2606+
};
26472607
return matchMedia( query );
26482608
}
26492609
}

0 commit comments

Comments
 (0)