Skip to content

Commit

Permalink
Merge branch 'MrAdamCook-ifset'
Browse files Browse the repository at this point in the history
  • Loading branch information
peteboere committed Jun 9, 2015
2 parents 1198de4 + b1dfa10 commit 9561fd5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/core/variables.md
Expand Up @@ -42,6 +42,7 @@ Sections of CSS can be included and excluded on the basis of variable existence

```crush
@set foo #f00;
@set bar true;
@ifset foo {
p {
Expand All @@ -54,5 +55,8 @@ p {
@ifset not foo {
line-height: 1.5;
}
@ifset bar(true) {
margin-bottom: 5px;
}
}
```
```
17 changes: 15 additions & 2 deletions lib/CssCrush/Process.php
Expand Up @@ -457,7 +457,7 @@ protected function resolveSettings()

protected function resolveIfDefines()
{
$ifdefinePatt = Regex::make('~@if(?:set|define) \s+ (?<negate>not \s+)? (?<name>{{ ident }}) \s* \{~ixS');
$ifdefinePatt = Regex::make('~@if(?:set|define) \s+ (?<negate>not \s+)? (?<name>{{ ident }}) \s* {{ parens }}? \s* \{~ixS');

$matches = $this->string->matchAll($ifdefinePatt);

Expand All @@ -472,7 +472,20 @@ protected function resolveIfDefines()
$negate = $match['negate'][1] != -1;
$nameDefined = isset($this->vars[$match['name'][0]]);

if (! $negate && $nameDefined || $negate && ! $nameDefined) {
$valueDefined = isset($match['parens_content'][0]);
$valueMatch = false;
if ($nameDefined && $valueDefined) {
$testValue = Util::rawValue(trim($match['parens_content'][0]));
$varValue = Util::rawValue($this->vars[$match['name'][0]]);
$valueMatch = $varValue == $testValue;
}

if (
( $valueDefined && !$negate && $valueMatch )
|| ( $valueDefined && $negate && !$valueMatch )
|| ( !$valueDefined && !$negate && $nameDefined )
|| ( !$valueDefined && $negate && !$nameDefined )
) {
$curlyMatch->unWrap();
}
else {
Expand Down
1 change: 1 addition & 0 deletions lib/CssCrush/Regex.php
Expand Up @@ -85,6 +85,7 @@ public static function init()
$patt->ruleDirective = '~^(?:(@include)|(@extends?)|(@name))[\s]+~iS';
$patt->argListSplit = '~\s*[,\s]\s*~S';
$patt->cruftyHex = Regex::make('~\#({{hex}})\1({{hex}})\2({{hex}})\3~S');
$patt->token = Regex::make('~^ \? (?<type>[a-zA-Z]) {{token_id}} \? $~xS');
}

public static function make($pattern)
Expand Down
7 changes: 6 additions & 1 deletion lib/CssCrush/Tokens.php
Expand Up @@ -146,11 +146,16 @@ public static function pad($label, $replaced_text)

public static function is($label, $of_type)
{
if (preg_match(Regex::make('~^ \? (?<type>[a-zA-Z]) {{token_id}} \? $~xS'), $label, $m)) {
if (preg_match(Regex::$patt->token, $label, $m)) {

return $of_type ? ($of_type === $m['type']) : true;
}

return false;
}

public static function test($value)
{
return preg_match(Regex::$patt->token, $value, $m) ? $m['type'] : false;
}
}
17 changes: 17 additions & 0 deletions lib/CssCrush/Util.php
Expand Up @@ -228,6 +228,23 @@ public static function readConfigFile($path)
return Options::filter(get_defined_vars());
}

/*
* Get raw value (useful if testing values that may or may not be a token).
*/
public static function rawValue($value)
{
if ($tokenType = Tokens::test($value)) {
if ($tokenType == 'u') {
$value = Crush::$process->tokens->get($value)->value;
}
elseif ($tokenType == 's') {
$value = Crush::$process->tokens->get($value);
}
}

return $value;
}

/*
* Encode integer to Base64 VLQ.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/CssCrush/TokensTest.php
Expand Up @@ -111,4 +111,11 @@ public function testIs()
{
$this->assertTrue(Tokens::is($this->tokens->createLabel('s'), 's'));
}

public function testTest()
{
$this->assertFalse(Tokens::test('foobar'));
$this->assertEquals(Tokens::test($this->tokens->createLabel('u')), 'u');
$this->assertEquals(Tokens::test($this->tokens->createLabel('s')), 's');
}
}
26 changes: 26 additions & 0 deletions tests/unit/CssCrush/UtilTest.php
Expand Up @@ -3,9 +3,17 @@
namespace CssCrush\UnitTest;

use CssCrush\Util;
use CssCrush\Tokens;
use CssCrush\Url;

class UtilTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->process = bootstrap_process(array('minify' => false));
$this->tokens = $this->process->tokens;
}

public function testNormalizePath()
{
$this->assertEquals('/Some/crazy/Path', Util::normalizePath('C:\\Some\crazy/Path\\', true));
Expand Down Expand Up @@ -90,6 +98,24 @@ public function testFilePutContents()
$this->assertTrue(Util::filePutContents($test_file, 'Hello Mum'));
}

public function testRawValue()
{
$url1 = $this->tokens->add(new Url('foo.jpg'));
$url2 = $this->tokens->add(new Url('foo.jpg'));
$this->assertNotEquals($url1, $url2);
$this->assertEquals(Util::rawValue($url1), Util::rawValue($url2));
$this->assertEquals(Util::rawValue($url1), 'foo.jpg');

$string1 = $this->tokens->add('"bar"', 's');
$string2 = $this->tokens->add('"bar"', 's');
$this->assertNotEquals($string1, $string2);
$this->assertEquals(Util::rawValue($string1), Util::rawValue($string2));
$this->assertEquals(Util::rawValue($string1), '"bar"');

$this->assertEquals(Util::rawValue('foobar'), 'foobar');
$this->assertNotEquals(Util::rawValue('foobar'), 'notFoobar');
}

public function testReadConfigFile()
{
$contents = <<<'NOW_DOC'
Expand Down

0 comments on commit 9561fd5

Please sign in to comment.