css.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. define("dojox/data/css", ["dojo/_base/lang", "dojo/_base/array"],
  2. function(lang, array) {
  3. var css = lang.getObject("dojox.data.css",true)
  4. css.rules = {};
  5. css.rules.forEach = function(fn,ctx,context){
  6. if(context){
  7. var _processSS = function(styleSheet){
  8. //iterate across rules in the stylesheet
  9. array.forEach(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
  10. if(!rule.type || rule.type !== 3){// apply fn to current rule with approp ctx. rule is arg (all browsers)
  11. var href = "";
  12. if(styleSheet && styleSheet.href){
  13. href = styleSheet.href;
  14. }
  15. fn.call(ctx?ctx:this,rule, styleSheet, href);
  16. }
  17. });
  18. //process any child stylesheets
  19. };
  20. array.forEach(context,_processSS);
  21. }
  22. };
  23. css.findStyleSheets = function(sheets){
  24. // Takes an array of stylesheet paths and finds the currently loaded StyleSheet objects matching
  25. // those names
  26. var sheetObjects = [];
  27. var _processSS = function(styleSheet){
  28. var s = css.findStyleSheet(styleSheet);
  29. if(s){
  30. array.forEach(s, function(sheet){
  31. if(array.indexOf(sheetObjects, sheet) === -1){
  32. sheetObjects.push(sheet);
  33. }
  34. });
  35. }
  36. };
  37. array.forEach(sheets, _processSS);
  38. return sheetObjects;
  39. };
  40. css.findStyleSheet = function(sheet){
  41. // Takes a stylesheet path and finds the currently loaded StyleSheet objects matching
  42. // those names (and it's parent(s), if it is imported from another)
  43. var sheetObjects = [];
  44. if(sheet.charAt(0) === '.'){
  45. sheet = sheet.substring(1);
  46. }
  47. var _processSS = function(styleSheet){
  48. if(styleSheet.href && styleSheet.href.match(sheet)){
  49. sheetObjects.push(styleSheet);
  50. return true;
  51. }
  52. if(styleSheet.imports){
  53. return array.some(styleSheet.imports, function(importedSS){ //IE stylesheet has imports[] containing @import'ed rules
  54. //console.debug("Processing IE @import rule",importedSS);
  55. return _processSS(importedSS);
  56. });
  57. }
  58. //iterate across rules in the stylesheet
  59. return array.some(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
  60. if(rule.type && rule.type === 3 && _processSS(rule.styleSheet)){// CSSImportRule (firefox)
  61. //sheetObjects.push(styleSheet);
  62. return true;
  63. }
  64. return false;
  65. });
  66. };
  67. array.some(document.styleSheets, _processSS);
  68. return sheetObjects;
  69. };
  70. css.determineContext = function(initialStylesheets){
  71. // Takes an array of stylesheet paths and returns an array of all stylesheets that fall in the
  72. // given context. If no paths are given, all stylesheets are returned.
  73. var ret = [];
  74. if(initialStylesheets && initialStylesheets.length > 0){
  75. initialStylesheets = css.findStyleSheets(initialStylesheets);
  76. }else{
  77. initialStylesheets = document.styleSheets;
  78. }
  79. var _processSS = function(styleSheet){
  80. ret.push(styleSheet);
  81. if(styleSheet.imports){
  82. array.forEach(styleSheet.imports, function(importedSS){ //IE stylesheet has imports[] containing @import'ed rules
  83. //console.debug("Processing IE @import rule",importedSS);
  84. _processSS(importedSS);
  85. });
  86. }
  87. //iterate across rules in the stylesheet
  88. array.forEach(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
  89. if(rule.type && rule.type === 3){// CSSImportRule (firefox)
  90. _processSS(rule.styleSheet);
  91. }
  92. });
  93. };
  94. array.forEach(initialStylesheets,_processSS);
  95. return ret;
  96. };
  97. return css;
  98. });