StyleSheet.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Generated by CoffeeScript 1.9.3
  2. var Rule, StyleSheet;
  3. Rule = require('./Rule');
  4. module.exports = StyleSheet = (function() {
  5. var self;
  6. self = StyleSheet;
  7. function StyleSheet() {
  8. this._rulesBySelector = {};
  9. }
  10. StyleSheet.prototype.setRule = function(selector, styles) {
  11. var key, val;
  12. if (typeof selector === 'string') {
  13. this._setRule(selector, styles);
  14. } else if (typeof selector === 'object') {
  15. for (key in selector) {
  16. val = selector[key];
  17. this._setRule(key, val);
  18. }
  19. }
  20. return this;
  21. };
  22. StyleSheet.prototype._setRule = function(s, styles) {
  23. var i, len, ref, selector;
  24. ref = self.splitSelectors(s);
  25. for (i = 0, len = ref.length; i < len; i++) {
  26. selector = ref[i];
  27. this._setSingleRule(selector, styles);
  28. }
  29. return this;
  30. };
  31. StyleSheet.prototype._setSingleRule = function(s, styles) {
  32. var rule, selector;
  33. selector = self.normalizeSelector(s);
  34. if (!(rule = this._rulesBySelector[selector])) {
  35. rule = new Rule(selector);
  36. this._rulesBySelector[selector] = rule;
  37. }
  38. rule.setStyles(styles);
  39. return this;
  40. };
  41. StyleSheet.prototype.getRulesFor = function(el) {
  42. var ref, rule, rules, selector;
  43. rules = [];
  44. ref = this._rulesBySelector;
  45. for (selector in ref) {
  46. rule = ref[selector];
  47. if (rule.selector.matches(el)) {
  48. rules.push(rule);
  49. }
  50. }
  51. return rules;
  52. };
  53. StyleSheet.normalizeSelector = function(selector) {
  54. return selector.replace(/[\s]+/g, ' ').replace(/[\s]*([>\,\+]{1})[\s]*/g, '$1').trim();
  55. };
  56. StyleSheet.splitSelectors = function(s) {
  57. return s.trim().split(',');
  58. };
  59. return StyleSheet;
  60. })();