-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Cache match elements to speed up compiler #1831
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,33 +32,59 @@ tree.Selector.prototype = { | |
match: function (other) { | ||
var elements = this.elements, | ||
len = elements.length, | ||
oelements, olen, i; | ||
|
||
oelements = other.elements.map( function(v) { | ||
return v.combinator.value + (v.value.value || v.value); | ||
}).join("").match(/[,&#\.\w-]([\w-]|(\\.))*/g); | ||
// ^ regexp could be more simple but see test/less/css-escapes.less:17, doh! | ||
|
||
if (!oelements) { | ||
return 0; | ||
} | ||
olen, i; | ||
|
||
if (oelements[0] === "&") { | ||
oelements.shift(); | ||
} | ||
other.CacheElements(); | ||
|
||
olen = oelements.length; | ||
olen = other._elements.length; | ||
if (olen === 0 || len < olen) { | ||
return 0; | ||
} else { | ||
for (i = 0; i < olen; i++) { | ||
if (elements[i].value !== oelements[i]) { | ||
if (elements[i].value !== other._elements[i]) { | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
return olen; // return number of matched elements | ||
}, | ||
CacheElements: function(){ | ||
var css = '', len, v, i; | ||
|
||
if( !this._elements ){ | ||
|
||
len = this.elements.length; | ||
for(i = 0; i < len; i++){ | ||
|
||
v = this.elements[i]; | ||
css += v.combinator.value; | ||
|
||
if( !v.value.value ){ | ||
css += v.value; | ||
continue; | ||
} | ||
|
||
if( typeof v.value.value !== "string" ){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This make sense. But unfortunately there're too many possible value types beyond a string (e.g. an array, an expression etc.) so it still remains broken for "complex" variable values... Ideally we should use I was also thinking of something like that (i.e. caching + proper |
||
css = ''; | ||
break; | ||
} | ||
css += v.value.value; | ||
} | ||
|
||
this._elements = css.match(/[,&#\.\w-]([\w-]|(\\.))*/g); | ||
|
||
if (this._elements) { | ||
if (this._elements[0] === "&") { | ||
this._elements.shift(); | ||
} | ||
|
||
}else{ | ||
this._elements = []; | ||
} | ||
|
||
} | ||
}, | ||
eval: function (env) { | ||
var evaldCondition = this.condition && this.condition.eval(env), | ||
elements = this.elements, extendList = this.extendList; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, var
i
is not declared here (so grunt test fails).