css.js 3.6 KB

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