Skip to content

Commit 2723c4e

Browse files
author
Alexis Sellier
committed
Merge pull request #634 from sirlantis/patch-media
Add @media bubbling/nesting/merging
2 parents ab53430 + 1ba622d commit 2723c4e

File tree

8 files changed

+214
-10
lines changed

8 files changed

+214
-10
lines changed

lib/less/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ var less = {
8282
'selector', 'quoted', 'expression', 'rule',
8383
'call', 'url', 'alpha', 'import',
8484
'mixin', 'comment', 'anonymous', 'value',
85-
'javascript', 'assignment', 'condition', 'paren'
85+
'javascript', 'assignment', 'condition', 'paren',
86+
'media'
8687
].forEach(function (n) {
8788
require('./tree/' + n);
8889
});

lib/less/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ less.Parser = function Parser(env) {
10891089
features = $(this.mediaFeatures);
10901090

10911091
if (rules = $(this.block)) {
1092-
return new(tree.Directive)('@media', rules, features);
1092+
return new(tree.Media)(rules, features);
10931093
}
10941094
}
10951095
},

lib/less/tree/directive.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
tree.Directive = function (name, value, features) {
44
this.name = name;
5-
this.features = features && new(tree.Value)(features);
65

76
if (Array.isArray(value)) {
87
this.ruleset = new(tree.Ruleset)([], value);
@@ -13,19 +12,16 @@ tree.Directive = function (name, value, features) {
1312
};
1413
tree.Directive.prototype = {
1514
toCSS: function (ctx, env) {
16-
var features = this.features ? ' ' + this.features.toCSS(env) : '';
17-
1815
if (this.ruleset) {
1916
this.ruleset.root = true;
20-
return this.name + features + (env.compress ? '{' : ' {\n ') +
17+
return this.name + (env.compress ? '{' : ' {\n ') +
2118
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
2219
(env.compress ? '}': '\n}\n');
2320
} else {
2421
return this.name + ' ' + this.value.toCSS() + ';\n';
2522
}
2623
},
2724
eval: function (env) {
28-
this.features = this.features && this.features.eval(env);
2925
env.frames.unshift(this);
3026
this.ruleset = this.ruleset && this.ruleset.eval(env);
3127
env.frames.shift();

lib/less/tree/import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tree.Import.prototype = {
7171
[i, 1].concat(ruleset.rules[i].eval(env)));
7272
}
7373
}
74-
return this.features ? new(tree.Directive)('@media', ruleset.rules, this.features.value) : ruleset.rules;
74+
return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules;
7575
}
7676
}
7777
};

lib/less/tree/media.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
(function (tree) {
2+
3+
tree.Media = function (value, features) {
4+
var el = new(tree.Element)('&', null, 0),
5+
selectors = [new(tree.Selector)([el])];
6+
7+
this.features = new(tree.Value)(features);
8+
this.ruleset = new(tree.Ruleset)(selectors, value);
9+
this.ruleset.allowImports = true;
10+
};
11+
tree.Media.prototype = {
12+
toCSS: function (ctx, env) {
13+
var features = this.features.toCSS(env);
14+
15+
this.ruleset.root = (ctx.length === 0 || ctx[0].multiMedia);
16+
return '@media ' + features + (env.compress ? '{' : ' {\n ') +
17+
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
18+
(env.compress ? '}': '\n}\n');
19+
},
20+
eval: function (env) {
21+
if (!env.mediaBlocks) {
22+
env.mediaBlocks = [];
23+
env.mediaPath = [];
24+
}
25+
26+
var blockIndex = env.mediaBlocks.length;
27+
env.mediaPath.push(this);
28+
env.mediaBlocks.push(this);
29+
30+
var media = new(tree.Media)([], []);
31+
media.features = this.features.eval(env);
32+
33+
env.frames.unshift(this.ruleset);
34+
media.ruleset = this.ruleset.eval(env);
35+
env.frames.shift();
36+
37+
env.mediaBlocks[blockIndex] = media;
38+
env.mediaPath.pop();
39+
40+
return env.mediaPath.length === 0 ? media.evalTop(env) :
41+
media.evalNested(env)
42+
},
43+
variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
44+
find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
45+
rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) },
46+
47+
evalTop: function (env) {
48+
var result = this;
49+
50+
// Render all dependent Media blocks.
51+
if (env.mediaBlocks.length > 1) {
52+
var el = new(tree.Element)('&', null, 0);
53+
var selectors = [new(tree.Selector)([el])];
54+
result = new(tree.Ruleset)(selectors, env.mediaBlocks);
55+
result.multiMedia = true;
56+
}
57+
58+
delete env.mediaBlocks;
59+
delete env.mediaPath;
60+
61+
return result;
62+
},
63+
evalNested: function (env) {
64+
var i, value,
65+
path = env.mediaPath.concat([this]);
66+
67+
// Extract the media-query conditions separated with `,` (OR).
68+
for (i = 0; i < path.length; i++) {
69+
value = path[i].features instanceof tree.Value ?
70+
path[i].features.value : path[i].features;
71+
path[i] = Array.isArray(value) ? value : [value];
72+
}
73+
74+
// Trace all permutations to generate the resulting media-query.
75+
//
76+
// (a, b and c) with nested (d, e) ->
77+
// a and d
78+
// a and e
79+
// b and c and d
80+
// b and c and e
81+
this.features = new(tree.Value)(this.permute(path).map(function (path) {
82+
path = path.map(function (fragment) {
83+
return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment);
84+
});
85+
86+
for(i = path.length - 1; i > 0; i--) {
87+
path.splice(i, 0, new(tree.Anonymous)("and"));
88+
}
89+
90+
return new(tree.Expression)(path);
91+
}));
92+
93+
// Fake a tree-node that doesn't output anything.
94+
return new(tree.Ruleset)([], []);
95+
},
96+
permute: function (arr) {
97+
if (arr.length === 0) {
98+
return [];
99+
} else if (arr.length === 1) {
100+
return arr[0];
101+
} else {
102+
var result = [];
103+
var rest = this.permute(arr.slice(1));
104+
for (var i = 0; i < rest.length; i++) {
105+
for (var j = 0; j < arr[0].length; j++) {
106+
result.push([arr[0][j]].concat(rest[i]));
107+
}
108+
}
109+
return result;
110+
}
111+
}
112+
};
113+
114+
})(require('../tree'));

lib/less/tree/ruleset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ tree.Ruleset.prototype = {
130130
for (var i = 0; i < this.rules.length; i++) {
131131
rule = this.rules[i];
132132

133-
if (rule.rules || (rule instanceof tree.Directive)) {
133+
if (rule.rules || (rule instanceof tree.Directive) || (rule instanceof tree.Media)) {
134134
rulesets.push(rule.toCSS(paths, env));
135135
} else if (rule instanceof tree.Comment) {
136136
if (!rule.silent) {

test/css/media.css

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,52 @@
2525
max-width: 480px;
2626
}
2727
}
28+
@media print {
29+
body {
30+
padding: 20px;
31+
}
32+
body header {
33+
background-color: red;
34+
}
35+
}
36+
@media print and (orientation: landscape) {
37+
body {
38+
margin-left: 20px;
39+
}
40+
}
41+
@media a, b and c {
42+
body {
43+
width: 95%;
44+
}
45+
}
46+
@media a and x, b and c and x, a and y, b and c and y {
47+
body {
48+
width: 100%;
49+
}
50+
}
51+
.a {
52+
background: black;
53+
}
54+
@media handheld {
55+
.a {
56+
background: white;
57+
}
58+
}
59+
@media handheld and (max-width: 100px) {
60+
.a {
61+
background: red;
62+
}
63+
}
64+
.b {
65+
background: black;
66+
}
67+
@media handheld {
68+
.b {
69+
background: white;
70+
}
71+
}
72+
@media handheld and (max-width: 200px) {
73+
.b {
74+
background: red;
75+
}
76+
}

test/less/media.less

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
@media print {
77
.class {
8-
color: blue;
8+
color: blue;
99
.sub {
1010
width: @var;
1111
}
@@ -29,3 +29,47 @@
2929
max-width: 480px;
3030
}
3131
}
32+
33+
body {
34+
@media print {
35+
padding: 20px;
36+
37+
header {
38+
background-color: red;
39+
}
40+
41+
@media (orientation:landscape) {
42+
margin-left: 20px;
43+
}
44+
}
45+
}
46+
47+
body {
48+
@media a, b and c {
49+
width: 95%;
50+
51+
@media x, y {
52+
width: 100%;
53+
}
54+
}
55+
}
56+
57+
.mediaMixin(@fallback: 200px) {
58+
background: black;
59+
60+
@media handheld {
61+
background: white;
62+
63+
@media (max-width: @fallback) {
64+
background: red;
65+
}
66+
}
67+
}
68+
69+
.a {
70+
.mediaMixin(100px);
71+
}
72+
73+
.b {
74+
.mediaMixin();
75+
}

0 commit comments

Comments
 (0)