Skip to content

Commit 73cdb11

Browse files
committed
Merge pull request reworkcss#17 from philipwalton/paged-media
Add support for @page at-rules with nested @Margin at-rules.
2 parents ea64e6e + 49d2c66 commit 73cdb11

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,50 @@ module.exports = function(css){
213213
return { media: media, rules: style };
214214
}
215215

216+
/**
217+
* Parse paged media.
218+
*/
219+
220+
function atpage() {
221+
var m = match(/^@page */);
222+
if (!m) return;
223+
224+
var sel = selector() || [];
225+
var decls = [];
226+
227+
if (!open()) return;
228+
comments();
229+
230+
// declarations
231+
var decl;
232+
while (decl = declaration() || atmargin()) {
233+
decls.push(decl);
234+
comments();
235+
}
236+
237+
if (!close()) return;
238+
239+
return {
240+
type: "page",
241+
selectors: sel,
242+
declarations: decls
243+
};
244+
}
245+
246+
/**
247+
* Parse margin at-rules
248+
*/
249+
250+
function atmargin() {
251+
var m = match(/^@([a-z\-]+) */);
252+
if (!m) return;
253+
var type = m[1]
254+
255+
return {
256+
type: type,
257+
declarations: declarations()
258+
}
259+
}
216260

217261
/**
218262
* Parse import
@@ -281,8 +325,8 @@ module.exports = function(css){
281325
|| supports()
282326
|| atimport()
283327
|| atcharset()
284-
|| atnamespace();
285-
328+
|| atnamespace()
329+
|| atpage();
286330
}
287331

288332
/**

test/cases/paged-media.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@page toc, index:blank {
2+
color: green;
3+
4+
@top-left {
5+
content: "foo";
6+
color: blue;
7+
}
8+
@top-right {
9+
content: "bar";
10+
}
11+
}
12+
13+
@page {
14+
font-size: 16pt;
15+
}

test/cases/paged-media.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"stylesheet": {
3+
"rules": [
4+
{
5+
"type": "page",
6+
"selectors": [
7+
"toc",
8+
"index:blank"
9+
],
10+
"declarations": [
11+
{
12+
"property": "color",
13+
"value": "green"
14+
},
15+
{
16+
"type": "top-left",
17+
"declarations": [
18+
{
19+
"property": "content",
20+
"value": "\"foo\""
21+
},
22+
{
23+
"property": "color",
24+
"value": "blue"
25+
}
26+
]
27+
},
28+
{
29+
"type": "top-right",
30+
"declarations": [
31+
{
32+
"property": "content",
33+
"value": "\"bar\""
34+
}
35+
]
36+
}
37+
]
38+
},
39+
{
40+
"type": "page",
41+
"selectors": [],
42+
"declarations": [
43+
{
44+
"property": "font-size",
45+
"value": "16pt"
46+
}
47+
]
48+
}
49+
]
50+
}
51+
}

0 commit comments

Comments
 (0)