StyleSheet.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  6. // Generated by CoffeeScript 2.5.1
  7. var Rule, StyleSheet;
  8. Rule = require('./Rule');
  9. module.exports = StyleSheet = function () {
  10. var self;
  11. var StyleSheet = /*#__PURE__*/function () {
  12. function StyleSheet() {
  13. _classCallCheck(this, StyleSheet);
  14. this._rulesBySelector = {};
  15. }
  16. _createClass(StyleSheet, [{
  17. key: "setRule",
  18. value: function setRule(selector, styles) {
  19. var key, val;
  20. if (typeof selector === 'string') {
  21. this._setRule(selector, styles);
  22. } else if (_typeof(selector) === 'object') {
  23. for (key in selector) {
  24. val = selector[key];
  25. this._setRule(key, val);
  26. }
  27. }
  28. return this;
  29. }
  30. }, {
  31. key: "_setRule",
  32. value: function _setRule(s, styles) {
  33. var i, len, ref, selector;
  34. ref = self.splitSelectors(s);
  35. for (i = 0, len = ref.length; i < len; i++) {
  36. selector = ref[i];
  37. this._setSingleRule(selector, styles);
  38. }
  39. return this;
  40. }
  41. }, {
  42. key: "_setSingleRule",
  43. value: function _setSingleRule(s, styles) {
  44. var rule, selector;
  45. selector = self.normalizeSelector(s);
  46. if (!(rule = this._rulesBySelector[selector])) {
  47. rule = new Rule(selector);
  48. this._rulesBySelector[selector] = rule;
  49. }
  50. rule.setStyles(styles);
  51. return this;
  52. }
  53. }, {
  54. key: "getRulesFor",
  55. value: function getRulesFor(el) {
  56. var ref, rule, rules, selector;
  57. rules = [];
  58. ref = this._rulesBySelector;
  59. for (selector in ref) {
  60. rule = ref[selector];
  61. if (rule.selector.matches(el)) {
  62. rules.push(rule);
  63. }
  64. }
  65. return rules;
  66. }
  67. }], [{
  68. key: "normalizeSelector",
  69. value: function normalizeSelector(selector) {
  70. return selector.replace(/[\s]+/g, ' ').replace(/[\s]*([>\,\+]{1})[\s]*/g, '$1').trim();
  71. }
  72. }, {
  73. key: "splitSelectors",
  74. value: function splitSelectors(s) {
  75. return s.trim().split(',');
  76. }
  77. }]);
  78. return StyleSheet;
  79. }();
  80. ;
  81. self = StyleSheet;
  82. return StyleSheet;
  83. }.call(void 0);