Skip to content

Added a new function for use in fluid/responsive layouts #481

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 2 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
3 changes: 3 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ tree.functions = {
argb: function (color) {
return new(tree.Anonymous)(color.toARGB());

},
fluid: function(target, container){
return new tree.Dimension((target.value / container.value) * 100.0, '%');
}
};

Expand Down
9 changes: 8 additions & 1 deletion lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,14 @@ less.Parser = function Parser(env) {

if (value = $(this['import'])) {
return value;
} else if (name = $(/^@media|@page/) || $(/^@(?:-webkit-|-moz-)?keyframes/)) {
} else if (name = $(/^@media/)) {
var nodes = [], n;
while(n = $(this.entity) || $(/^[\(|\)|:]/)) {
nodes.push( (typeof n == 'string'? new tree.Keyword(n):n));
}
rules = $(this.block);
return new(tree.Directive)(name, rules, nodes);
} else if (name = $(/^@page/) || $(/^@(?:-webkit-|-moz-)?keyframes/)) {
types = ($(/^[^{]+/) || '').trim();
if (rules = $(this.block)) {
return new(tree.Directive)(name + " " + types, rules);
Expand Down
20 changes: 17 additions & 3 deletions lib/less/tree/directive.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
(function (tree) {

tree.Directive = function (name, value) {
tree.Directive = function (name, value, nodes) {
this.name = name;
if (Array.isArray(value)) {
this.ruleset = new(tree.Ruleset)([], value);
} else {
this.value = value;
}
this.nodes = (typeof(nodes) != 'undefined' ? nodes : null);
};
tree.Directive.prototype = {
toCSS: function (ctx, env) {
var node_css = '';
if(this.nodes) {
for(var n in this.nodes) {
node_css += ' ' + this.nodes[n].toCSS(ctx, env) ;
}
/* Remove extra spaces in query synax so unit tests pass */
node_css = node_css.replace(/\(\s+([^\s]+)\s+:\s+([^\s]+)\s\)/g,'($1:$2)');
}
if (this.ruleset) {
this.ruleset.root = true;
return this.name + (env.compress ? '{' : ' {\n ') +
return this.name + node_css +(env.compress ? '{' : ' {\n ') +
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
(env.compress ? '}': '\n}\n');
} else {
return this.name + ' ' + this.value.toCSS() + ';\n';
return this.name + node_css + ' ' + this.value.toCSS() + ';\n';
}
},
eval: function (env) {
if(this.nodes) {
for(var i = 0; i < this.nodes.length; i++) {
this.nodes[i] = this.nodes[i].eval(env);
}
}
env.frames.unshift(this);
this.ruleset = this.ruleset && this.ruleset.eval(env);
env.frames.shift();
Expand Down
1 change: 1 addition & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
height: undefined("self");
border-width: 5;
variable: 11;
padding: 5%;
}
#built-in {
escaped: -Some::weird(#thing, y);
Expand Down
5 changes: 5 additions & 0 deletions test/css/media.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
float: none;
}
}
@media screen and (min-width:42) {
div {
color: #222;
}
}
1 change: 1 addition & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
height: undefined("self");
border-width: add(2, 3);
variable: increment(@var);
padding: fluid(10px, 200px);
}

#built-in {
Expand Down
3 changes: 3 additions & 0 deletions test/less/media.less
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
@media all and (orientation:portrait) {
aside { float: none; }
}
@media screen and (min-width: @var) {
div { color: #222; }
}