You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

174 lines
4.1 KiB

  1. /*
  2. * Require-CSS RequireJS css! loader plugin
  3. * 0.1.8
  4. * Guy Bedford 2014
  5. * MIT
  6. */
  7. /*
  8. *
  9. * Usage:
  10. * require(['css!./mycssFile']);
  11. *
  12. * Tested and working in (up to latest versions as of March 2013):
  13. * Android
  14. * iOS 6
  15. * IE 6 - 10
  16. * Chome 3 - 26
  17. * Firefox 3.5 - 19
  18. * Opera 10 - 12
  19. *
  20. * browserling.com used for virtual testing environment
  21. *
  22. * Credit to B Cavalier & J Hann for the IE 6 - 9 method,
  23. * refined with help from Martin Cermak
  24. *
  25. * Sources that helped along the way:
  26. * - https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent
  27. * - http://www.phpied.com/when-is-a-stylesheet-really-loaded/
  28. * - https://github.com/cujojs/curl/blob/master/src/curl/plugin/css.js
  29. *
  30. */
  31. define(function() {
  32. //>>excludeStart('excludeRequireCss', pragmas.excludeRequireCss)
  33. if (typeof window == 'undefined')
  34. return {
  35. load: function(n, r, load) {
  36. load()
  37. }
  38. };
  39. var head = document.getElementsByTagName('head')[0];
  40. var engine = window.navigator.userAgent.match(/Trident\/([^ ;]*)|AppleWebKit\/([^ ;]*)|Opera\/([^ ;]*)|rv\:([^ ;]*)(.*?)Gecko\/([^ ;]*)|MSIE\s([^ ;]*)|AndroidWebKit\/([^ ;]*)/) || 0;
  41. // use <style> @import load method (IE < 9, Firefox < 18)
  42. var useImportLoad = false;
  43. // set to false for explicit <link> load checking when onload doesn't work perfectly (webkit)
  44. var useOnload = true;
  45. // trident / msie
  46. if (engine[1] || engine[7])
  47. useImportLoad = parseInt(engine[1]) < 6 || parseInt(engine[7]) <= 9;
  48. // webkit
  49. else if (engine[2] || engine[8])
  50. useOnload = false;
  51. // gecko
  52. else if (engine[4])
  53. useImportLoad = parseInt(engine[4]) < 18;
  54. //>>excludeEnd('excludeRequireCss')
  55. //main api object
  56. var cssAPI = {};
  57. //>>excludeStart('excludeRequireCss', pragmas.excludeRequireCss)
  58. cssAPI.pluginBuilder = './css-builder';
  59. // <style> @import load method
  60. var curStyle, curSheet;
  61. var createStyle = function() {
  62. curStyle = document.createElement('style');
  63. head.appendChild(curStyle);
  64. curSheet = curStyle.styleSheet || curStyle.sheet;
  65. }
  66. var ieCnt = 0;
  67. var ieLoads = [];
  68. var ieCurCallback;
  69. var createIeLoad = function(url) {
  70. curSheet.addImport(url);
  71. curStyle.onload = function() {
  72. processIeLoad()
  73. };
  74. ieCnt++;
  75. if (ieCnt == 31) {
  76. createStyle();
  77. ieCnt = 0;
  78. }
  79. }
  80. var processIeLoad = function() {
  81. ieCurCallback();
  82. var nextLoad = ieLoads.shift();
  83. if (!nextLoad) {
  84. ieCurCallback = null;
  85. return;
  86. }
  87. ieCurCallback = nextLoad[1];
  88. createIeLoad(nextLoad[0]);
  89. }
  90. var importLoad = function(url, callback) {
  91. if (!curSheet || !curSheet.addImport)
  92. createStyle();
  93. if (curSheet && curSheet.addImport) {
  94. // old IE
  95. if (ieCurCallback) {
  96. ieLoads.push([url, callback]);
  97. } else {
  98. createIeLoad(url);
  99. ieCurCallback = callback;
  100. }
  101. } else {
  102. // old Firefox
  103. curStyle.textContent = '@import "' + url + '";';
  104. var loadInterval = setInterval(function() {
  105. try {
  106. curStyle.sheet.cssRules;
  107. clearInterval(loadInterval);
  108. callback();
  109. } catch (e) {}
  110. }, 10);
  111. }
  112. }
  113. // <link> load method
  114. var linkLoad = function(url, callback) {
  115. var link = document.createElement('link');
  116. link.type = 'text/css';
  117. link.rel = 'stylesheet';
  118. if (useOnload) {
  119. link.onload = function() {
  120. link.onload = function() {};
  121. // for style dimensions queries, a short delay can still be necessary
  122. setTimeout(callback, 7);
  123. }
  124. } else {
  125. var loadInterval = setInterval(function() {
  126. for (var i = 0; i < document.styleSheets.length; i++) {
  127. var sheet = document.styleSheets[i];
  128. if (sheet.href == link.href) {
  129. clearInterval(loadInterval);
  130. return callback();
  131. }
  132. }
  133. }, 10);
  134. }
  135. link.href = url;
  136. head.appendChild(link);
  137. }
  138. //>>excludeEnd('excludeRequireCss')
  139. cssAPI.normalize = function(name, normalize) {
  140. if (name.substr(name.length - 4, 4) == '.css')
  141. name = name.substr(0, name.length - 4);
  142. return normalize(name);
  143. }
  144. //>>excludeStart('excludeRequireCss', pragmas.excludeRequireCss)
  145. cssAPI.load = function(cssId, req, load, config) {
  146. (useImportLoad ? importLoad : linkLoad)(req.toUrl(cssId + '.css'), load);
  147. }
  148. //>>excludeEnd('excludeRequireCss')
  149. return cssAPI;
  150. });