Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit de18d0d

Browse files
Merge pull request #42 from danReynolds/ImportSupport
Import support
2 parents be4731f + bd54f7a commit de18d0d

File tree

10 files changed

+121
-50
lines changed

10 files changed

+121
-50
lines changed

css/general.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ html {
77
line-height: 1.6;
88
}
99
.test {
10-
background-color:blue;
10+
background-color:black;
1111
color: white;
12-
width: 10em;
13-
height: 5rem;
12+
width: 5rem;
13+
height: 5em;
1414
}
1515
p {font-size: 5rem;}
1616

@@ -23,4 +23,4 @@ p {font-size: 5rem;}
2323
font-size: 10rem;
2424
}
2525
.test {background-color: green;}
26-
}
26+
}

css/importee1.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.importee1 {
2+
background-color:red;
3+
color: white;
4+
width: 15rem;
5+
height: 5em;
6+
}

css/importee2.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.importee2 {
2+
background-color:yellow;
3+
color: white;
4+
width: 25rem;
5+
height: 5em;
6+
}

css/importer.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*Basic Test*/
2+
@import url("importee1.css");
3+
/*Folder Test*/
4+
@import 'nested_imports/nested_importer.css';
5+
6+
.importer {
7+
background-color: brown;
8+
color: white;
9+
width: 10rem;
10+
height: 5em;
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.nested-importee1 {
2+
background-color:green;
3+
color: white;
4+
width: 30rem;
5+
height: 5em;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.nested-importee2 {
2+
background-color:blue;
3+
color: white;
4+
width: 35rem;
5+
height: 5em;
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*Parent Directory and Same Line Test */
2+
@import url("../importee2.css"); @import url(nested_importee1.css);
3+
4+
/*No URL test*/
5+
@import "nested_importee2.css";
6+
7+
.nested-importer {
8+
background-color:orange;
9+
color: white;
10+
width: 20rem;
11+
height: 5em;
12+
}

index.html

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
<!DOCTYPE HTML>
22
<html>
33
<head>
4-
<title>REM Unit polyfill</title>
5-
<!-- adding reference to Zurb Foundation for testing with larger file
4+
<title>REM Unit polyfill</title>
5+
<!-- adding reference to Zurb Foundation for testing with larger file
66
that has REM values in it as well
77
-->
8-
<link rel="stylesheet" type="text/css" href="css/foundation.css" />
8+
<link rel="stylesheet" type="text/css" href="css/foundation.css" />
9+
<!-- Standard CSS with some REM values, media queries are ignored -->
10+
<link rel="stylesheet" type="text/css" href="css/general.css" />
11+
<!-- CSS containing @import lines, test importing of additional sheets -->
12+
<link rel="stylesheet" type="text/css" href="css/importer.css" />
13+
<!-- ignore.css shows in normal browsers, but ones where we expect the REM.js to
14+
run then the REM rules should not get parsed.
15+
-->
16+
<link rel="stylesheet" type="text/css" href="css/ignore.css" data-norem />
917

10-
<!-- Standard CSS with some REM values, media queries are ignored -->
11-
<link rel="stylesheet" type="text/css" href="css/general.css" />
12-
<!-- ignore.css shows in normal browsers, but ones where we expect the REM.js to
13-
run then the REM rules should not get parsed.
14-
-->
15-
<link rel="stylesheet" type="text/css" href="css/ignore.css" data-norem />
1618
</head>
1719
<body>
18-
<!-- In a REM supported browser, you see a green box and very small text.
19-
In an unsupport browser, but successful process should show a
20-
blue box and large paragraph text outside it.
21-
-->
22-
<div class="test">Hello World!</div>
23-
<p>This thing works.</p>
24-
<script src="js/rem.min.js" type="text/javascript"></script>
20+
<div class="test">Hello World!</div>
21+
<div class="importer">Importer Test</div>
22+
<div class="importee1">Basic Test</div>
23+
<div class="nested-importer">Folder Test</div>
24+
<div class="importee2">Parent Directory Test</div>
25+
<div class="nested-importee1">Same Line Test</div>
26+
<div class="nested-importee2">No URL Test</div>
27+
<p>This thing works.</p>
28+
<script src="js/rem.js" type="text/javascript"></script>
2529
</body>
26-
</html>
30+
</html>

js/rem.js

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,67 @@
88
return (/rem/).test(div.style.fontSize);
99
},
1010

11-
// filter returned link nodes for stylesheets
11+
// filter returned links for stylesheets
1212
isStyleSheet = function () {
1313
var styles = document.getElementsByTagName('link'),
14-
filteredStyles = [];
14+
filteredLinks = [];
1515

1616
for ( var i = 0; i < styles.length; i++) {
1717
if ( styles[i].rel.toLowerCase() === 'stylesheet' && styles[i].getAttribute('data-norem') === null ) {
18-
filteredStyles.push( styles[i] );
18+
19+
filteredLinks.push( styles[i].href );
1920
}
2021
}
2122

22-
return filteredStyles;
23+
return filteredLinks;
2324
},
2425

25-
processSheets = function () {
26-
var links = [];
27-
sheets = isStyleSheet(); // search for link tags and confirm it's a stylesheet
28-
sheets.og = sheets.length; // store the original length of sheets as a property
29-
sheets.loaded = 0; // keep track of how many have been loaded so far
30-
for( var i = 0; i < sheets.length; i++ ){
31-
links[i] = sheets[i].href;
32-
xhr( links[i], storeCSS, i );
26+
processLinks = function () {
27+
if( links.length === 0 ){
28+
links = isStyleSheet(); // search for link tags and confirm it's a stylesheet
29+
}
30+
31+
//prepare to match each link
32+
for( var i = 0; i < links.length; i++ ){
33+
xhr( links[i], storeCSS, links[i], i );
3334
}
3435
},
3536

36-
storeCSS = function ( response, i ) {
37+
storeCSS = function ( response, link ) {
3738

38-
preCSS[i] = response;
39+
preCSS.push(response.responseText);
40+
CSSLinks.push(link);
3941

40-
if( ++sheets.loaded === sheets.og ){
41-
for ( var j = 0; j < preCSS.length; j++ ){
42-
matchCSS( preCSS[j] );
42+
if( CSSLinks.length === links.length ){
43+
for( var j = 0; j < CSSLinks.length; j++ ){
44+
matchCSS( preCSS[j], CSSLinks[j] );
4345
}
4446

45-
buildCSS();
47+
if( ( links = importLinks.slice(0) ).length > 0 ){ //after finishing all current links, set links equal to the new imports found
48+
CSSLinks = [];
49+
preCSS = [];
50+
importLinks = [];
51+
processLinks();
52+
} else {
53+
buildCSS();
54+
}
4655
}
47-
4856
},
49-
50-
matchCSS = function ( response ) { // collect all of the rules from the xhr response texts and match them to a pattern
51-
var clean = removeComments( removeMediaQueries(response.responseText) ),
57+
58+
matchCSS = function ( sheetCSS, link ) { // collect all of the rules from the xhr response texts and match them to a pattern
59+
var clean = removeComments( removeMediaQueries(sheetCSS) ),
5260
pattern = /[\w\d\s\-\/\\\[\]:,.'"*()<>+~%#^$_=|@]+\{[\w\d\s\-\/\\%#:;,.'"*()]+\d*\.?\d+rem[\w\d\s\-\/\\%#:;,.'"*()]*\}/g, //find selectors that use rem in one or more of their rules
5361
current = clean.match(pattern),
5462
remPattern =/\d*\.?\d+rem/g,
55-
remCurrent = clean.match(remPattern);
63+
remCurrent = clean.match(remPattern),
64+
sheetPathPattern = /(.*\/)/,
65+
sheetPath = sheetPathPattern.exec(link)[0], //relative path to css file specified in @import
66+
importPattern = /@import (?:url\()?['"]?([^'\)"]*)['"]?\)?[^;]*/gm, //matches all @import variations outlined at: https://developer.mozilla.org/en-US/docs/Web/CSS/@import
67+
importStatement;
68+
69+
while( (importStatement = importPattern.exec(sheetCSS)) !== null ){
70+
importLinks.push( sheetPath + importStatement[1] );
71+
}
5672

5773
if( current !== null && current.length !== 0 ){
5874
found = found.concat( current ); // save all of the blocks of rules with rem in a property
@@ -77,7 +93,7 @@
7793
},
7894

7995
parseCSS = function () { // replace each set of parentheses with evaluated content
80-
var remSize;
96+
var remSize;
8197
for( var i = 0; i < foundProps.length; i++ ){
8298
remSize = parseFloat(foundProps[i].substr(0,foundProps[i].length-3));
8399
css[i] = Math.round( remSize * fontSize ) + 'px';
@@ -104,6 +120,7 @@
104120
},
105121

106122
xhr = function ( url, callback, i ) { // create new XMLHttpRequest object and run it
123+
107124
try {
108125
var xhr = getXMLHttpRequest();
109126
xhr.open( 'GET', url, true );
@@ -163,7 +180,7 @@
163180
}
164181
},
165182

166-
// Test for Media Query support
183+
// Test for Media Query support
167184
mediaQuery = function() {
168185
if (window.matchMedia || window.msMatchMedia) { return true; }
169186
return false;
@@ -177,7 +194,7 @@
177194
css = css.replace(/@media[\s\S]*?\}\s*\}/, "");
178195
}
179196

180-
return css;
197+
return css;
181198
},
182199

183200
getXMLHttpRequest = function () { // we're gonna check if our browser will let us use AJAX
@@ -198,13 +215,16 @@
198215

199216
if( !cssremunit() ){ // this checks if the rem value is supported
200217
var rules = '', // initialize the rules variable in this scope so it can be used later
201-
sheets = [], // initialize the array holding the sheets for use later
218+
links = [], // initialize the array holding the sheets urls for use later
219+
importLinks = [], //initialize the array holding the import sheet urls for use later
202220
found = [], // initialize the array holding the found rules for use later
203221
foundProps = [], // initialize the array holding the found properties for use later
204222
preCSS = [], // initialize array that holds css before being parsed
223+
CSSLinks = [], //initialize array holding css links returned from xhr
205224
css = [], // initialize the array holding the parsed rules for use later
206225
body = document.getElementsByTagName('body')[0],
207226
fontSize = '';
227+
208228
if (body.currentStyle) {
209229
if ( body.currentStyle.fontSize.indexOf("px") >= 0 ) {
210230
fontSize = body.currentStyle.fontSize.replace('px', '');
@@ -218,7 +238,7 @@
218238
} else if (window.getComputedStyle) {
219239
fontSize = document.defaultView.getComputedStyle(body, null).getPropertyValue('font-size').replace('px', ''); // find font-size in body element
220240
}
221-
processSheets();
241+
processLinks();
222242
} // else { do nothing, you are awesome and have REM support }
223243

224244
})(window);

js/rem.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)