Posted By

kif on 07/29/06


Tagged

css


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

meth
fantomex
shachi
Knarf


CSS writer


 / Published in: JavaScript
 

Support: Fx2, Opera9, IE6 CSS.add() can add style rules by the method of like GM_addStyle(). This can add Style string at high speed, because it doesn't use a handmade parser.

  1. /*-----------------------------------------------------------------------------
  2.  * CSS writer
  3.  * Example Source
  4. CSS.add(
  5. 'body { background: #eee; color: #333; }',
  6. '#id .class { display: block; width: 100%; }',
  7. 'a:link, a:visited { color: #00f; }',
  8. 'a:active, a:hover { color: #f00; }'
  9. );
  10. var value1 = CSS.get('#wrapper', 'width');
  11. var value2 = CSS.get($('id'), 'color', 'hover');
  12.  *-------------------------------------------------------------------------- */
  13.  
  14. var CSS = {
  15. _add: function (selector, property) {
  16. var css = document.styleSheets[0];
  17. if (css.addRule)
  18. css.addRule(selector, '{' + property + '}');
  19. else if (css.insertRule)
  20. css.insertRule(selector + '{' + property + '}', css.cssRules.length);
  21. },
  22. add: function () {
  23. $A(arguments).join('').split('}').remove('').forEach(function (css) {
  24. var tmp = css.split('{');
  25. CSS._add(tmp[0], tmp[1]);
  26. });
  27. },
  28. get: function (selector, property, pseudo) {
  29. if (isString(selector)) {
  30. var sheets = document.styleSheets;
  31. (sheets[0].rule) && (selector = selector.replace(/\*/g, ''));
  32. for (var i = 0, len = sheets.length; i < len; ++i) {
  33. var sheet = sheets[i].rule || sheets[i].cssRules;
  34. for (var j = 0, l = sheet.length; j < len; ++j) {
  35. var css = sheet[i];
  36. if (css.selectorText.toLowerCase() == selector.toLowerCase())
  37. return css.style[property];
  38. }
  39. }
  40. return null;
  41. } else
  42. return (
  43. (selector.currentStyle) ? selector.currentStyle[property.toCamelize()] :
  44. (document.defaultView.getComputedStyle) ? document.defaultView.getComputedStyle(selector, pseudo).getPropertyValue(property.toSelector()) :
  45. ''
  46. );
  47. }
  48. }
  49.  
  50. /*
  51.  * Extend
  52.  */
  53.  
  54. function $A(v) {
  55. if (!v) return [];
  56. if (isArray(v))
  57. return v;
  58. if (v.toArray)
  59. return v.toArray();
  60. else {
  61. var len = v.length;
  62. var res = new Array(len);
  63. for (var i = 0; i < len; ++i)
  64. res[i] = v[i];
  65. return res;
  66. }
  67. }
  68.  
  69. Array.prototype.filter || (Array.prototype.filter = function (callback, thisobj) {
  70. var res = [];
  71. for (var i = 0, len = this.length; i < len; ++i)
  72. callback.call(thisobj, this[i], i, this) && res.push(this[i]);
  73. return res;
  74. });
  75.  
  76. Array.prototype.remove = function (rule) {
  77. return this.filter(function (val) {
  78. return (val != rule);
  79. });
  80. }
  81.  
  82. Array.prototype.forEach || (Array.prototype.forEach = function (callback, thisobj) {
  83. for (var i = 0, len = this.length; i < len; ++i)
  84. callback.call(thisobj, this[i], i, this);
  85. });
  86.  
  87. String.prototype.toCamelize = function () {
  88. return this.replace(/\-(.)/g, function (x, y) {
  89. return y.toUpperCase();
  90. });
  91. }
  92.  
  93. String.prototype.toSelector = function () {
  94. return this.replace(/([A-Z])/g, '-$1').toLowerCase();
  95. }

Report this snippet  

You need to login to post a comment.