Skip to content

Commit

Permalink
yii\helpers\Html allows correct rendering of conditional comments c…
Browse files Browse the repository at this point in the history
…ontaining `!IE`
  • Loading branch information
klimov-paul committed May 14, 2015
1 parent f0a4dc3 commit 3a39f8a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #8322: `yii\behaviors\TimestampBehavior::touch()` now throws an exception if owner is new record (klimov-paul)
- Enh #8070: `yii\console\controllers\MessageController` now sorts created messages, even if there is no new one, while saving to PHP file (klimov-paul)
- Enh #8286: `yii\console\controllers\MessageController` improved allowing extraction of nested translator calls (klimov-paul)
- Enh #8415: `yii\helpers\Html` allows correct rendering of conditional comments containing `!IE` (salaros, klimov-paul)


2.0.4 May 10, 2015
Expand Down
18 changes: 16 additions & 2 deletions framework/helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static function cssFile($url, $options = [])
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return "<!--[if $condition]>\n" . static::tag('link', '', $options) . "\n<![endif]-->";
return self::wrapIntoCondition(static::tag('link', '', $options), $condition);
} elseif (isset($options['noscript']) && $options['noscript'] === true) {
unset($options['noscript']);
return "<noscript>" . static::tag('link', '', $options) . "</noscript>";
Expand Down Expand Up @@ -254,12 +254,26 @@ public static function jsFile($url, $options = [])
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return "<!--[if $condition]>\n" . static::tag('script', '', $options) . "\n<![endif]-->";
return self::wrapIntoCondition(static::tag('script', '', $options), $condition);
} else {
return static::tag('script', '', $options);
}
}

/**
* Wraps given content into conditional comments for IE, e.g., `lt IE 9`.
* @param string $content raw HTML content.
* @param string $condition condition string.
* @return string generated HTML.
*/
private static function wrapIntoCondition($content, $condition)
{
if (strpos($condition, '!IE') !== false) {
return "<!--[if $condition]><!-->\n" . $content . "\n<!--<![endif]-->";
}
return "<!--[if $condition]>\n" . $content . "\n<![endif]-->";
}

/**
* Generates the meta tags containing CSRF token information.
* @return string the generated meta tags
Expand Down
2 changes: 2 additions & 0 deletions tests/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ public function testCssFile()
$this->assertEquals('<link href="http://example.com" rel="stylesheet">', Html::cssFile('http://example.com'));
$this->assertEquals('<link href="/test" rel="stylesheet">', Html::cssFile(''));
$this->assertEquals("<!--[if IE 9]>\n" . '<link href="http://example.com" rel="stylesheet">' . "\n<![endif]-->", Html::cssFile('http://example.com', ['condition' => 'IE 9']));
$this->assertEquals("<!--[if (gte IE 9)|(!IE)]><!-->\n" . '<link href="http://example.com" rel="stylesheet">' . "\n<!--<![endif]-->", Html::cssFile('http://example.com', ['condition' => '(gte IE 9)|(!IE)']));
}

public function testJsFile()
{
$this->assertEquals('<script src="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script src="/test"></script>', Html::jsFile(''));
$this->assertEquals("<!--[if IE 9]>\n" . '<script src="http://example.com"></script>' . "\n<![endif]-->", Html::jsFile('http://example.com', ['condition' => 'IE 9']));
$this->assertEquals("<!--[if (gte IE 9)|(!IE)]><!-->\n" . '<script src="http://example.com"></script>' . "\n<!--<![endif]-->", Html::jsFile('http://example.com', ['condition' => '(gte IE 9)|(!IE)']));
}

public function testBeginForm()
Expand Down

0 comments on commit 3a39f8a

Please sign in to comment.