Skip to content

Commit

Permalink
basic support for at-rule inside rule block
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 16, 2017
1 parent f57a886 commit a95fb58
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ function getBlock() {
scanner.next();
break;

case COMMERCIALAT:
children.appendData(getAtrule());
break;

default:
children.appendData(getDeclaration());
}
Expand Down
31 changes: 28 additions & 3 deletions lib/utils/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function walkRules(node, item, list) {

case 'Rule':
this.fn(node, item, list);

var oldRule = this.rule;
this.rule = node;

node.block.children.each(walkRules, this);

this.rule = oldRule;
break;
}

Expand All @@ -46,6 +53,13 @@ function walkRulesRight(node, item, list) {
break;

case 'Rule':
var oldRule = this.rule;
this.rule = node;

node.block.children.eachRight(walkRulesRight, this);

this.rule = oldRule;

this.fn(node, item, list);
break;
}
Expand All @@ -63,19 +77,30 @@ function walkDeclarations(node) {
break;

case 'Atrule':
if (node.block !== null && node.block.type === 'StyleSheet') {
if (node.block !== null) {
walkDeclarations.call(this, node.block);
}
break;

case 'Rule':
var oldRule = this.rule;
this.rule = node;

if (node.block !== null) {
node.block.children.each(this.fn);
walkDeclarations.call(this, node.block);
}

this.rule = null;
this.rule = oldRule;
break;

case 'Block':
node.children.each(function(node, item, list) {
if (node.type === 'Declaration') {
this.fn(node, item, list);
} else {
walkDeclarations.call(this, node);
}
}, this);
break;
}
}
Expand Down
198 changes: 198 additions & 0 deletions test/fixture/parse/rule/nested-atrule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{
"basic": {
"source": ".test{@test;}",
"ast": {
"type": "Rule",
"selector": {
"type": "SelectorList",
"children": [
{
"type": "Selector",
"children": [
{
"type": "Class",
"name": "test"
}
]
}
]
},
"block": {
"type": "Block",
"children": [
{
"type": "Atrule",
"name": "test",
"expression": {
"type": "AtruleExpression",
"children": []
},
"block": null
}
]
}
}
},
"with expression": {
"source": ".test{@test not(something);}",
"ast": {
"type": "Rule",
"selector": {
"type": "SelectorList",
"children": [
{
"type": "Selector",
"children": [
{
"type": "Class",
"name": "test"
}
]
}
]
},
"block": {
"type": "Block",
"children": [
{
"type": "Atrule",
"name": "test",
"expression": {
"type": "AtruleExpression",
"children": [
{
"type": "Function",
"name": "not",
"children": [
{
"type": "Identifier",
"name": "something"
}
]
}
]
},
"block": null
}
]
}
}
},
"with block": {
"source": ".test{@test foo{a:1}}",
"ast": {
"type": "Rule",
"selector": {
"type": "SelectorList",
"children": [
{
"type": "Selector",
"children": [
{
"type": "Class",
"name": "test"
}
]
}
]
},
"block": {
"type": "Block",
"children": [
{
"type": "Atrule",
"name": "test",
"expression": {
"type": "AtruleExpression",
"children": [
{
"type": "Identifier",
"name": "foo"
}
]
},
"block": {
"type": "Block",
"children": [
{
"type": "Declaration",
"important": false,
"property": "a",
"value": {
"type": "Value",
"children": [
{
"type": "Number",
"value": "1"
}
]
}
}
]
}
}
]
}
}
},
"recursion": {
"source": ".test{@test foo{@test foo{}}}",
"ast": {
"type": "Rule",
"selector": {
"type": "SelectorList",
"children": [
{
"type": "Selector",
"children": [
{
"type": "Class",
"name": "test"
}
]
}
]
},
"block": {
"type": "Block",
"children": [
{
"type": "Atrule",
"name": "test",
"expression": {
"type": "AtruleExpression",
"children": [
{
"type": "Identifier",
"name": "foo"
}
]
},
"block": {
"type": "StyleSheet",
"children": [
{
"type": "Atrule",
"name": "test",
"expression": {
"type": "AtruleExpression",
"children": [
{
"type": "Identifier",
"name": "foo"
}
]
},
"block": {
"type": "Block",
"children": []
}
}
]
}
}
]
}
}
}
}
24 changes: 11 additions & 13 deletions test/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function createWalkTest(name, test, context, walker, right) {
});

// type arrays should be equal
assert.equal(
actual.sort().join(','),
expectedWalk(test.ast, right).sort().join(',')
assert.deepEqual(
actual.sort(),
expectedWalk(test.ast, right).sort()
);
});
}
Expand All @@ -79,11 +79,11 @@ function createWalkRulesTest(test, context, walker) {
});

// type arrays should be equal
assert.equal(
actual.sort().join(','),
assert.deepEqual(
actual.sort(),
expectedWalk(test.ast, true).filter(function(type) {
return type === 'Rule' || type === 'Atrule';
}).sort().join(',')
}).sort()
);
});
}
Expand All @@ -100,13 +100,11 @@ function createWalkDeclarationsTest(test, context, walker) {
});

// type arrays should be equal
assert.equal(
actual.sort().join(','),
expectedWalk(test.ast, false, function(stack, node) {
return node.type === 'Declaration' && stack.some(function(node) {
return node.type === 'Rule';
});
}).sort().join(',')
assert.deepEqual(
actual.sort(),
expectedWalk(test.ast, false).filter(function(type) {
return type === 'Declaration';
}).sort()
);
});
}
Expand Down

0 comments on commit a95fb58

Please sign in to comment.