CSSStyleDeclaration.js 2.9 KB

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