CSSImportRule.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSRule: require("./CSSRule").CSSRule,
  4. CSSStyleSheet: require("./CSSStyleSheet").CSSStyleSheet,
  5. MediaList: require("./MediaList").MediaList
  6. };
  7. ///CommonJS
  8. /**
  9. * @constructor
  10. * @see http://dev.w3.org/csswg/cssom/#cssimportrule
  11. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSImportRule
  12. */
  13. CSSOM.CSSImportRule = function CSSImportRule() {
  14. CSSOM.CSSRule.call(this);
  15. this.href = "";
  16. this.media = new CSSOM.MediaList();
  17. this.styleSheet = new CSSOM.CSSStyleSheet();
  18. };
  19. CSSOM.CSSImportRule.prototype = new CSSOM.CSSRule();
  20. CSSOM.CSSImportRule.prototype.constructor = CSSOM.CSSImportRule;
  21. CSSOM.CSSImportRule.prototype.type = 3;
  22. Object.defineProperty(CSSOM.CSSImportRule.prototype, "cssText", {
  23. get: function() {
  24. var mediaText = this.media.mediaText;
  25. return "@import url(" + this.href + ")" + (mediaText ? " " + mediaText : "") + ";";
  26. },
  27. set: function(cssText) {
  28. var i = 0;
  29. /**
  30. * @import url(partial.css) screen, handheld;
  31. * || |
  32. * after-import media
  33. * |
  34. * url
  35. */
  36. var state = '';
  37. var buffer = '';
  38. var index;
  39. for (var character; (character = cssText.charAt(i)); i++) {
  40. switch (character) {
  41. case ' ':
  42. case '\t':
  43. case '\r':
  44. case '\n':
  45. case '\f':
  46. if (state === 'after-import') {
  47. state = 'url';
  48. } else {
  49. buffer += character;
  50. }
  51. break;
  52. case '@':
  53. if (!state && cssText.indexOf('@import', i) === i) {
  54. state = 'after-import';
  55. i += 'import'.length;
  56. buffer = '';
  57. }
  58. break;
  59. case 'u':
  60. if (state === 'url' && cssText.indexOf('url(', i) === i) {
  61. index = cssText.indexOf(')', i + 1);
  62. if (index === -1) {
  63. throw i + ': ")" not found';
  64. }
  65. i += 'url('.length;
  66. var url = cssText.slice(i, index);
  67. if (url[0] === url[url.length - 1]) {
  68. if (url[0] === '"' || url[0] === "'") {
  69. url = url.slice(1, -1);
  70. }
  71. }
  72. this.href = url;
  73. i = index;
  74. state = 'media';
  75. }
  76. break;
  77. case '"':
  78. if (state === 'url') {
  79. index = cssText.indexOf('"', i + 1);
  80. if (!index) {
  81. throw i + ": '\"' not found";
  82. }
  83. this.href = cssText.slice(i + 1, index);
  84. i = index;
  85. state = 'media';
  86. }
  87. break;
  88. case "'":
  89. if (state === 'url') {
  90. index = cssText.indexOf("'", i + 1);
  91. if (!index) {
  92. throw i + ': "\'" not found';
  93. }
  94. this.href = cssText.slice(i + 1, index);
  95. i = index;
  96. state = 'media';
  97. }
  98. break;
  99. case ';':
  100. if (state === 'media') {
  101. if (buffer) {
  102. this.media.mediaText = buffer.trim();
  103. }
  104. }
  105. break;
  106. default:
  107. if (state === 'media') {
  108. buffer += character;
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. });
  115. //.CommonJS
  116. exports.CSSImportRule = CSSOM.CSSImportRule;
  117. ///CommonJS