Skip to content

Commit d729827

Browse files
committed
allow expressions in @media queries
1 parent 15bcb87 commit d729827

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lessc.inc.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,26 @@ function import(&$url, &$media) {
572572
}
573573

574574
// a list of media types, very lenient
575-
function mediaTypes(&$types) {
575+
function mediaTypes(&$parts) {
576+
$parts = array();
577+
while ($this->to("(", $chunk, false, "[^{]")) {
578+
$parts[] = array('raw', $chunk."(");
579+
$s = $this->seek();
580+
if ($this->keyword($name) && $this->assign() &&
581+
$this->propertyValue($value))
582+
{
583+
$parts[] = array('assign', $name, $value);
584+
} else {
585+
$this->seek($s);
586+
}
587+
}
588+
576589
if ($this->to('{', $rest, true, true)) {
577-
$types = trim($rest);
590+
$parts[] = array('raw', $rest);
578591
return true;
579592
}
580593

594+
$parts = null;
581595
return false;
582596
}
583597

@@ -1018,7 +1032,7 @@ function compileBlock($block, $parent_tags = null) {
10181032
if ($special_block) {
10191033
$this->indentLevel--;
10201034
if (isset($block->media)) {
1021-
echo "@media ".$block->media;
1035+
echo $this->compileMedia($block);
10221036
} elseif (isset($block->keyframes)) {
10231037
echo $block->tags[0]." ".
10241038
$this->compileValue($this->reduce($block->keyframes));
@@ -1374,6 +1388,21 @@ function compileValue($value) {
13741388
}
13751389
}
13761390

1391+
function compileMedia($block) {
1392+
$mediaParts = array();
1393+
foreach ($block->media as $part) {
1394+
if ($part[0] == "raw") {
1395+
$mediaParts[] = $part[1];
1396+
} elseif ($part[0] == "assign") {
1397+
list(, $propName, $propVal) = $part;
1398+
$mediaParts[] = "$propName: ".
1399+
$this->compileValue($this->reduce($propVal));
1400+
}
1401+
}
1402+
1403+
return "@media ".trim(implode($mediaParts));
1404+
}
1405+
13771406
function lib_isnumber($value) {
13781407
return $this->toBool(is_numeric($value[1]));
13791408
}
@@ -2090,8 +2119,13 @@ function preg_quote($what) {
20902119

20912120
// advance counter to next occurrence of $what
20922121
// $until - don't include $what in advance
2122+
// $allowNewline, if string, will be used as valid char set
20932123
function to($what, &$out, $until = false, $allowNewline = false) {
2094-
$validChars = $allowNewline ? "." : "[^\n]";
2124+
if (is_string($allowNewline)) {
2125+
$validChars = $allowNewline;
2126+
} else {
2127+
$validChars = $allowNewline ? "." : "[^\n]";
2128+
}
20952129
if (!$this->match('('.$validChars.'*?)'.$this->preg_quote($what), $m, !$until)) return false;
20962130
if ($until) $this->count -= strlen($what); // give back $what
20972131
$out = $m[1];
@@ -2111,7 +2145,7 @@ function match($regex, &$out, $eatWhitespace = true) {
21112145
// match something without consuming it
21122146
function peek($regex, &$out = null) {
21132147
$r = '/'.$regex.'/Ais';
2114-
$result = preg_match($r, $this->buffer, $out, null, $this->count);
2148+
$result = preg_match($r, $this->buffer, $out, null, $this->count);
21152149

21162150
return $result;
21172151
}

tests/inputs/media.less

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@
2525
body { color: blue }
2626
}
2727

28+
29+
@media screen and (min-height: 100px + 10px) {
30+
body { color: red; }
31+
}
32+
33+
@cool: 100px;
34+
35+
@media screen and (height: @cool) and (width: @cool + 10), (size: @cool + 20) {
36+
body { color: red; }
37+
}
38+

tests/outputs/media.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919
@media screen and (min-width: 102.5em) and (max-width: 117.9375em),
2020
screen and (min-width: 150em) {
2121
body { color:blue; }
22+
}
23+
@media screen and (min-height: 110px) {
24+
body { color:red; }
25+
}
26+
@media screen and (height: 100px) and (width: 110px), (size: 120px) {
27+
body { color:red; }
2228
}

0 commit comments

Comments
 (0)