Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support atpage margin boxes #105

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ module.exports = function(css, options){

// declarations
var decl;
while (decl = declaration()) {
while (decl = declaration() || atmarginbox()) {
decls.push(decl);
decls = decls.concat(comments());
}
Expand All @@ -436,6 +436,37 @@ module.exports = function(css, options){
});
}

/**
* Parse margin boxes
*/

function atmarginbox() {
var pos = position();
var m = match(/^@((top|bottom|left|right)-(center|right|top|middle|left|bottom)(-corner)?) */);
if (!m) return;

var name = m[1];

if (!open()) return error("@margin-box missing '{'");
var decls = comments();

// declarations
var decl;
while (decl = declaration()) {
decls.push(decl);
decls = decls.concat(comments());
}

if (!close()) return error("@margin-box missing '}'");

return pos({
type: 'margin-box',
name: name,
declarations: decls
});

}

/**
* Parse document.
*/
Expand Down
11 changes: 11 additions & 0 deletions lib/stringify/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Compiler.prototype.page = function(node){
+ this.emit('}');
};

/**
* Visit margin-box node.
*/

Compiler.prototype['margin-box'] = function(node){
return this.emit('@' + node.name + ' ', node.position)
+ this.emit('{')
+ this.mapVisit(node.declarations)
+ this.emit('}');
};

/**
* Visit font-face node.
*/
Expand Down
14 changes: 14 additions & 0 deletions lib/stringify/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ Compiler.prototype.page = function(node){
+ this.emit('\n}');
};

/**
* Visit margin-box node.
*/

Compiler.prototype['margin-box'] = function(node){
return this.emit(this.indent())
+ this.emit('@' + node.name + ' ', node.position)
+ this.emit('{\n')
+ this.emit(this.indent(1))
+ this.mapVisit(node.declarations, '\n')
+ this.emit(this.indent(-1))
+ this.emit('\n' + this.indent() + '}');
};

/**
* Visit font-face node.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css",
"version": "2.2.1",
"version": "2.2.2",
"description": "CSS parser / stringifier",
"main": "index",
"files": [
Expand Down
140 changes: 138 additions & 2 deletions test/cases/paged-media/ast.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"type": "stylesheet",
"stylesheet": {
"source": "input.css",
"rules": [
{
"type": "comment",
Expand Down Expand Up @@ -135,7 +136,142 @@
},
"source": "input.css"
}
},
{
"type": "page",
"selectors": [
"named-page"
],
"declarations": [
{
"type": "declaration",
"property": "size",
"value": "A4 landscape",
"position": {
"start": {
"line": 16,
"column": 3
},
"end": {
"line": 16,
"column": 21
},
"source": "input.css"
}
}
],
"position": {
"start": {
"line": 15,
"column": 1
},
"end": {
"line": 17,
"column": 2
},
"source": "input.css"
}
},
{
"type": "page",
"selectors": [],
"declarations": [
{
"type": "declaration",
"property": "size",
"value": "210mm 279mm",
"position": {
"start": {
"line": 20,
"column": 3
},
"end": {
"line": 20,
"column": 20
},
"source": "input.css"
}
},
{
"type": "margin-box",
"name": "top-left",
"declarations": [
{
"type": "declaration",
"property": "padding-top",
"value": "0",
"position": {
"start": {
"line": 23,
"column": 5
},
"end": {
"line": 23,
"column": 19
},
"source": "input.css"
}
}
],
"position": {
"start": {
"line": 22,
"column": 3
},
"end": {
"line": 24,
"column": 4
},
"source": "input.css"
}
},
{
"type": "margin-box",
"name": "top-right",
"declarations": [
{
"type": "declaration",
"property": "content",
"value": "\"foo\"",
"position": {
"start": {
"line": 27,
"column": 5
},
"end": {
"line": 27,
"column": 19
},
"source": "input.css"
}
}
],
"position": {
"start": {
"line": 26,
"column": 3
},
"end": {
"line": 28,
"column": 4
},
"source": "input.css"
}
}
],
"position": {
"start": {
"line": 19,
"column": 1
},
"end": {
"line": 29,
"column": 2
},
"source": "input.css"
}
}
]
],
"parsingErrors": []
}
}
}
2 changes: 1 addition & 1 deletion test/cases/paged-media/compressed.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@page toc, index:blank{color:green;}@page {font-size:16pt;}@page :left{margin-left:5cm;}
@page toc, index:blank{color:green;}@page {font-size:16pt;}@page :left{margin-left:5cm;}@page named-page{size:A4 landscape;}@page {size:210mm 279mm;@top-left {padding-top:0;}@top-right {content:"foo";}}
16 changes: 16 additions & 0 deletions test/cases/paged-media/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@
@page :left {
margin-left: 5cm;
}

@page named-page {
size: A4 landscape;
}

@page {
size: 210mm 279mm;

@top-left {
padding-top: 0;
}

@top-right {
content: "foo";
}
}
14 changes: 14 additions & 0 deletions test/cases/paged-media/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@
@page :left {
margin-left: 5cm;
}

@page named-page {
size: A4 landscape;
}

@page {
size: 210mm 279mm;
@top-left {
padding-top: 0;
}
@top-right {
content: "foo";
}
}