CSSPropertyRule.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSRule: require("./CSSRule").CSSRule
  4. };
  5. ///CommonJS
  6. /**
  7. * @constructor
  8. * @see https://drafts.css-houdini.org/css-properties-values-api/#the-css-property-rule-interface
  9. */
  10. CSSOM.CSSPropertyRule = function CSSPropertyRule() {
  11. CSSOM.CSSRule.call(this);
  12. this.__name = "";
  13. this.__syntax = "";
  14. this.__inherits = false;
  15. this.__initialValue = null;
  16. };
  17. CSSOM.CSSPropertyRule.prototype = Object.create(CSSOM.CSSRule.prototype);
  18. CSSOM.CSSPropertyRule.prototype.constructor = CSSOM.CSSPropertyRule;
  19. Object.setPrototypeOf(CSSOM.CSSPropertyRule, CSSOM.CSSRule);
  20. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "type", {
  21. value: 0,
  22. writable: false
  23. });
  24. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "cssText", {
  25. get: function() {
  26. var text = "@property " + this.name + " {";
  27. if (this.syntax !== "") {
  28. text += " syntax: \"" + this.syntax.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + "\";";
  29. }
  30. text += " inherits: " + (this.inherits ? "true" : "false") + ";";
  31. if (this.initialValue !== null) {
  32. text += " initial-value: " + this.initialValue + ";";
  33. }
  34. text += " }";
  35. return text;
  36. }
  37. });
  38. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "name", {
  39. get: function() {
  40. return this.__name;
  41. }
  42. });
  43. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "syntax", {
  44. get: function() {
  45. return this.__syntax;
  46. }
  47. });
  48. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "inherits", {
  49. get: function() {
  50. return this.__inherits;
  51. }
  52. });
  53. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "initialValue", {
  54. get: function() {
  55. return this.__initialValue;
  56. }
  57. });
  58. /**
  59. * NON-STANDARD
  60. * Rule text parser.
  61. * @param {string} cssText
  62. * @returns {boolean} True if the rule is valid and was parsed successfully
  63. */
  64. Object.defineProperty(CSSOM.CSSPropertyRule.prototype, "parse", {
  65. value: function(cssText) {
  66. // Extract the name from "@property <name> { ... }"
  67. var match = cssText.match(/@property\s+(--[^\s{]+)\s*\{([^]*)\}/);
  68. if (!match) {
  69. return false;
  70. }
  71. this.__name = match[1];
  72. var bodyText = match[2];
  73. // Parse syntax descriptor (REQUIRED)
  74. var syntaxMatch = bodyText.match(/syntax\s*:\s*(['"])([^]*?)\1\s*;/);
  75. if (!syntaxMatch) {
  76. return false; // syntax is required
  77. }
  78. this.__syntax = syntaxMatch[2];
  79. // Syntax cannot be empty
  80. if (this.__syntax === "") {
  81. return false;
  82. }
  83. // Parse inherits descriptor (REQUIRED)
  84. var inheritsMatch = bodyText.match(/inherits\s*:\s*(true|false)\s*;/);
  85. if (!inheritsMatch) {
  86. return false; // inherits is required
  87. }
  88. this.__inherits = inheritsMatch[1] === "true";
  89. // Parse initial-value descriptor (OPTIONAL, but required if syntax is not "*")
  90. var initialValueMatch = bodyText.match(/initial-value\s*:\s*([^;]+);/);
  91. if (initialValueMatch) {
  92. this.__initialValue = initialValueMatch[1].trim();
  93. } else {
  94. // If syntax is not "*", initial-value is required
  95. if (this.__syntax !== "*") {
  96. return false;
  97. }
  98. }
  99. return true; // Successfully parsed
  100. }
  101. });
  102. //.CommonJS
  103. exports.CSSPropertyRule = CSSOM.CSSPropertyRule;
  104. ///CommonJS