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

nested rule parsing #1

Open
wants to merge 1 commit 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
55 changes: 44 additions & 11 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,27 @@ export const createRule = node => {
const declarations = node.nodes || [];
const style = {};

declarations.forEach(({ prop, value, important }) => {
style[prop] = value + (important ? ' !important' : '');
});
if (declarations.length > 0) {
declarations.forEach(({prop, value, important}) => {
style[prop] = value + (important ? ' !important' : '');
});
}

return {
selectors: (node.parent.type == 'atrule' && node.type == 'atrule' ? '@' + node.name : node.selector) || '',
style,
}
};

/**
* Add declaration to existing rule
* @param {Object} node
* @return {Object}
*/
export const addDeclaration = (node, style) => {
let {prop, value, important} = node;

style[prop] = value + (important ? ' !important' : '');

return {
selectors: node.selector || '',
Expand All @@ -34,16 +52,31 @@ export const createRule = node => {
* @return {Object}
*/
export const createAtRule = (node, result) => {
const { name, params } = node;
const isNested = ['media', 'keyframes'].indexOf(name) >= 0;
let { name, params } = node;
const isNested = ['media', 'page', 'keyframes'].indexOf(name) >= 0;

if (isNested) {
node.nodes.forEach(node => {
result.push({
...createRule(node),
atRule: name,
params,
})
const style = {};
let ruleCreated = false;
node.nodes.forEach(cnode => {
if (cnode.type == 'decl') {
addDeclaration(cnode, style);
if (!ruleCreated) {
result.push({
selectors: '',
style,
atRule: name,
params,
});
ruleCreated = true;
}
} else {
result.push({
...createRule(cnode),
atRule: name,
params,
})
}
});
} else {
result.push({
Expand Down
115 changes: 115 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,121 @@ describe('Parser', () => {
]);
});

it('returns the correct object of a simple @page rule', () => {
expect(parser(`@page Normal {
size: 21.0cm 29.7cm;
margin: 2.5cm;
}`)).toEqual([
{
params: 'Normal',
selectors: '',
atRule: 'page',
style: {
size: '21.0cm 29.7cm',
margin: '2.5cm',
},
},
]);
});

it('returns the correct object of a @page rule', () => {
expect(parser(`@page Normal {
size: 21.0cm 29.7cm;
margin: 2.5cm;
@top-left-corner {
content: "@top-left-corner";
background-color: red;
padding: 10px;
border: 1px solid black;
}
}`)).toEqual([
{
params: 'Normal',
selectors: '',
atRule: 'page',
style: {
size: '21.0cm 29.7cm',
margin: '2.5cm',
},
}, {
params: 'Normal',
selectors: '@top-left-corner',
atRule: 'page',
style: {
content: '"@top-left-corner"',
'background-color': "red",
padding: "10px",
border: "1px solid black",
},
}
]);
});

it('returns the correct object of a @page rule', () => {
expect(parser(`@page Normal {
size: 21.0cm 29.7cm;
margin: 2.5cm;
@top-left {
content: "@top-left";
background-color: orange;
padding: 10px;
border: 2px solid blue;
}
@top-center {
content: "@top-center";
background-color: yellow;
padding: 10px;
border: 3px solid green;
}
@top-right {
content: "@top-right";
background-color: deeppink;
padding: 10px;
border: 1px solid magenta;
}
}`)).toEqual([
{
params: 'Normal',
selectors: '',
atRule: 'page',
style: {
size: '21.0cm 29.7cm',
margin: '2.5cm',
},
}, {
params: 'Normal',
selectors: '@top-left',
atRule: 'page',
style: {
content: '"@top-left"',
'background-color': "orange",
padding: "10px",
border: "2px solid blue",
},
}, {
params: 'Normal',
selectors: '@top-center',
atRule: 'page',
style: {
content: '"@top-center"',
'background-color': "yellow",
padding: "10px",
border: "3px solid green",
},
}, {
params: 'Normal',
selectors: '@top-right',
atRule: 'page',
style: {
content: '"@top-right"',
'background-color': "deeppink",
padding: "10px",
border: "1px solid magenta",
},
}
]);
});

it('returns the correct object of a simple id rule', () => {
expect(parser(`#main {
border: 1px solid black;
Expand Down