CSSImportRule.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSRule: require("./CSSRule").CSSRule,
  4. CSSStyleSheet: require("./CSSStyleSheet").CSSStyleSheet,
  5. MediaList: require("./MediaList").MediaList
  6. };
  7. var regexPatterns = require("./regexPatterns").regexPatterns;
  8. ///CommonJS
  9. /**
  10. * @constructor
  11. * @see http://dev.w3.org/csswg/cssom/#cssimportrule
  12. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSImportRule
  13. */
  14. CSSOM.CSSImportRule = function CSSImportRule() {
  15. CSSOM.CSSRule.call(this);
  16. this.__href = "";
  17. this.__media = new CSSOM.MediaList();
  18. this.__layerName = null;
  19. this.__supportsText = null;
  20. this.__styleSheet = new CSSOM.CSSStyleSheet();
  21. };
  22. CSSOM.CSSImportRule.prototype = Object.create(CSSOM.CSSRule.prototype);
  23. CSSOM.CSSImportRule.prototype.constructor = CSSOM.CSSImportRule;
  24. Object.setPrototypeOf(CSSOM.CSSImportRule, CSSOM.CSSRule);
  25. Object.defineProperty(CSSOM.CSSImportRule.prototype, "type", {
  26. value: 3,
  27. writable: false
  28. });
  29. Object.defineProperty(CSSOM.CSSImportRule.prototype, "cssText", {
  30. get: function() {
  31. var mediaText = this.media.mediaText;
  32. return "@import url(\"" + this.href.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + "\")" + (this.layerName !== null ? " layer" + (this.layerName && "(" + this.layerName + ")") : "" ) + (this.supportsText ? " supports(" + this.supportsText + ")" : "" ) + (mediaText ? " " + mediaText : "") + ";";
  33. }
  34. });
  35. Object.defineProperty(CSSOM.CSSImportRule.prototype, "href", {
  36. get: function() {
  37. return this.__href;
  38. }
  39. });
  40. Object.defineProperty(CSSOM.CSSImportRule.prototype, "media", {
  41. get: function() {
  42. return this.__media;
  43. },
  44. set: function(value) {
  45. if (typeof value === "string") {
  46. this.__media.mediaText = value;
  47. } else {
  48. this.__media = value;
  49. }
  50. }
  51. });
  52. Object.defineProperty(CSSOM.CSSImportRule.prototype, "layerName", {
  53. get: function() {
  54. return this.__layerName;
  55. }
  56. });
  57. Object.defineProperty(CSSOM.CSSImportRule.prototype, "supportsText", {
  58. get: function() {
  59. return this.__supportsText;
  60. }
  61. });
  62. Object.defineProperty(CSSOM.CSSImportRule.prototype, "styleSheet", {
  63. get: function() {
  64. return this.__styleSheet;
  65. }
  66. });
  67. /**
  68. * NON-STANDARD
  69. * Rule text parser.
  70. * @param {string} cssText
  71. */
  72. Object.defineProperty(CSSOM.CSSImportRule.prototype, "parse", {
  73. value: function(cssText) {
  74. var i = 0;
  75. /**
  76. * @import url(partial.css) screen, handheld;
  77. * || |
  78. * after-import media
  79. * |
  80. * url
  81. */
  82. var state = '';
  83. var buffer = '';
  84. var index;
  85. var layerRegExp = regexPatterns.layerRegExp;
  86. var layerRuleNameRegExp = regexPatterns.layerRuleNameRegExp;
  87. var doubleOrMoreSpacesRegExp = regexPatterns.doubleOrMoreSpacesRegExp;
  88. /**
  89. * Extracts the content inside supports() handling nested parentheses.
  90. * @param {string} text - The text to parse
  91. * @returns {object|null} - {content: string, endIndex: number} or null if not found
  92. */
  93. function extractSupportsContent(text) {
  94. var supportsIndex = text.indexOf('supports(');
  95. if (supportsIndex !== 0) {
  96. return null;
  97. }
  98. var depth = 0;
  99. var start = supportsIndex + 'supports('.length;
  100. var i = start;
  101. for (; i < text.length; i++) {
  102. if (text[i] === '(') {
  103. depth++;
  104. } else if (text[i] === ')') {
  105. if (depth === 0) {
  106. // Found the closing parenthesis for supports()
  107. return {
  108. content: text.slice(start, i),
  109. endIndex: i
  110. };
  111. }
  112. depth--;
  113. }
  114. }
  115. return null; // Unbalanced parentheses
  116. }
  117. for (var character; (character = cssText.charAt(i)); i++) {
  118. switch (character) {
  119. case ' ':
  120. case '\t':
  121. case '\r':
  122. case '\n':
  123. case '\f':
  124. if (state === 'after-import') {
  125. state = 'url';
  126. } else {
  127. buffer += character;
  128. }
  129. break;
  130. case '@':
  131. if (!state && cssText.indexOf('@import', i) === i) {
  132. state = 'after-import';
  133. i += 'import'.length;
  134. buffer = '';
  135. }
  136. break;
  137. case 'u':
  138. if (state === 'media') {
  139. buffer += character;
  140. }
  141. if (state === 'url' && cssText.indexOf('url(', i) === i) {
  142. index = cssText.indexOf(')', i + 1);
  143. if (index === -1) {
  144. throw i + ': ")" not found';
  145. }
  146. i += 'url('.length;
  147. var url = cssText.slice(i, index);
  148. if (url[0] === url[url.length - 1]) {
  149. if (url[0] === '"' || url[0] === "'") {
  150. url = url.slice(1, -1);
  151. }
  152. }
  153. this.__href = url;
  154. i = index;
  155. state = 'media';
  156. }
  157. break;
  158. case '"':
  159. if (state === 'after-import' || state === 'url') {
  160. index = cssText.indexOf('"', i + 1);
  161. if (!index) {
  162. throw i + ": '\"' not found";
  163. }
  164. this.__href = cssText.slice(i + 1, index);
  165. i = index;
  166. state = 'media';
  167. }
  168. break;
  169. case "'":
  170. if (state === 'after-import' || state === 'url') {
  171. index = cssText.indexOf("'", i + 1);
  172. if (!index) {
  173. throw i + ': "\'" not found';
  174. }
  175. this.__href = cssText.slice(i + 1, index);
  176. i = index;
  177. state = 'media';
  178. }
  179. break;
  180. case ';':
  181. if (state === 'media') {
  182. if (buffer) {
  183. var bufferTrimmed = buffer.trim();
  184. if (bufferTrimmed.indexOf('layer') === 0) {
  185. var layerMatch = bufferTrimmed.match(layerRegExp);
  186. if (layerMatch) {
  187. var layerName = layerMatch[1].trim();
  188. if (layerName.match(layerRuleNameRegExp) !== null) {
  189. this.__layerName = layerMatch[1].trim();
  190. bufferTrimmed = bufferTrimmed.replace(layerRegExp, '')
  191. .replace(doubleOrMoreSpacesRegExp, ' ') // Replace double or more spaces with single space
  192. .trim();
  193. } else {
  194. // REVIEW: In the browser, an empty layer() is not processed as a unamed layer
  195. // and treats the rest of the string as mediaText, ignoring the parse of supports()
  196. if (bufferTrimmed) {
  197. this.media.mediaText = bufferTrimmed;
  198. return;
  199. }
  200. }
  201. } else {
  202. this.__layerName = "";
  203. bufferTrimmed = bufferTrimmed.substring('layer'.length).trim()
  204. }
  205. }
  206. var supportsResult = extractSupportsContent(bufferTrimmed);
  207. if (supportsResult) {
  208. // REVIEW: In the browser, an empty supports() invalidates and ignores the entire @import rule
  209. this.__supportsText = supportsResult.content.trim();
  210. // Remove the entire supports(...) from the buffer
  211. bufferTrimmed = bufferTrimmed.slice(0, 0) + bufferTrimmed.slice(supportsResult.endIndex + 1);
  212. bufferTrimmed = bufferTrimmed.replace(doubleOrMoreSpacesRegExp, ' ').trim();
  213. }
  214. // REVIEW: In the browser, any invalid media is replaced with 'not all'
  215. if (bufferTrimmed) {
  216. this.media.mediaText = bufferTrimmed;
  217. }
  218. }
  219. }
  220. break;
  221. default:
  222. if (state === 'media') {
  223. buffer += character;
  224. }
  225. break;
  226. }
  227. }
  228. }
  229. });
  230. //.CommonJS
  231. exports.CSSImportRule = CSSOM.CSSImportRule;
  232. ///CommonJS