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

Add Sass like extend #509

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var less = {
'selector', 'quoted', 'expression', 'rule',
'call', 'url', 'alpha', 'import',
'mixin', 'comment', 'anonymous', 'value',
'javascript', 'assignment'
'javascript', 'assignment', 'extend'
].forEach(function (n) {
require('./tree/' + n);
});
Expand Down
20 changes: 19 additions & 1 deletion lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ less.Parser = function Parser(env) {
var node, root = [];

while ((node = $(this.mixin.definition) || $(this.rule) || $(this.ruleset) ||
$(this.mixin.call) || $(this.comment) || $(this.directive))
$(this.mixin.call) || $(this.comment) || $(this.directive) ||
$(this.extend))
|| $(/^[\s\n]+/)) {
node && root.push(node);
}
Expand Down Expand Up @@ -672,6 +673,23 @@ less.Parser = function Parser(env) {
}
},

//
// extend
//
extend: function() {
var elements = [], e, c, args, index = i, s = input.charAt(i);

if (s !== '+') { return }

while (e = $(/^\+[#.](?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+/)) {
elements.push(new(tree.Element)(c, e.slice(1), i));
}

if (elements.length > 0 && ($(';') || peek('}'))) {
return new(tree.Extend)(elements, index);
}
},

//
// Mixins
//
Expand Down
56 changes: 56 additions & 0 deletions lib/less/tree/extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(function (tree) {

tree.Extend = function Extend(elements, index) {
this.selector = new(tree.Selector)(elements);
this.index = index;
};

tree.Extend.prototype.eval = function Extend_eval(env) {
var selfSelectors = findSelfSelectors(env.selectors),
targetValue = this.selector.elements[0].value;

env.frames.forEach(function(frame) {
frame.rulesets().forEach(function(rule) {
rule.selectors.forEach(function(selector) {
selector.elements.forEach(function(element, idx) {
if (element.value === targetValue) {
selfSelectors.forEach(function(_selector) {
_selector.elements[0] = new tree.Element(
element.combinator,
_selector.elements[0].value,
_selector.elements[0].index
);
rule.selectors.push(new tree.Selector(
selector.elements
.slice(0, idx)
.concat(_selector.elements)
.concat(selector.elements.slice(idx + 1))
));
});
}
});
});
});
});
return this;
};

function findSelfSelectors(selectors) {
var ret = [];

(function loop(elem, i) {
if (selectors[i] && selectors[i].length) {
selectors[i].forEach(function(s) {
loop(s.elements.concat(elem), i + 1);
});
}
else {
ret.push({ elements: elem });
}
})([], 0);

return ret;
}


})(require('../tree'));
7 changes: 7 additions & 0 deletions lib/less/tree/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ tree.Ruleset.prototype = {
// push the current ruleset to the frames stack
env.frames.unshift(ruleset);

// currrent selectors
if (!env.selectors) {
env.selectors = [];
}
env.selectors.unshift(this.selectors);

// Evaluate imports
if (ruleset.root) {
for (var i = 0; i < ruleset.rules.length; i++) {
Expand Down Expand Up @@ -51,6 +57,7 @@ tree.Ruleset.prototype = {

// Pop the stack
env.frames.shift();
env.selectors.shift();

return ruleset;
},
Expand Down
15 changes: 15 additions & 0 deletions test/css/extend-clearfix.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.clearfix, .foo, .bar {
*zoom: 1;
}
.clearfix:after, .foo:after, .bar:after {
content: '';
display: block;
clear: both;
height: 0;
}
.foo {
color: red;
}
.bar {
color: blue;
}
15 changes: 15 additions & 0 deletions test/css/extend-nest.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.sidebar, .sidebar2, .type1 .sidebar3 {
width: 300px;
background: red;
}
.sidebar .box, .sidebar2 .box, .type1 .sidebar3 .box {
background: #FFF;
border: 1px solid #000;
margin: 10px 0;
}
.sidebar2 {
background: blue;
}
.type1 .sidebar3 {
background: green;
}
30 changes: 30 additions & 0 deletions test/css/extend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.error, .badError {
border: 1px #f00;
background: #fdd;
}
.error.intrusion, .badError.intrusion {
font-size: 1.3em;
font-weight: bold;
}
.intrusion .error, .intrusion .badError {
display: none;
}
.badError {
border-width: 3px;
}
.foo .bar,
.foo .baz,
.ext1 .ext2 .bar,
.ext1 .ext2 .baz,
.ext3 .bar,
.ext4 .bar,
.ext3 .baz,
.ext4 .baz {
display: none;
}
div.ext5,
.ext6 > .ext5,
div.ext7,
.ext6 > .ext7 {
width: 100px;
}
19 changes: 19 additions & 0 deletions test/less/extend-clearfix.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.clearfix {
*zoom: 1;
&:after {
content: '';
display: block;
clear: both;
height: 0;
}
}

.foo {
+.clearfix;
color: red;
}

.bar {
+.clearfix;
color: blue;
}
22 changes: 22 additions & 0 deletions test/less/extend-nest.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.sidebar {
width: 300px;
background: red;

.box {
background: #FFF;
border: 1px solid #000;
margin: 10px 0;
}
}

.sidebar2 {
+.sidebar;
background: blue;
}

.type1 {
.sidebar3 {
+.sidebar;
background: green;
}
}
35 changes: 35 additions & 0 deletions test/less/extend.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.error {
border: 1px #f00;
background: #fdd;
}
.error.intrusion {
font-size: 1.3em;
font-weight: bold;
}
.intrusion .error {
display: none;
}
.badError {
+.error;
border-width: 3px;
}

.foo .bar, .foo .baz {
display: none;
}

.ext1 .ext2 {
+.foo;
}

.ext3, .ext4 {
+.foo;
}

div.ext5, .ext6 > .ext5 {
width: 100px;
}

.ext7 {
+.ext5;
}