Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #61

Merged
merged 4 commits into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

before_script:
- composer install --dev --prefer-source

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm

script:
- phpunit --coverage-text
19 changes: 13 additions & 6 deletions CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ private function calculateCSSSpecifity($selector)
// loop chunks
foreach ($chunks as $chunk) {
// an ID is important, so give it a high specifity
if(strstr($chunk, '#') !== false) $specifity += 100;

if(strstr($chunk, '#') !== false) {
$specifity += 100;
// classes are more important than a tag, but less important then an ID
elseif(strstr($chunk, '.')) $specifity += 10;

} elseif(strstr($chunk, '.')) {
$specifity += 10;
// anything else isn't that important
else $specifity += 1;
} else {
$specifity += 1;
}
}

// return
Expand Down Expand Up @@ -501,6 +503,10 @@ private function processCSS()

// sort based on specifity
if (!empty($this->cssRules)) {
// compatibility for HHVM - https://github.com/facebook/hhvm/issues/1781
foreach ($this->cssRules as $index => $rule) {
$this->cssRules[$index]['originalIndex'] = $index;
}
usort($this->cssRules, array(__CLASS__, 'sortOnSpecifity'));
}
}
Expand Down Expand Up @@ -647,7 +653,8 @@ private function stripOriginalStyleTags($html)
private static function sortOnSpecifity($e1, $e2)
{
if ($e1['specifity'] == $e2['specifity']) {
return 0;
// compatibility for HHVM - https://github.com/facebook/hhvm/issues/1781
return ($e1['originalIndex'] > $e2['originalIndex']) ? -1 : 1;
}
return ($e1['specifity'] < $e2['specifity']) ? -1 : 1;
}
Expand Down
30 changes: 30 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
cacheTokens="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="false">

<testsuites>
<testsuite name="CssToInlineStyles Test">
<file>./tests/CssToInlineStyles.Test.php</file>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory>./vendor</directory>
<directory>./doc</directory>
<directory>./report</directory>
<directory>./tests</directory>
</blacklist>
</filter>
</phpunit>
216 changes: 216 additions & 0 deletions tests/CssToInlineStyles.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php

namespace TijsVerkoyen\CssToInlineStyles\tests;

use \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

class CssToInlineStyles_Test extends \PHPUnit_Framework_TestCase
{
protected $cssToInlineStyles;

public function setUp()
{
$this->cssToInlineStyles = new CssToInlineStyles();
}

public function teardown()
{
$this->cssToInlineStyles = null;
}

public function testSimpleElementSelector()
{
$html = '<div></div>';
$css = 'div { display: none; }';
$expected = '<div style="display: none;"></div>';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testSimpleCssSelector()
{
$html = '<a class="test-class">nodeContent</a>';
$css = '.test-class { background-color: #aaa; text-decoration: none; }';
$expected = '<a class="test-class" style="background-color: #aaa; text-decoration: none;">nodeContent</a>';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testSimpleIdSelector()
{
$html = '<img id="IMG1">';
$css = '#IMG1 { border: 1px solid red; }';
$expected = '<img id="IMG1" style="border: 1px solid red;">';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testInlineStylesBlock()
{
$html = <<<EOF
<style type="text/css">
a {
padding: 10px;
margin: 0;
}
</style>
<a></a>
EOF;
$expected = '<a style="margin: 0; padding: 10px;"></a>';
$this->cssToInlineStyles->setUseInlineStylesBlock();
$this->cssToInlineStyles->setHTML($html);
$actual = $this->findAndSaveNode($this->cssToInlineStyles->convert(), '//a');
$this->assertEquals($expected, $actual);
}

public function testStripOriginalStyleTags()
{
$html = <<<EOF
<style type="text/css">
a {
padding: 10px;
margin: 0;
}
</style>
<a></a>
EOF;
$expected = '<a></a>';
$this->cssToInlineStyles->setStripOriginalStyleTags();
$this->cssToInlineStyles->setHTML($html);
$actual = $this->findAndSaveNode($this->cssToInlineStyles->convert(), '//a');
$this->assertEquals($expected, $actual);

$this->assertNull($this->findAndSaveNode($actual, '//style'));
}

public function testSpecificity()
{
$html = '<a class="one" id="ONE"><img class="two" id="TWO"></a>';
$css = <<<EOF
a {
border: 1px solid red;
padding: 10px;
margin: 20px;
}
.one {
padding: 15px;
}
#ONE {
margin: 10px;
}
img {
border: 2px solid green;
}
a img {
border: none;
}
EOF;
$expected = '<a class="one" id="ONE" style="border: 1px solid red; margin: 10px; padding: 15px;"><img class="two" id="TWO" style="border: none;"></a>';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testMergeOriginalStyles()
{
$html = '<p style="padding: 20px; margin-top: 10px;">text</p>';
$css = <<<EOF
p {
margin-top: 20px;
text-indent: 1em;
}
EOF;
$expected = '<p style="margin-top: 10px; text-indent: 1em; padding: 20px;">text</p>';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testXHTMLOutput()
{
$html = '<a><img></a>';
$css = 'a { display: block; }';

$this->cssToInlineStyles->setHTML($html);
$this->cssToInlineStyles->setCSS($css);
$actual = $this->cssToInlineStyles->convert(true);

$this->assertContains('<img></img>', $actual);
}

public function testCleanup()
{
$html = '<div id="id" class="className"></div>';
$css = ' #id { display: inline; } .className { margin-right: 10px; }';
$expected = '<div style="margin-right: 10px; display: inline;"></div>';
$this->cssToInlineStyles->setCleanup();
$this->runHTMLToCSS($html, $css, $expected);
}

public function testEqualSpecificity()
{
$html = '<img class="one">';
$css = ' .one { display: inline; } a > strong {} a {} a {} a {} a {} a {} a {}a {} img { display: block; }';
$expected = '<img class="one" style="display: inline;">';
$this->runHTMLToCSS($html, $css, $expected);
}

public function testInvalidSelector()
{
$html = "<p></p>";
$css = ' p&@*$%& { display: inline; }';
$expected = $html;
$this->runHTMLToCSS($html, $css, $expected);
}

public function testEncoding()
{
$html = "<p>" . html_entity_decode('&rsquo;', 0, 'UTF-8') . "</p>";
$css = '';
$expected = '<p>' . chr(0xc3) . chr(0xa2) . chr(0xc2) . chr(0x80) . chr(0xc2) . chr(0x99) . '</p>';

$this->cssToInlineStyles->setEncoding('ISO-8859-1');
$this->runHTMLToCSS($html, $css, $expected);
}

private function runHTMLToCSS($html, $css, $expected, $asXHTML = false)
{
$cssToInlineStyles = $this->cssToInlineStyles;
$cssToInlineStyles->setHTML($html);
$cssToInlineStyles->setCSS($css);
$output = $cssToInlineStyles->convert($asXHTML);
$actual = $this->stripBody($output, $asXHTML);
$this->assertEquals($expected, $actual);
}

private function stripBody($html, $asXHTML = false)
{
$dom = new \DOMDocument();
/*if ($asXHTML) {
$dom->loadXML($html);
} else {*/
$dom->loadHTML($html);
/*}*/
$xpath = new \DOMXPath($dom);
$nodelist = $xpath->query('//body/*');
$result = '';
for ($i = 0; $i < $nodelist->length; $i++) {
$node = $nodelist->item($i);
if ($asXHTML) {
$result .= $dom->saveXML($node);
} else {
$result .= $dom->saveHTML($node);
}
}

return $result;
}

private function findAndSaveNode($html, $query)
{
$dom = new \DOMDocument();
$dom->loadHTML($html);
$xpath = new \DOMXPath($dom);
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
$node = $nodelist->item(0);

return $dom->saveHTML($node);
} else {
return null;
}
}
}