CSSStyleDeclaration.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //.CommonJS
  2. var CSSOM = {};
  3. ///CommonJS
  4. /**
  5. * @constructor
  6. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
  7. */
  8. CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){
  9. this.length = 0;
  10. this.parentRule = null;
  11. // NON-STANDARD
  12. this._importants = {};
  13. };
  14. CSSOM.CSSStyleDeclaration.prototype = {
  15. constructor: CSSOM.CSSStyleDeclaration,
  16. /**
  17. *
  18. * @param {string} name
  19. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-getPropertyValue
  20. * @return {string} the value of the property if it has been explicitly set for this declaration block.
  21. * Returns the empty string if the property has not been set.
  22. */
  23. getPropertyValue: function(name) {
  24. return this[name] || "";
  25. },
  26. /**
  27. *
  28. * @param {string} name
  29. * @param {string} value
  30. * @param {string} [priority=null] "important" or null
  31. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty
  32. */
  33. setProperty: function(name, value, priority) {
  34. if (this[name]) {
  35. // Property already exist. Overwrite it.
  36. var index = Array.prototype.indexOf.call(this, name);
  37. if (index < 0) {
  38. this[this.length] = name;
  39. this.length++;
  40. }
  41. } else {
  42. // New property.
  43. this[this.length] = name;
  44. this.length++;
  45. }
  46. this[name] = value + "";
  47. this._importants[name] = priority;
  48. },
  49. /**
  50. *
  51. * @param {string} name
  52. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-removeProperty
  53. * @return {string} the value of the property if it has been explicitly set for this declaration block.
  54. * Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.
  55. */
  56. removeProperty: function(name) {
  57. if (!(name in this)) {
  58. return "";
  59. }
  60. var index = Array.prototype.indexOf.call(this, name);
  61. if (index < 0) {
  62. return "";
  63. }
  64. var prevValue = this[name];
  65. this[name] = "";
  66. // That's what WebKit and Opera do
  67. Array.prototype.splice.call(this, index, 1);
  68. // That's what Firefox does
  69. //this[index] = ""
  70. return prevValue;
  71. },
  72. getPropertyCSSValue: function() {
  73. //FIXME
  74. },
  75. /**
  76. *
  77. * @param {String} name
  78. */
  79. getPropertyPriority: function(name) {
  80. return this._importants[name] || "";
  81. },
  82. /**
  83. * element.style.overflow = "auto"
  84. * element.style.getPropertyShorthand("overflow-x")
  85. * -> "overflow"
  86. */
  87. getPropertyShorthand: function() {
  88. //FIXME
  89. },
  90. isPropertyImplicit: function() {
  91. //FIXME
  92. },
  93. // Doesn't work in IE < 9
  94. get cssText(){
  95. var properties = [];
  96. for (var i=0, length=this.length; i < length; ++i) {
  97. var name = this[i];
  98. var value = this.getPropertyValue(name);
  99. var priority = this.getPropertyPriority(name);
  100. if (priority) {
  101. priority = " !" + priority;
  102. }
  103. properties[i] = name + ": " + value + priority + ";";
  104. }
  105. return properties.join(" ");
  106. },
  107. set cssText(text){
  108. var i, name;
  109. for (i = this.length; i--;) {
  110. name = this[i];
  111. this[name] = "";
  112. }
  113. Array.prototype.splice.call(this, 0, this.length);
  114. this._importants = {};
  115. var dummyRule = CSSOM.parse('#bogus{' + text + '}').cssRules[0].style;
  116. var length = dummyRule.length;
  117. for (i = 0; i < length; ++i) {
  118. name = dummyRule[i];
  119. this.setProperty(dummyRule[i], dummyRule.getPropertyValue(name), dummyRule.getPropertyPriority(name));
  120. }
  121. }
  122. };
  123. //.CommonJS
  124. exports.CSSStyleDeclaration = CSSOM.CSSStyleDeclaration;
  125. CSSOM.parse = require('./parse').parse; // Cannot be included sooner due to the mutual dependency between parse.js and CSSStyleDeclaration.js
  126. ///CommonJS