CSSStyleRule.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
  4. CSSRule: require("./CSSRule").CSSRule,
  5. CSSRuleList: require("./CSSRuleList").CSSRuleList,
  6. CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
  7. };
  8. var regexPatterns = require("./regexPatterns").regexPatterns;
  9. // Use cssstyle if available
  10. try {
  11. CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
  12. } catch (e) {
  13. // ignore
  14. }
  15. ///CommonJS
  16. /**
  17. * @constructor
  18. * @see http://dev.w3.org/csswg/cssom/#cssstylerule
  19. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule
  20. */
  21. CSSOM.CSSStyleRule = function CSSStyleRule() {
  22. CSSOM.CSSGroupingRule.call(this);
  23. this.__selectorText = "";
  24. this.__style = new CSSOM.CSSStyleDeclaration();
  25. this.__style.parentRule = this;
  26. };
  27. CSSOM.CSSStyleRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
  28. CSSOM.CSSStyleRule.prototype.constructor = CSSOM.CSSStyleRule;
  29. Object.setPrototypeOf(CSSOM.CSSStyleRule, CSSOM.CSSGroupingRule);
  30. Object.defineProperty(CSSOM.CSSStyleRule.prototype, "type", {
  31. value: 1,
  32. writable: false
  33. });
  34. Object.defineProperty(CSSOM.CSSStyleRule.prototype, "selectorText", {
  35. get: function() {
  36. return this.__selectorText;
  37. },
  38. set: function(value) {
  39. if (typeof value === "string") {
  40. // Don't trim if the value ends with a hex escape sequence followed by space
  41. // (e.g., ".\31 " where the space is part of the escape terminator)
  42. var endsWithHexEscapeRegExp = regexPatterns.endsWithHexEscapeRegExp;
  43. var endsWithEscape = endsWithHexEscapeRegExp.test(value);
  44. var trimmedValue = endsWithEscape ? value.replace(/\s+$/, ' ').trimStart() : value.trim();
  45. if (trimmedValue === '') {
  46. return;
  47. }
  48. // TODO: Setting invalid selectorText should be ignored
  49. // There are some validations already on lib/parse.js
  50. // but the same validations should be applied here.
  51. // Check if we can move these validation logic to a shared function.
  52. this.__selectorText = trimmedValue;
  53. }
  54. },
  55. configurable: true
  56. });
  57. Object.defineProperty(CSSOM.CSSStyleRule.prototype, "style", {
  58. get: function() {
  59. return this.__style;
  60. },
  61. set: function(value) {
  62. if (typeof value === "string") {
  63. this.__style.cssText = value;
  64. } else {
  65. this.__style = value;
  66. }
  67. },
  68. configurable: true
  69. });
  70. Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
  71. get: function() {
  72. var text;
  73. if (this.selectorText) {
  74. var values = "";
  75. if (this.cssRules.length) {
  76. var valuesArr = [" {"];
  77. this.style.cssText && valuesArr.push(this.style.cssText);
  78. valuesArr.push(this.cssRules.reduce(function(acc, rule){
  79. if (rule.cssText !== "") {
  80. acc.push(rule.cssText);
  81. }
  82. return acc;
  83. }, []).join("\n "));
  84. values = valuesArr.join("\n ") + "\n}";
  85. } else {
  86. values = " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
  87. }
  88. text = this.selectorText + values;
  89. } else {
  90. text = "";
  91. }
  92. return text;
  93. }
  94. });
  95. //.CommonJS
  96. exports.CSSStyleRule = CSSOM.CSSStyleRule;
  97. ///CommonJS