Skip to content

Commit d6864f1

Browse files
committed
add @media support
1 parent 370eb25 commit d6864f1

File tree

3 files changed

+132
-13
lines changed

3 files changed

+132
-13
lines changed

index.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ module.exports = function(css){
1212
*/
1313

1414
function stylesheet() {
15-
var rules = [];
15+
return { stylesheet: { rules: rules() }};
16+
}
17+
18+
/**
19+
* Parse ruleset.
20+
*/
21+
22+
function rules() {
1623
var node;
24+
var rules = [];
1725
whitespace();
1826
comments();
19-
while (node = atrule() || rule()) {
27+
while (css[0] != '}' && (node = atrule() || rule())) {
2028
comments();
2129
rules.push(node);
2230
}
23-
return { stylesheet: { rules: rules }};
31+
return rules;
2432
}
2533

2634
/**
@@ -155,6 +163,37 @@ module.exports = function(css){
155163
};
156164
}
157165

166+
/**
167+
* Parse media.
168+
*/
169+
170+
function media() {
171+
var m = match(/^@media *([^{]+)/);
172+
if (!m) return;
173+
var media = m[1].trim();
174+
175+
// {
176+
if (!match(/^{\s*/)) return;
177+
comments();
178+
179+
var style = rules();
180+
181+
// }
182+
if (!match(/^}\s*/)) return;
183+
184+
return { media: media, rules: style };
185+
}
186+
187+
/**
188+
* Parse import
189+
*/
190+
191+
function atimport() {
192+
var m = match(/^@import *([^;\n]+);\s*/);
193+
if (!m) return;
194+
return { import: m[1].trim() };
195+
}
196+
158197
/**
159198
* Parse declarations.
160199
*/
@@ -178,22 +217,13 @@ module.exports = function(css){
178217
return decls;
179218
}
180219

181-
/**
182-
* Parse import
183-
*/
184-
185-
function atimport() {
186-
var m = match(/^@import *([^;\n]+);\s*/);
187-
if (!m) return;
188-
return { import: m[1].trim() };
189-
}
190-
191220
/**
192221
* Parse at rule.
193222
*/
194223

195224
function atrule() {
196225
return keyframes()
226+
|| media()
197227
|| atimport();
198228
}
199229

test/cases/media.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@media screen, projection {
2+
html {
3+
background: #fffef0;
4+
color: #300;
5+
}
6+
body {
7+
max-width: 35em;
8+
margin: 0 auto;
9+
}
10+
}
11+
12+
@media print {
13+
html {
14+
background: #fff;
15+
color: #000;
16+
}
17+
body {
18+
padding: 1in;
19+
border: 0.5pt solid #666;
20+
}
21+
}

test/cases/media.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"stylesheet": {
3+
"rules": [
4+
{
5+
"media": "screen, projection",
6+
"rules": [
7+
{
8+
"selector": "html",
9+
"declarations": [
10+
{
11+
"property": "background",
12+
"value": "#fffef0"
13+
},
14+
{
15+
"property": "color",
16+
"value": "#300"
17+
}
18+
]
19+
},
20+
{
21+
"selector": "body",
22+
"declarations": [
23+
{
24+
"property": "max-width",
25+
"value": "35em"
26+
},
27+
{
28+
"property": "margin",
29+
"value": "0 auto"
30+
}
31+
]
32+
}
33+
]
34+
},
35+
{
36+
"media": "print",
37+
"rules": [
38+
{
39+
"selector": "html",
40+
"declarations": [
41+
{
42+
"property": "background",
43+
"value": "#fff"
44+
},
45+
{
46+
"property": "color",
47+
"value": "#000"
48+
}
49+
]
50+
},
51+
{
52+
"selector": "body",
53+
"declarations": [
54+
{
55+
"property": "padding",
56+
"value": "1in"
57+
},
58+
{
59+
"property": "border",
60+
"value": "0.5pt solid #666"
61+
}
62+
]
63+
}
64+
]
65+
}
66+
]
67+
}
68+
}

0 commit comments

Comments
 (0)