forked from guybedford/require-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.js
284 lines (238 loc) · 8.2 KB
/
css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* css! loader plugin
* Allows for loading stylesheets with the 'css!' syntax.
*
* in Chrome 19+, IE10+, Firefox 9+, Safari 6+ <link> tags are used with onload support
* in all other environments, <style> tags are used with injection to mimic onload support
*
* <link> tag can be enforced with the configuration useLinks, although support cannot be guaranteed.
*
* External stylesheets loaded with link tags, unless useLinks explicitly set to false assuming CORS support.
*
* Stylesheet parsers always use <style> injection even on external urls, which may cause origin issues.
*
*/
define(['./normalize', 'module'], function(normalize, module) {
if (typeof window == 'undefined')
return { load: function(n, r, load){ load() } };
var head = document.getElementsByTagName('head')[0];
var agentMatch = window.navigator.userAgent.match(/Chrome\/([^ \.]*)|MSIE ([^ ;]*)|Firefox\/([^ ;]*)|Version\/([\d\.]*) Safari\//);
var useLinks, browserEngine;
if (window.opera) {
browserEngine = 'opera';
useLinks = true;
}
if (agentMatch) {
if (agentMatch[4])
browserEngine = 'webkit'
if (agentMatch[3])
browserEngine = 'mozilla';
else if (agentMatch[2])
browserEngine = 'ie';
else if (agentMatch[1])
browserEngine = 'webkit';
useLinks = (browserEngine && (parseInt(agentMatch[4]) > 5 || parseInt(agentMatch[3]) > 8 || parseInt(agentMatch[2]) > 9 || parseInt(agentMatch[1]) > 18)) || undefined;
}
var config = module.config();
if (config && config.useLinks !== undefined)
useLinks = config.useLinks;
/* XHR code - copied from RequireJS text plugin */
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
var fileCache = {};
var get = function(url, callback, errback) {
if (fileCache[url]) {
callback(fileCache[url]);
return;
}
var xhr, i, progId;
if (typeof XMLHttpRequest !== 'undefined')
xhr = new XMLHttpRequest();
else if (typeof ActiveXObject !== 'undefined')
for (i = 0; i < 3; i += 1) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
}
catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
xhr.open('GET', url, requirejs.inlineRequire ? false : true);
xhr.onreadystatechange = function (evt) {
var status, err;
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
status = xhr.status;
if (status > 399 && status < 600) {
//An http 4xx or 5xx error. Signal an error.
err = new Error(url + ' HTTP status: ' + status);
err.xhr = xhr;
errback(err);
}
else {
fileCache[url] = xhr.responseText;
callback(xhr.responseText);
}
}
};
xhr.send(null);
}
//main api object
var cssAPI = {};
cssAPI.pluginBuilder = './css-builder';
//uses the <style> load method
var stylesheet = document.createElement('style');
stylesheet.type = 'text/css';
head.appendChild(stylesheet);
if (stylesheet.styleSheet)
cssAPI.inject = function(css) {
stylesheet.styleSheet.cssText += css;
}
else
cssAPI.inject = function(css) {
stylesheet.appendChild(document.createTextNode(css));
}
var webkitLoadCheck = function(link, callback) {
setTimeout(function() {
for (var i = 0; i < document.styleSheets.length; i++) {
var sheet = document.styleSheets[i];
if (sheet.href == link.href)
return callback();
}
webkitLoadCheck(link, callback);
}, 10);
}
var mozillaLoadCheck = function(style, callback) {
setTimeout(function() {
try {
style.sheet.cssRules;
return callback();
} catch (e){}
mozillaLoadCheck(style, callback);
}, 10);
}
// uses the <link> load method
var createLink = function(url) {
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
return link;
}
cssAPI.linkLoad = function(url, callback) {
var timeout = setTimeout(callback, waitSeconds * 1000 - 100);
var _callback = function() {
clearTimeout(timeout);
callback();
}
if (browserEngine == 'webkit') {
var link = createLink(url);
webkitLoadCheck(link, _callback);
head.appendChild(link);
}
// onload support only in firefox 18+
else if (browserEngine == 'mozilla' && parseInt(agentMatch[3]) < 18) {
var style = document.createElement('style');
style.textContent = '@import "' + url + '"';
mozillaLoadCheck(style, _callback);
head.appendChild(style);
}
else {
var link = createLink(url);
link.onload = _callback;
head.appendChild(link);
}
}
cssAPI.inspect = function() {
if (stylesheet.styleSheet)
return stylesheet.styleSheet.cssText;
else if (stylesheet.innerHTML)
return stylesheet.innerHTML;
}
cssAPI.normalize = function(name, normalize) {
if (name.substr(name.length - 4, 4) == '.css')
name = name.substr(0, name.length - 4);
return normalize(name);
}
// NB add @media query support for media imports
var importRegEx = /@import\s*(url)?\s*(('([^']*)'|"([^"]*)")|\(('([^']*)'|"([^"]*)"|([^\)]*))\))\s*;?/g;
var pathname = window.location.pathname.split('/');
pathname.pop();
pathname = pathname.join('/') + '/';
var loadCSS = function(fileUrl, callback, errback) {
//make file url absolute
if (fileUrl.substr(0, 1) != '/')
fileUrl = '/' + normalize.convertURIBase(fileUrl, pathname, '/');
get(fileUrl, function(css) {
// normalize the css (except import statements)
css = normalize(css, fileUrl, pathname);
// detect all import statements in the css and normalize
var importUrls = [];
var importIndex = [];
var importLength = [];
var match;
while (match = importRegEx.exec(css)) {
var importUrl = match[4] || match[5] || match[7] || match[8] || match[9];
// add less extension if necessary
if (importUrl.indexOf('.less') === -1)
importUrl += '.less';
importUrls.push(importUrl);
importIndex.push(importRegEx.lastIndex - match[0].length);
importLength.push(match[0].length);
}
// load the import stylesheets and substitute into the css
var completeCnt = 0;
for (var i = 0; i < importUrls.length; i++)
(function(i) {
loadCSS(importUrls[i], function(importCSS) {
css = css.substr(0, importIndex[i]) + importCSS + css.substr(importIndex[i] + importLength[i]);
var lenDiff = importCSS.length - importLength[i];
for (var j = i + 1; j < importUrls.length; j++)
importIndex[j] += lenDiff;
completeCnt++;
if (completeCnt == importUrls.length) {
callback(css);
}
}, errback);
})(i);
if (importUrls.length == 0)
callback(css);
}, errback);
}
var waitSeconds;
cssAPI.load = function(cssId, req, load, config, parse) {
waitSeconds = waitSeconds || config.waitSeconds || 7;
var fileUrl = cssId;
if (fileUrl.substr(fileUrl.length - 4, 4) != '.css' && !parse)
fileUrl += '.css';
fileUrl = req.toUrl(fileUrl);
// determine if it is the same domain or not
var sameDomain = true,
domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(fileUrl);
if (domainCheck) {
sameDomain = domainCheck[2] === window.location.host;
if (domainCheck[1])
sameDomain &= domainCheck[1] === window.location.protocol;
}
//external url -> add as a <link> tag to load
if (!parse && useLinks !== false && (!sameDomain || useLinks)) {
cssAPI.linkLoad(fileUrl, function() {
load(cssAPI);
});
}
//internal url or parsing -> inject into <style> tag
else {
loadCSS(fileUrl, function(css) {
// run parsing last - since less is a CSS subset this works fine
if (parse)
css = parse(css);
cssAPI.inject(css);
load(cssAPI);
}, load.error);
}
}
return cssAPI;
});