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

Propose an alternative way to solve regression #38

Merged
merged 3 commits into from
Nov 24, 2015
Merged
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
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