Skip to content

Commit 05fc86f

Browse files
committed
Alpha update
1 parent 7cebd9c commit 05fc86f

File tree

2 files changed

+90
-37
lines changed

2 files changed

+90
-37
lines changed

dist/less-1.4.0-alpha.js

+86-33
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,6 @@ less.Parser = function Parser(env) {
941941

942942
option = option && option[1];
943943

944-
if (option != "all") {
945-
error(":extend only supports the all option at the moment, please specify it after your selector, e.g. :extend(.a all)");
946-
}
947-
948944
if (isRule) {
949945
expect(/^;/);
950946
}
@@ -3155,6 +3151,25 @@ tree.Extend = function Extend(selector, option, index) {
31553151
this.selector = selector;
31563152
this.option = option;
31573153
this.index = index;
3154+
3155+
switch(option) {
3156+
case "all":
3157+
this.deep = true;
3158+
this.any = true;
3159+
break;
3160+
case "deep":
3161+
this.deep = true;
3162+
this.any = false;
3163+
break;
3164+
case "any":
3165+
this.deep = false;
3166+
this.any = true;
3167+
break;
3168+
default:
3169+
this.deep = false;
3170+
this.any = false;
3171+
break;
3172+
}
31583173
};
31593174

31603175
tree.Extend.prototype = {
@@ -4958,24 +4973,51 @@ tree.jsify = function (obj) {
49584973
for(k = 0; k < allExtends.length; k++) {
49594974
for(i = 0; i < rulesetNode.paths.length; i++) {
49604975
selectorPath = rulesetNode.paths[i];
4961-
var match = this.findMatch(allExtends[k], selectorPath);
4962-
if (match) {
4963-
selector = selectorPath[match.pathIndex];
4976+
var matches = this.findMatch(allExtends[k], selectorPath);
4977+
if (matches.length) {
49644978
allExtends[k].selfSelectors.forEach(function(selfSelector) {
4965-
var path = selectorPath.slice(0, match.pathIndex),
4966-
firstElement = new tree.Element(
4967-
match.initialCombinator,
4968-
selfSelector.elements[0].value,
4969-
selfSelector.elements[0].index
4970-
);
4971-
path.push(new tree.Selector(
4972-
selector.elements
4973-
.slice(0, match.index)
4974-
.concat([firstElement])
4975-
.concat(selfSelector.elements.slice(1))
4976-
.concat(selector.elements.slice(match.index + match.length))
4977-
));
4978-
path = path.concat(selectorPath.slice(match.endPathIndex + 1, selectorPath.length));
4979+
var currentSelectorPathIndex = 0,
4980+
currentSelectorPathElementIndex = 0,
4981+
path = [];
4982+
for(j = 0; j < matches.length; j++) {
4983+
match = matches[j];
4984+
var selector = selectorPath[match.pathIndex],
4985+
firstElement = new tree.Element(
4986+
match.initialCombinator,
4987+
selfSelector.elements[0].value,
4988+
selfSelector.elements[0].index
4989+
);
4990+
4991+
if (match.pathIndex > currentSelectorPathIndex && currentSelectorPathElementIndex > 0) {
4992+
path[path.length-1].elements = path[path.length-1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
4993+
currentSelectorPathElementIndex = 0;
4994+
currentSelectorPathIndex++;
4995+
}
4996+
4997+
path = path.concat(selectorPath.slice(currentSelectorPathIndex, match.pathIndex));
4998+
4999+
path.push(new tree.Selector(
5000+
selector.elements
5001+
.slice(currentSelectorPathElementIndex, match.index)
5002+
.concat([firstElement])
5003+
.concat(selfSelector.elements.slice(1))
5004+
));
5005+
currentSelectorPathIndex = match.endPathIndex;
5006+
currentSelectorPathElementIndex = match.endPathElementIndex;
5007+
if (currentSelectorPathElementIndex >= selector.elements.length) {
5008+
currentSelectorPathElementIndex = 0;
5009+
currentSelectorPathIndex++;
5010+
}
5011+
}
5012+
5013+
if (currentSelectorPathIndex < selectorPath.length && currentSelectorPathElementIndex > 0) {
5014+
path[path.length-1].elements = path[path.length-1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
5015+
currentSelectorPathElementIndex = 0;
5016+
currentSelectorPathIndex++;
5017+
}
5018+
5019+
path = path.concat(selectorPath.slice(currentSelectorPathIndex, selectorPath.length));
5020+
49795021
selectorsToAdd.push(path);
49805022
});
49815023
}
@@ -4984,29 +5026,41 @@ tree.jsify = function (obj) {
49845026
rulesetNode.paths = rulesetNode.paths.concat(selectorsToAdd);
49855027
},
49865028
findMatch: function (extend, selectorPath) {
4987-
var i, j, k, l, targetElementIndex, element, hasMatch, potentialMatches = [], potentialMatch, matches = [];
5029+
var i, k, l, element, hasMatch, potentialMatches = [], potentialMatch, matches = [];
49885030
for(k = 0; k < selectorPath.length; k++) {
49895031
selector = selectorPath[k];
49905032
for(i = 0; i < selector.elements.length; i++) {
4991-
potentialMatches.push({pathIndex: k, index: i, matched: 0});
5033+
if (extend.any || (k == 0 && i == 0)) {
5034+
potentialMatches.push({pathIndex: k, index: i, matched: 0, initialCombinator: selector.elements[i].combinator});
5035+
}
49925036

49935037
for(l = 0; l < potentialMatches.length; l++) {
49945038
potentialMatch = potentialMatches[l];
4995-
targetElementIndex = i;
4996-
for(j = potentialMatch.matched; j < extend.selector.elements.length && targetElementIndex < selector.elements.length; j++, targetElementIndex++) {
4997-
potentialMatch.matched = j + 1;
4998-
if (extend.selector.elements[j].value !== selector.elements[targetElementIndex].value ||
4999-
(j > 0 && extend.selector.elements[j].combinator.value !== selector.elements[targetElementIndex].combinator.value)) {
5039+
5040+
var targetCombinator = selector.elements[i].combinator.value;
5041+
if (targetCombinator == '' && i === 0) {
5042+
targetCombinator = ' ';
5043+
}
5044+
if (extend.selector.elements[potentialMatch.matched].value !== selector.elements[i].value ||
5045+
(potentialMatch.matched > 0 && extend.selector.elements[potentialMatch.matched].combinator.value !== targetCombinator)) {
5046+
potentialMatch = null;
5047+
} else {
5048+
potentialMatch.matched++;
5049+
}
5050+
5051+
if (potentialMatch) {
5052+
potentialMatch.finished = potentialMatch.matched === extend.selector.elements.length;
5053+
if (potentialMatch.finished &&
5054+
(!extend.deep && (i+1 < selector.elements.length ||
5055+
k+1 < selectorPath.length))) {
50005056
potentialMatch = null;
5001-
break;
50025057
}
50035058
}
50045059
if (potentialMatch) {
5005-
if (potentialMatch.matched === extend.selector.elements.length) {
5006-
potentialMatch.initialCombinator = selector.elements[i].combinator;
5060+
if (potentialMatch.finished) {
50075061
potentialMatch.length = extend.selector.elements.length;
50085062
potentialMatch.endPathIndex = k;
5009-
return potentialMatch;
5063+
potentialMatch.endPathElementIndex = i+1; // index after end of match
50105064
potentialMatches.length = 0;
50115065
matches.push(potentialMatch);
50125066
break;
@@ -5018,7 +5072,6 @@ tree.jsify = function (obj) {
50185072
}
50195073
}
50205074
}
5021-
return null;
50225075
return matches;
50235076
},
50245077
visitRulesetOut: function (rulesetNode) {

0 commit comments

Comments
 (0)