Skip to content

Commit 86ec2f0

Browse files
committed
Updated tests to handle <![CDATA[ tags wrapping CSS.
Updated dependencies.
1 parent 62d1965 commit 86ec2f0

File tree

3 files changed

+107
-96
lines changed

3 files changed

+107
-96
lines changed

composer.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tokens/custom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function minify(array $minify) : void {
6666
if (empty($config['cache'])) {
6767
$file = null;
6868
} else {
69-
$file = sprintf($config['cache'], md5($content));
69+
$file = \sprintf($config['cache'], \md5($content));
7070
if (\file_exists($file) && ($output = \file_get_contents($file)) !== false) {
7171
$this->content = $output;
7272
return;

tests/minifyHtmldocTest.php

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -138,51 +138,51 @@ public function testCanMinifyUrls() {
138138
'<a href="http://tester.com/test">Different Host</a>' => '<a href="//tester.com/test">Different Host</a>',
139139
'<video src="http://test.com/assets/video.mp4" poster="http://test.com/assets/video.jpg"></video>' => '<video src="/assets/video.mp4" poster="/assets/video.jpg"></video>'
140140
),
141-
// 'https://test.com/url/' => Array(
142-
// '<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
143-
// '<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
144-
// '<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
145-
// '<a href="https://test.com/url">Own Host</a>' => '<a href="/url">Own Host</a>',
146-
// '<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="test.php">Own Host under folder</a>',
147-
// '<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="test.php">Own Host under folder no scheme</a>',
148-
// '<a href="http://test.com/test">Different scheme</a>' => '<a href="http://test.com/test">Different scheme</a>',
149-
// '<a href="http://tester.com/test">Different Host</a>' => '<a href="http://tester.com/test">Different Host</a>',
150-
// ),
151-
// 'https://test.com/url' => Array(
152-
// '<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
153-
// '<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
154-
// '<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
155-
// '<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="/url/test.php">Own Host under folder</a>',
156-
// '<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="/url/test.php">Own Host under folder no scheme</a>',
157-
// '<a href="http://test.com/test">Different scheme</a>' => '<a href="http://test.com/test">Different scheme</a>',
158-
// '<a href="http://tester.com/test">Different Host</a>' => '<a href="http://tester.com/test">Different Host</a>',
159-
// ),
160-
// 'https://test.com/url/?var=value' => Array(
161-
// '<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
162-
// '<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
163-
// '<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
164-
// '<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="test.php">Own Host under folder</a>',
165-
// '<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="test.php">Own Host under folder no scheme</a>',
166-
// '<a href="https://test.com/url">Same URL with no querystring or slash</a>' => '<a href="/url">Same URL with no querystring or slash</a>',
167-
// '<a href="https://test.com/url/">Same URL with no querystring</a>' => '<a href="./">Same URL with no querystring</a>',
168-
// ),
169-
// 'https://test.com/deep/lot/of/folders/' => Array(
170-
// '<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
171-
// '<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
172-
// '<a href="https://test.com/different/folders/">Different Folders</a>' => '<a href="/different/folders/">Different Folders</a>',
173-
// '<a href="https://test.com/deep/lot/test">Back two</a>' => '<a href="../../test">Back two</a>',
174-
// '<a href="https://test.com/deep/lot/test/">Back two keep slash</a>' => '<a href="../../test/">Back two keep slash</a>',
175-
// '<a href="https://test.com/deep/lot/test/this/and/this.php">Back two</a>' => '<a href="../../test/this/and/this.php">Back two</a>',
176-
// '<link rel="stylesheet" href="/deep/css/build/file.css?_12345">' => '<link rel="stylesheet" href="/deep/css/build/file.css?_12345">', // shorter to stay as is
177-
// ),
178-
// 'https://test.com/alotof/of/folders/' => Array(
179-
// '<link rel="stylesheet" href="/alotof/css/build/file.css?_12345">' => '<link rel="stylesheet" href="../../css/build/file.css?_12345">',
180-
// '<a href="https://nottest.com/alotof/of/test/test.php">Different Domain</a>' => '<a href="//nottest.com/alotof/of/test/test.php">Different Domain</a>',
181-
// ),
182-
// 'https://test.com/' => Array(
183-
// '<link rel="stylesheet" href="https://test.com/alotof/css/build/file.css?_12345">' => '<link rel="stylesheet" href="alotof/css/build/file.css?_12345">',
184-
// '<link itemtype="url" href="https://test.com/">' => '<link itemtype="url" href="https://test.com/">',
185-
// )
141+
'https://test.com/url/' => Array(
142+
'<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
143+
'<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
144+
'<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
145+
'<a href="https://test.com/url">Own Host</a>' => '<a href="/url">Own Host</a>',
146+
'<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="test.php">Own Host under folder</a>',
147+
'<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="test.php">Own Host under folder no scheme</a>',
148+
'<a href="http://test.com/test">Different scheme</a>' => '<a href="http://test.com/test">Different scheme</a>',
149+
'<a href="http://tester.com/test">Different Host</a>' => '<a href="http://tester.com/test">Different Host</a>',
150+
),
151+
'https://test.com/url' => Array(
152+
'<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
153+
'<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
154+
'<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
155+
'<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="/url/test.php">Own Host under folder</a>',
156+
'<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="/url/test.php">Own Host under folder no scheme</a>',
157+
'<a href="http://test.com/test">Different scheme</a>' => '<a href="http://test.com/test">Different scheme</a>',
158+
'<a href="http://tester.com/test">Different Host</a>' => '<a href="http://tester.com/test">Different Host</a>',
159+
),
160+
'https://test.com/url/?var=value' => Array(
161+
'<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
162+
'<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
163+
'<a href="https://test.com/test">Own Host</a>' => '<a href="/test">Own Host</a>',
164+
'<a href="https://test.com/url/test.php">Own Host under folder</a>' => '<a href="test.php">Own Host under folder</a>',
165+
'<a href="//test.com/url/test.php">Own Host under folder no scheme</a>' => '<a href="test.php">Own Host under folder no scheme</a>',
166+
'<a href="https://test.com/url">Same URL with no querystring or slash</a>' => '<a href="/url">Same URL with no querystring or slash</a>',
167+
'<a href="https://test.com/url/">Same URL with no querystring</a>' => '<a href="./">Same URL with no querystring</a>',
168+
),
169+
'https://test.com/deep/lot/of/folders/' => Array(
170+
'<a href="https://test.com">Root</a>' => '<a href="/">Root</a>',
171+
'<a href="https://test.com/">Root</a>' => '<a href="/">Root</a>',
172+
'<a href="https://test.com/different/folders/">Different Folders</a>' => '<a href="/different/folders/">Different Folders</a>',
173+
'<a href="https://test.com/deep/lot/test">Back two</a>' => '<a href="../../test">Back two</a>',
174+
'<a href="https://test.com/deep/lot/test/">Back two keep slash</a>' => '<a href="../../test/">Back two keep slash</a>',
175+
'<a href="https://test.com/deep/lot/test/this/and/this.php">Back two</a>' => '<a href="../../test/this/and/this.php">Back two</a>',
176+
'<link rel="stylesheet" href="/deep/css/build/file.css?_12345">' => '<link rel="stylesheet" href="/deep/css/build/file.css?_12345">', // shorter to stay as is
177+
),
178+
'https://test.com/alotof/of/folders/' => Array(
179+
'<link rel="stylesheet" href="/alotof/css/build/file.css?_12345">' => '<link rel="stylesheet" href="../../css/build/file.css?_12345">',
180+
'<a href="https://nottest.com/alotof/of/test/test.php">Different Domain</a>' => '<a href="//nottest.com/alotof/of/test/test.php">Different Domain</a>',
181+
),
182+
'https://test.com/' => Array(
183+
'<link rel="stylesheet" href="https://test.com/alotof/css/build/file.css?_12345">' => '<link rel="stylesheet" href="alotof/css/build/file.css?_12345">',
184+
'<link itemtype="url" href="https://test.com/">' => '<link itemtype="url" href="https://test.com/">',
185+
)
186186
);
187187
$doc = new htmldoc();
188188
foreach ($html AS $url => $items) {
@@ -294,47 +294,58 @@ public function testCanMinifyCssAndJs() {
294294

295295
// this basic test is to test that the HTMLdoc object can invoke the minifiers, not how good the minifiers themselves are
296296
// There are tests within the minifiers' respective projects for that
297-
$doc = new htmldoc();
298-
$input = '
299-
<style type="text/css">
297+
$tests = [
298+
'<style type="text/css">
300299
.test {
301300
display: block;
302301
font-weight: bold;
303302
}
304-
</style>
305-
<script type="text/javascript" async="async">
303+
</style>' => '<style>.test{display:block;font-weight:700}</style>',
304+
'<style type="text/css">
305+
<![CDATA[
306+
.test {
307+
display: block;
308+
font-weight: bold;
309+
}
310+
]]>
311+
</style>' => '<style><![CDATA[.test{display:block;font-weight:700}]]></style>',
312+
'<script type="text/javascript" async="async">
306313
(function () {
307314
console.log("Test");
308315
}());
309-
</script>
310-
';
311-
$output = '<style>.test{display:block;font-weight:700}</style><script async>(function(){console.log("Test")}())</script>';
312-
$doc->load($input);
313-
$this->assertEquals($input, $doc->html(), 'Can load CSS and Javascript');
316+
</script>' => '<script async>(function(){console.log("Test")}())</script>'
317+
];
318+
foreach ($tests AS $key => $item) {
319+
$doc = new htmldoc();
320+
$doc->load($key);
321+
$this->assertEquals($key, $doc->html(), 'Can load CSS and Javascript');
314322

315-
// minify the code
316-
$doc->minify();
317-
$this->assertEquals($output, $doc->html(), 'Can minify CSS and Javascript');
323+
// minify the code
324+
$doc->minify();
325+
$this->assertEquals($item, $doc->html(), 'Can minify CSS and Javascript');
326+
}
318327

319-
// minify and cache
320-
$doc = new htmldoc([
321-
'custom' => [
322-
'style' => [
323-
'cache' => __DIR__.'/cache/%s.css'
324-
],
325-
'script' => [
326-
'cache' => __DIR__.'/cache/%s.js'
328+
foreach ($tests AS $key => $item) {
329+
// minify and cache
330+
$doc = new htmldoc([
331+
'custom' => [
332+
'style' => [
333+
'cache' => __DIR__.'/cache/%s.css'
334+
],
335+
'script' => [
336+
'cache' => __DIR__.'/cache/%s.js'
337+
]
327338
]
328-
]
329-
]);
330-
$doc->load($input);
331-
$doc->minify();
332-
$this->assertEquals($output, $doc->html(), 'Can minify and cache CSS and Javascript');
339+
]);
340+
$doc->load($key);
341+
$doc->minify();
342+
$this->assertEquals($item, $doc->html(), 'Can minify and cache CSS and Javascript');
333343

334-
// do it again to pull from cache
335-
$doc->load($input);
336-
$doc->minify();
337-
$this->assertEquals($output, $doc->html(), 'Can load minified CSS and Javascript from cache');
344+
// do it again to pull from cache
345+
$doc->load($key);
346+
$doc->minify();
347+
$this->assertEquals($item, $doc->html(), 'Can load minified CSS and Javascript from cache');
348+
}
338349
}
339350

340351
public function testCanMinifyDifficultHtml() {

0 commit comments

Comments
 (0)