This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
css.js
144 lines (113 loc) · 3.13 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
/**
* @license RequireCSS 0.3.1 Copyright (c) 2011, VIISON All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/VIISON/RequireCSS for details
*/
/*jslint forin: true */
/*global document: true, setTimeout: true, define: true */
(function () {
"use strict";
var doc = document,
head = doc.head || doc.getElementsByTagName('head')[0],
// Eliminate browsers that admit to not support the link load event (e.g. Firefox < 9)
nativeLoad = doc.createElement('link').onload === null ? undefined : false,
a = doc.createElement('a');
function createLink(url) {
var link = doc.createElement('link');
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
return link;
}
function styleSheetLoaded(url) {
var i;
// Get absolute url by assigning to a link and reading it back below
a.href = url;
for (i in doc.styleSheets) {
if (doc.styleSheets[i].href === a.href) {
return true;
}
}
return false;
}
/**
* Load using the browsers built-in load event on link tags
*/
function loadLink(url, load) {
var link = createLink(url);
link.onload = function () {
load();
};
head.appendChild(link);
};
/**
* Insert a script tag and use it's onload & onerror to know when
* the CSS is loaded, this will unfortunately also fire on other
* errors (file not found, network problems)
*/
function loadScript(url, load) {
var link = createLink(url),
script = doc.createElement('script');
head.appendChild(link);
script.onload = script.onerror = function () {
head.removeChild(script);
// In Safari the stylesheet might not yet be applied, when
// the script is loaded so we poll document.styleSheets for it
var checkLoaded = function () {
if (styleSheetLoaded(url)) {
load();
return;
}
setTimeout(checkLoaded, 25);
};
checkLoaded();
};
script.src = url;
head.appendChild(script);
};
function loadSwitch(url, load) {
if (nativeLoad) {
loadLink(url, load);
} else {
loadScript(url, load);
}
};
define(function () {
var css;
css = {
version: '0.3.1',
load: function (name, req, load) { //, config (not used)
// convert name to actual url
var url = req.toUrl(
// Append default extension
name.search(/\.(css|less|scss)$/i) === -1 ? name + '.css' : name
);
// Test if the browser supports the link load event,
// in case we don't know yet (mostly WebKit)
if (nativeLoad === undefined) {
// Create a link element with a data url,
// it would fire a load event immediately
var link = createLink('data:text/css,');
link.onload = function () {
// Native link load event works
nativeLoad = true;
};
head.appendChild(link);
// Schedule function in event loop, this will
// execute after a potential execution of the link onload
setTimeout(function () {
head.removeChild(link);
if (nativeLoad !== true) {
// Native link load event is broken
nativeLoad = false;
}
loadSwitch(url, load);
}, 0);
} else {
loadSwitch(url, load);
}
}
};
return css;
});
}());