Skip to content

Feature - the following functions would be useful: top(), bottom(), left(), right() #1110

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

Closed
wants to merge 6 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
32 changes: 32 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,39 @@ tree.functions = {
},
shade: function(color, amount) {
return this.mix(this.rgb(0, 0, 0), color, amount);
},

/* Shorthand Extraction */

top: function(n) {
return this._extractPadding('top', n);
},
right: function(n) {
return this._extractPadding('right', n);
},
bottom: function(n) {
return this._extractPadding('bottom', n);
},
left: function(n) {
return this._extractPadding('left', n);
},
_extractPadding: function(property, n) {
if (n instanceof tree.Dimension) {
return n;
} else if (n && typeof(n.length) === 'number') {
// Extract the padding property from the shorthand value:
var index = ['top', 'right', 'bottom', 'left'].indexOf(property);
if (n.length === 3 && index === 3) {
// left(1px 2px 3px) should return "2px"; this is the only case where the math would be complicated
return n[1];
} else {
return n[index % n.length];
}
} else {
throw { type: "Argument", message: "argument must be a Dimension" };
}
}

};

function hsla(color) {
Expand Down
18 changes: 18 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,21 @@
average: #7b007b;
negation: #d73131;
}
#padding-extraction {
top: 1px;
right: 2px;
bottom: 3px;
left: 4px;
top: 1px;
right: 2px;
bottom: 3px;
left: 2px;
top: 1px;
right: 2px;
bottom: 1px;
left: 2px;
top: 1px;
right: 1px;
bottom: 1px;
left: 1px;
}
30 changes: 30 additions & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,33 @@
average: average(#f60000, #0000f6);
negation: negation(#f60000, #313131);
}

#padding-extraction {
// Four:
@pad: 1px 2px 3px 4px;
top: top(@pad);
right: right(@pad);
bottom: bottom(@pad);
left: left(@pad);

// Three:
@pad: 1px 2px 3px;
top: top(@pad);
right: right(@pad);
bottom: bottom(@pad);
left: left(@pad);

// Two:
@pad: 1px 2px;
top: top(@pad);
right: right(@pad);
bottom: bottom(@pad);
left: left(@pad);

// One:
@pad: 1px;
top: top(@pad);
right: right(@pad);
bottom: bottom(@pad);
left: left(@pad);
}