Skip to content

Commit 1f97c27

Browse files
committed
Issue 122: Media Query variable parsing - Updated the Directive class to parse variables in media query statements. Add unit test
1 parent d1b52a0 commit 1f97c27

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

lib/less/parser.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,14 @@ less.Parser = function Parser(env) {
972972

973973
if (value = $(this['import'])) {
974974
return value;
975-
} else if (name = $(/^@media|@page/) || $(/^@(?:-webkit-|-moz-)?keyframes/)) {
975+
} else if (name = $(/^@media/)) {
976+
var nodes = [], n;
977+
while(n = $(this.entity) || $(/^[\(|\)|:]/)) {
978+
nodes.push( (typeof n == 'string'? new tree.Keyword(n):n));
979+
}
980+
rules = $(this.block);
981+
return new(tree.Directive)(name, rules, nodes);
982+
} else if (name = $(/^@page/) || $(/^@(?:-webkit-|-moz-)?keyframes/)) {
976983
types = ($(/^[^{]+/) || '').trim();
977984
if (rules = $(this.block)) {
978985
return new(tree.Directive)(name + " " + types, rules);

lib/less/tree/directive.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
(function (tree) {
22

3-
tree.Directive = function (name, value) {
3+
tree.Directive = function (name, value, nodes) {
44
this.name = name;
55
if (Array.isArray(value)) {
66
this.ruleset = new(tree.Ruleset)([], value);
77
} else {
88
this.value = value;
99
}
10+
this.nodes = (typeof(nodes) != 'undefined' ? nodes : null);
1011
};
1112
tree.Directive.prototype = {
1213
toCSS: function (ctx, env) {
14+
var node_css = '';
15+
if(this.nodes) {
16+
for(var n in this.nodes) {
17+
node_css += ' ' + this.nodes[n].toCSS(ctx, env) ;
18+
}
19+
/* Remove extra spaces in query synax so unit tests pass */
20+
node_css = node_css.replace(/\(\s+([^\s]+)\s+:\s+([^\s]+)\s\)/g,'($1:$2)');
21+
}
1322
if (this.ruleset) {
1423
this.ruleset.root = true;
15-
return this.name + (env.compress ? '{' : ' {\n ') +
24+
return this.name + node_css +(env.compress ? '{' : ' {\n ') +
1625
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
1726
(env.compress ? '}': '\n}\n');
1827
} else {
19-
return this.name + ' ' + this.value.toCSS() + ';\n';
28+
return this.name + node_css + ' ' + this.value.toCSS() + ';\n';
2029
}
2130
},
2231
eval: function (env) {
32+
if(this.nodes) {
33+
for(var i = 0; i < this.nodes.length; i++) {
34+
this.nodes[i] = this.nodes[i].eval(env);
35+
}
36+
}
2337
env.frames.unshift(this);
2438
this.ruleset = this.ruleset && this.ruleset.eval(env);
2539
env.frames.shift();

test/css/media.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@
1919
float: none;
2020
}
2121
}
22+
@media screen and (min-width:42) {
23+
div {
24+
color: #222;
25+
}
26+
}

test/less/media.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@
2323
@media all and (orientation:portrait) {
2424
aside { float: none; }
2525
}
26+
@media screen and (min-width: @var) {
27+
div { color: #222; }
28+
}

0 commit comments

Comments
 (0)