Skip to content

Commit

Permalink
Merge pull request #38 from Delgan/fix-regression
Browse files Browse the repository at this point in the history
Propose an alternative way to solve regression
  • Loading branch information
fb55 committed Nov 24, 2015
2 parents 09c405d + 5b9b1da commit 0d812a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function includesScopePseudo(t){
}

var DESCENDANT_TOKEN = {type: "descendant"},
FLEXIBLE_DESCENDANT_TOKEN = {type: "flexibleDescendant"},
SCOPE_TOKEN = {type: "pseudo", name: "scope"},
PLACEHOLDER_ELEMENT = {},
getParent = DomUtils.getParent;
Expand Down Expand Up @@ -83,20 +84,25 @@ function compileToken(token, options, context){

absolutize(token, context);

console.log(token);
return token
.map(function(rules){ return compileRules(rules, options, context, isArrayContext); })
.map(function(rules){
if (isArrayContext && rules[0] && rules[1] && rules[0].name === "scope" && rules[1].type === "descendant") {
rules[1] = FLEXIBLE_DESCENDANT_TOKEN;
}
return compileRules(rules, options, context);
})
.reduce(reduceRules, falseFunc);
}

function isTraversal(t){
return procedure[t.type] < 0;
}

function compileRules(rules, options, context, isArrayContext){
var acceptSelf = (isArrayContext && rules[0].name === "scope" && rules[1].type === "descendant");
return rules.reduce(function(func, rule, index){
function compileRules(rules, options, context){
return rules.reduce(function(func, rule){
if(func === falseFunc) return func;
return Rules[rule.type](func, rule, options, context, acceptSelf && index === 1);
return Rules[rule.type](func, rule, options, context);
}, options && options.rootFunc || trueFunc);
}

Expand Down
17 changes: 14 additions & 3 deletions lib/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ module.exports = {
},

//traversal
descendant: function(next, rule, options, context, acceptSelf){
descendant: function(next){
return function descendant(elem){

if (acceptSelf && next(elem)) return true;

var found = false;

while(!found && (elem = getParent(elem))){
Expand All @@ -37,6 +35,19 @@ module.exports = {
return found;
};
},
flexibleDescendant: function(next){
// Include element itself, only used while querying an array
return function descendant(elem){

var found = next(elem);

while(!found && (elem = getParent(elem))){
found = next(elem);
}

return found;
};
},
parent: function(next, data, options){
if(options && options.strict) throw SyntaxError("Parent selector isn't part of CSS3");

Expand Down

0 comments on commit 0d812a0

Please sign in to comment.