Skip to content

Commit

Permalink
Support for mixin nested selectors & multiple values for same property
Browse files Browse the repository at this point in the history
Test for Mixins that Nest Selectors ryanbahniuk#13.
Fixed issue with properties that have fallback.
E.g.: padding: 16px;
padding: 1rem;
  • Loading branch information
rtrufin committed Jun 1, 2016
1 parent 95a7710 commit 2337dab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/mixinResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ MixinResult.prototype = {
assert.equal(declarationValue, value.toString(), message);
},

declaresProperties: function(property, value) {
var declarations = parsers.findDeclarations(this.ast, property);
var declarationValues = [];
declarations.forEach(function(declaration){
declarationValues.push(declaration.value);
});
var message = 'Value: ' + declarationValues + ' does not equal value: ' + value + '.';
assert.equal(declarationValues.toString(), value.toString(), message);
},

declaresInSelector: function(selector, property, value) {
var declaration = parsers.findDeclarationInSelector(this.ast, selector, property);
var declarationValue = declaration ? utilities.scrubQuotes(declaration.value) : '';
var message = 'In Selector:'+ selector + ' the value: ' + declarationValue + ' does not equal value: ' + value + '.';
assert.equal(declarationValue, value.toString(), message);
},

doesNotDeclare: function(property, value) {
var declaration = parsers.findDeclaration(this.ast, property);
var declarationValue = declaration ? utilities.scrubQuotes(declaration.value) : '';
Expand Down
49 changes: 45 additions & 4 deletions src/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,28 @@ function hasSelectorValue(rule, selectorValue) {

function findDeclarationProperty(rule, declarationProperty) {
var foundDeclaration;

if (rule.declarations) {
rule.declarations.forEach(function(declaration) {
if (declaration.property === declarationProperty) {
foundDeclaration = declaration;
}
});
}

return foundDeclaration;
}

function findDeclarationsProperties(rule, declarationProperty) {
var foundDeclarations = [];
if (rule.declarations) {
rule.declarations.forEach(function(declaration) {
if (declaration.property === declarationProperty) {
foundDeclarations.push(declaration);
}
});
}
return foundDeclarations;
}

function isFontFace(rule) {
if (rule.type === 'font-face') {
return true;
Expand All @@ -57,10 +67,8 @@ var Parsers = {
});
return count;
},

findDeclaration: function(ast, property) {
var found;

ast.stylesheet.rules.forEach(function(rule) {
if (rule.type === 'media') {
rule.rules.forEach(function(rule) {
Expand All @@ -74,6 +82,39 @@ var Parsers = {
return found;
},

findDeclarations: function(ast, property) {
var found;
ast.stylesheet.rules.forEach(function(rule) {
if (rule.type === 'media') {
rule.rules.forEach(function(rule) {
found = found || findDeclarationsProperties(rule, property);
});
} else {
found = found || findDeclarationsProperties(rule, property);
}
});

return found;
},

findDeclarationInSelector: function(ast, selector, property) {
var found;
if (selector.indexOf("&") > -1) {
selector = selector.replace("&", ".test");
}
if (selector.indexOf("@at-root") > -1) {
selector = selector.replace("@at-root ", "");
}
ast.stylesheet.rules.forEach(function(rule) {
for(var i = 0; i < rule.selectors.length; i++) {
if(rule.selectors[i].indexOf(selector) >= 0 ) {
found = found || findDeclarationProperty(rule, property);
}
}
});
return found;
},

findMedia: function(ast) {
var found = [];

Expand Down

0 comments on commit 2337dab

Please sign in to comment.