UNPKG

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