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

Commit 4352af7

Browse files
added eCSStender::matchMedia() method
added eCSStender::emptyStyleSheet() method added eCSStender::elementMatchesSelector() method added eCSStender::getPathTo() method
1 parent cea69a2 commit 4352af7

File tree

2 files changed

+129
-16
lines changed

2 files changed

+129
-16
lines changed

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
*v1.2.7 (2011-07-14)
1+
*v1.2.7 (2011-07-23)
22
* added eCSStender::matchMedia() method
3+
* added eCSStender::emptyStyleSheet() method
4+
* added eCSStender::elementMatchesSelector() method
5+
* added eCSStender::getPathTo() method
36

47
*v1.2.6.6* (2010-08-26)
58
* bugfix: don't call determinePath() on embedded stylesheets

src/eCSStender.js

Lines changed: 125 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ License: MIT License (see homepage)
5959
CLOSE_CURLY = '}',
6060
COMMA = ',',
6161
DIV = 'div',
62+
SCRIPT = 'script',
63+
SRC = 'src',
6264
TYPE = 'type',
6365
COMPLETE = 'complete',
6466
BODY = 'body',
@@ -131,7 +133,7 @@ License: MIT License (see homepage)
131133
clearBrowserCache = EMPTY_FN,
132134

133135
// other stuff
134-
__script = newElement( 'script' ),
136+
__script = newElement( SCRIPT ),
135137

136138
// useful RegExps
137139
// for breaking on commas
@@ -2056,34 +2058,68 @@ License: MIT License (see homepage)
20562058
}
20572059
eCSStender.addRules = addRules;
20582060

2061+
/**
2062+
* eCSStender::emptyStyleSheets()
2063+
* clears the contents of embedded stylesheets
2064+
*
2065+
* @return null
2066+
*/
2067+
function emptyStyleSheets()
2068+
{
2069+
if ( defined( stylesheet.styleSheet ) )
2070+
{
2071+
emptyStyleSheets = function()
2072+
{
2073+
var i = arguments.length;
2074+
while ( i-- )
2075+
{
2076+
arguments[i].styleSheet.cssText = EMPTY;
2077+
}
2078+
};
2079+
}
2080+
else
2081+
{
2082+
emptyStyleSheets = function()
2083+
{
2084+
var i = arguments.length;
2085+
while ( i-- )
2086+
{
2087+
arguments[i].innerHTML = EMPTY;
2088+
}
2089+
};
2090+
}
2091+
emptyStyleSheets.apply( NULL, arguments );
2092+
}
2093+
eCSStender.emptyStyleSheets = emptyStyleSheets;
2094+
20592095
/**
20602096
* eCSStender::isSupported()
20612097
* tests support for properties and selectors
20622098
*
20632099
* Option 1: Selector test
20642100
* eCSStender::isSupported( type, selector, html, el )
2065-
* @param str type - 'property'
2101+
* @param str type - 'selector'
20662102
* @param str selector - the selector
20672103
* @param obj html - HTML to test against
20682104
* @param obj el - the element the selector should select
20692105
* @return bool - TRUE for success, FALSE for failure
20702106
*
20712107
* Option 2: Property Test (simple)
20722108
* eCSStender::isSupported( type, test )
2073-
* @param str type - 'selector'
2109+
* @param str type - 'property'
20742110
* @param str test - the property: value pair to test
20752111
* @return bool - TRUE for success, FALSE for failure
20762112
*
20772113
* Option 3: Property Test (complex)
20782114
* eCSStender::isSupported( type, property, value )
2079-
* @param str type - 'selector'
2115+
* @param str type - 'property'
20802116
* @param str property - the property to test
20812117
* @param mixed value - the string value or an array of possible values
20822118
* @return bool - TRUE for success, FALSE for failure
20832119
*
20842120
* Option 3: Storage
20852121
* eCSStender::isSupported( type, what, result )
2086-
* @param str type - 'selector'
2122+
* @param str type - 'selector' or 'property'
20872123
* @param str what - the key to store
20882124
* @param bool result - the result of the test you want stored
20892125
* @return bool - the result you passed in
@@ -2263,6 +2299,33 @@ License: MIT License (see homepage)
22632299
return str;
22642300
}
22652301
eCSStender.trim = trim;
2302+
2303+
/**
2304+
* eCSStender::getPathTo()
2305+
* finds the path to a given resource in the document (scripts by default)
2306+
*
2307+
* @param str resource - the filename you're looking for
2308+
* @param str tag - the tag family you're searching in
2309+
* @return str - the complete resource path
2310+
*/
2311+
eCSStender.getPathTo = function( resource, tag )
2312+
{
2313+
tag = tag || SCRIPT;
2314+
var
2315+
regex = new RegExp( resource ),
2316+
attr = tag == 'link' ? 'href' : SRC,
2317+
collection = getElements( tag ),
2318+
i = collection.length,
2319+
value;
2320+
while ( i-- )
2321+
{
2322+
value = collection[i].getAttribute( attr );
2323+
if ( regex.test( value ) )
2324+
{
2325+
return value.replace( regex, EMPTY );
2326+
}
2327+
}
2328+
};
22662329

22672330
/**
22682331
* eCSStender::loadScript()
@@ -2275,7 +2338,7 @@ License: MIT License (see homepage)
22752338
eCSStender.loadScript = function( src, callback )
22762339
{
22772340
var
2278-
scripts = DOCUMENT.getElementsByTagName('script'),
2341+
scripts = DOCUMENT.getElementsByTagName(SCRIPT),
22792342
i = scripts.length,
22802343
script = __script.cloneNode( TRUE ),
22812344
loaded = FALSE;
@@ -2300,7 +2363,7 @@ License: MIT License (see homepage)
23002363
callback();
23012364
}
23022365
};
2303-
script.setAttribute( 'src', src );
2366+
script.setAttribute( SRC, src );
23042367
__head.appendChild( script );
23052368
}
23062369
else
@@ -2377,9 +2440,10 @@ License: MIT License (see homepage)
23772440
return FALSE;
23782441
};
23792442
}
2380-
eCSStender.getCSSValue = getCSSValue;
23812443
return getCSSValue( el, prop );
23822444
}
2445+
eCSStender.getCSSValue = getCSSValue;
2446+
23832447
/**
23842448
* eCSStender::makeUniqueClass()
23852449
* creates a unique class for an element
@@ -2465,6 +2529,53 @@ License: MIT License (see homepage)
24652529
}
24662530
};
24672531
eCSStender.toggleClass = toggleClass;
2532+
/**
2533+
* eCSStender::elementMatchesSelector()
2534+
* checks to see if a given element matches the selector you've passed to it
2535+
*
2536+
* @return bool
2537+
*/
2538+
eCSStender.elementMatchesSelector = function( element, selector )
2539+
{
2540+
if ( defined( element.matchesSelector ) )
2541+
{
2542+
elementMatchesSelector = function( element, selector )
2543+
{
2544+
return element.matchesSelector( selector );
2545+
};
2546+
}
2547+
else if ( defined( element.mozMatchesSelector ) )
2548+
{
2549+
elementMatchesSelector = function( element, selector )
2550+
{
2551+
return element.mozMatchesSelector( selector );
2552+
};
2553+
}
2554+
else if ( defined( element.webkitMatchesSelector ) )
2555+
{
2556+
elementMatchesSelector = function( element, selector )
2557+
{
2558+
return element.webkitMatchesSelector( selector );
2559+
};
2560+
}
2561+
else
2562+
{
2563+
var
2564+
testStyleSheet = e.newStyleElement(SCREEN,'selector-matching-test',FALSE);
2565+
elementMatchesSelector = function( element, selector )
2566+
{
2567+
var
2568+
property = 'page-break-after',
2569+
value = 'avoid',
2570+
ret;
2571+
e.addRules( testStyleSheet, selector + OPEN_CURLY + property + COLON + value + SEMICOLON + CLOSE_CURLY );
2572+
ret = ( e.getCSSValue( element, property ) == value );
2573+
emptyStyleSheet( testStyleSheet );
2574+
return ret;
2575+
};
2576+
}
2577+
return elementMatchesSelector( element, selector );
2578+
};
24682579
/**
24692580
* eCSStender.matchMedia()
24702581
* returns true if the media query matches the state of rendered document
@@ -2489,7 +2600,7 @@ License: MIT License (see homepage)
24892600
{
24902601
var
24912602
number = parseInt(val, 10),
2492-
unit = val.replace(number, '');
2603+
unit = val.replace(number, EMPTY);
24932604
switch(unit) {
24942605
case PX:
24952606
break;
@@ -2506,10 +2617,9 @@ License: MIT License (see homepage)
25062617
getHeight;
25072618
getWidth = function()
25082619
{
2509-
var _body = getElements(BODY)[0];
2510-
return _body.clientWidth +
2511-
convertToPixels( getCSSValue( _body, 'margin-left' ) ) +
2512-
convertToPixels( getCSSValue( _body, 'margin-right' ) );
2620+
return __body.clientWidth +
2621+
convertToPixels( getCSSValue( __body, 'margin-left' ) ) +
2622+
convertToPixels( getCSSValue( __body, 'margin-right' ) );
25132623
};
25142624
if ( defined( WINDOW.innerHeight ) )
25152625
{
@@ -2531,7 +2641,7 @@ License: MIT License (see homepage)
25312641
{
25322642
getHeight = function()
25332643
{
2534-
return getElements(BODY)[0].clientHeight;
2644+
return __body.clientHeight;
25352645
};
25362646
}
25372647
/* Method */
@@ -2570,7 +2680,7 @@ License: MIT License (see homepage)
25702680
if ( mediaQueryRegex.test(q) )
25712681
{
25722682
q = q.split(COLON);
2573-
prop = q[0].toLowerCase();
2683+
prop = low( q[0] );
25742684
val = q[1];
25752685

25762686
prop = prop.replace(/^\(/, EMPTY);

0 commit comments

Comments
 (0)