8000 extend - replace elements multiple times in a path · nullstring/less.js@3164d57 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3164d57

Browse files
committed
extend - replace elements multiple times in a path
1 parent c90558b commit 3164d57

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

lib/less/extend-visitor.js

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,51 @@
100100
for(k = 0; k < allExtends.length; k++) {
101101
for(i = 0; i < rulesetNode.paths.length; i++) {
102102
selectorPath = rulesetNode.paths[i];
103-
var match = this.findMatch(allExtends[k], selectorPath);
104-
if (match) {
105-
selector = selectorPath[match.pathIndex];
103+
var matches = this.findMatch(allExtends[k], selectorPath);
104+
if (matches.length) {
106105
allExtends[k].selfSelectors.forEach(function(selfSelector) {
107-
var path = selectorPath.slice(0, match.pathIndex),
108-
firstElement = new tree.Element(
109-
match.initialCombinator,
110-
selfSelector.elements[0].value,
111-
selfSelector.elements[0].index
112-
);
113-
path.push(new tree.Selector(
114-
selector.elements
115-
.slice(0, match.index)
116-
.concat([firstElement])
117-
.concat(selfSelector.elements.slice(1))
118-
.concat(selector.elements.slice(match.index + match.length))
119-
));
120-
path = path.concat(selectorPath.slice(match.endPathIndex + 1, selectorPath.length));
106+
var currentSelectorPathIndex = 0,
107+
currentSelectorPathElementIndex = 0,
108+
path = [];
109+
for(j = 0; j < matches.length; j++) {
110+
match = matches[j];
111+
var selector = selectorPath[match.pathIndex],
112+
firstElement = new tree.Element(
113+
match.initialCombinator,
114+
selfSelector.elements[0].value,
115+
selfSelector.elements[0].index
116+
);
117+
118+
if (match.pathIndex > currentSelectorPathIndex && currentSelectorPathElementIndex > 0) {
119+
path[path.length-1].elements = path[path.length-1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
120+
currentSelectorPathElementIndex = 0;
121+
currentSelectorPathIndex++;
122+
}
123+
124+
path = path.concat(selectorPath.slice(currentSelectorPathIndex, match.pathIndex));
125+
126+
path.push(new tree.Selector(
127+
selector.elements
128+
.slice(currentSelectorPathElementIndex, match.index)
129+
.concat([firstElement])
130+
.concat(selfSelector.elements.slice(1))
131+
));
132+
currentSelectorPathIndex = match.endPathIndex;
133+
currentSelectorPathElementIndex = match.endPathElementIndex;
134+
if (currentSelectorPathElementIndex >= selector.elements.length) {
135+
currentSelectorPathElementIndex = 0;
136+
currentSelectorPathIndex++;
137+
}
138+
}
139+
140+
if (currentSelectorPathIndex < selectorPath.length && currentSelectorPathElementIndex > 0) {
141+
path[path.length-1].elements = path[path.length-1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
142+
currentSelectorPathElementIndex = 0;
143+
currentSelectorPathIndex++;
144+
}
145+
146+
path = path.concat(selectorPath.slice(currentSelectorPathIndex, selectorPath.length));
147+
121148
selectorsToAdd.push(path);
122149
});
123150
}
@@ -148,7 +175,7 @@
148175
potentialMatch.initialCombinator = selector.elements[i].combinator;
149176
potentialMatch.length = extend.selector.elements.length;
150177
potentialMatch.endPathIndex = k;
151-
return potentialMatch;
178+
potentialMatch.endPathElementIndex = targetElementIndex; // index after end of match
152179
potentialMatches.length = 0;
153180
matches.push(potentialMatch);
154181
break;
@@ -160,7 +187,6 @@
160187
}
161188
}
162189
}
163-
return null;
164190
return matches;
165191
},
166192
visitRulesetOut: function (rulesetNode) {

test/css/extend-selector.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ div.ext7,
4848
.b .c .d {
4949
test: 4;
5050
}
51+
.replace.replace .replace,
52+
.c.replace + .replace .replace,
53+
.replace.replace .c,
54+
.c.replace + .replace .c,
55+
.rep_ace .rep_ace .rep_ace,
56+
.c.rep_ace + .rep_ace .rep_ace,
57+
.rep_ace .rep_ace .c,
58+
.c.rep_ace + .rep_ace .c {
59+
prop: copy-paste-replace;
60+
}

test/css/extend.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ div.ext7,
4040
.ext8 .ext9,
4141
.ext8 + .ext9,
4242
.ext8 > .ext9,
43-
.bar {
43+
.bar,
44+
.zap,
45+
.zoo {
4446
result: bar-matched;
4547
}
4648
.ext8.nomatch {

test/less/extend-selector.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ div.ext5,
5151
}
5252
}
5353
}
54+
55+
.replace.replace,
56+
.c.replace + .replace {
57+
.replace,
58+
.c {
59+
prop: copy-paste-replace;
60+
}
61+
}
62+
.rep_ace:extend(.replace all) {}

test/less/extend.less

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ div.ext5,
6060
}
6161

6262
.foo:extend(.ext8.ext9 all) {}
63-
.bar:extend(.ext8 .ext9 all) {}
63+
.bar:extend(.ext8 .ext9 all) {}
64+
.zap:extend(.ext8 + .ext9 all) {}
65+
.zoo:extend(.ext8 > .ext9 all) {}

0 commit comments

Comments
 (0)
0