Skip to content

Commit b958604

Browse files
committed
Fixed issues handling empty attributes in tag::minify(), where the code expected the attribute value to be a string, but if only the attribute name appears, its value would be null.
Improved login in `tag::minify()`. Updated dependencies.
1 parent 5c016a1 commit b958604

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": ">=7.4",
1818
"hexydec/tokenise": "1.0.1",
1919
"hexydec/cssdoc": "1.0.4",
20-
"hexydec/jslite": "0.6.2"
20+
"hexydec/jslite": "0.6.3"
2121
},
2222
"autoload": {
2323
"classmap": ["src/"]

composer.lock

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tokens/tag.php

+15-16
Original file line numberDiff line numberDiff line change
@@ -506,44 +506,43 @@ public function minify(array $minify) : void {
506506
// minify attributes
507507
if ($minify['attributes']) {
508508

509+
// cache attributes
510+
$min = $minify['attributes'];
511+
509512
// trim attribute
510-
if ($minify['attributes']['trim'] && $attributes[$key]) {
513+
if ($min['trim'] && $attributes[$key]) {
511514
$attributes[$key] = \trim($attributes[$key], " \r\n\t");
512515
}
513516

514517
// boolean attributes
515-
if ($minify['attributes']['boolean'] && \in_array($key, $attr['boolean'], true)) {
518+
if ($min['boolean'] && \in_array($key, $attr['boolean'], true)) {
516519
$attributes[$key] = null;
517520

521+
// remove empty attributes
522+
} elseif (\in_array($attributes[$key], ['', null], true) && $min['empty'] && \in_array($key, $attr['empty'], true)) {
523+
unset($attributes[$key]);
524+
518525
// minify style tag
519-
} elseif ($key === 'style' && $minify['attributes']['style'] && !empty($attributes[$key])) {
526+
} elseif ($key === 'style' && $min['style'] && $attributes[$key]) {
520527
$attributes[$key] = \trim(\str_replace(
521-
[' ', ' : ', ': ', ' :', ' ; ', ' ;', '; '],
522-
[' ', ':', ':', ':', ';', ';', ';'],
528+
["\t", "\r", "\n", ' ', ' : ', ': ', ' :', ' ; ', ' ;', '; '],
529+
[' ', '', ' ', ' ', ':', ':', ':', ';', ';', ';'],
523530
$attributes[$key]
524531
), '; ');
525532

526533
// trim classes
527-
} elseif ($key === 'class' && $minify['attributes']['class'] && \mb_strpos($attributes[$key], ' ') !== false) {
534+
} elseif ($key === 'class' && $min['class'] && \mb_strpos($attributes[$key] ?? '', ' ') !== false) {
528535
$attributes[$key] = \trim(\preg_replace('/\s+/', ' ', $attributes[$key]));
529536

530537
// minify option tag, always capture the tag to prevent it being removed as a default
531538
} elseif ($key === 'value' && $tag === 'option') {
532-
if ($minify['attributes']['option'] && isset($this->children[0]) && $this->children[0]->text() === $attributes[$key]) {
539+
if ($min['option'] && isset($this->children[0]) && $this->children[0]->text() === $attributes[$key]) {
533540
unset($attributes[$key]);
534541
}
535-
continue;
536542

537543
// remove tag specific default attribute
538-
} elseif ($minify['attributes']['default'] && isset($attr['default'][$tag][$key]) && ($attr['default'][$tag][$key] === true || $attr['default'][$tag][$key] === $attributes[$key])) {
539-
unset($attributes[$key]);
540-
continue;
541-
}
542-
543-
// remove other attributes
544-
if ($attributes[$key] === '' && $minify['attributes']['empty'] && \in_array($key, $attr['empty'], true)) {
544+
} elseif ($min['default'] && isset($attr['default'][$tag][$key]) && ($attr['default'][$tag][$key] === true || $attr['default'][$tag][$key] === $attributes[$key])) {
545545
unset($attributes[$key]);
546-
continue;
547546
}
548547
}
549548
}

tests/templates/attributes.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script type="text/javascript" language="VBScript"></script>
33
<form method="get" id="form"></form>
44
<div id="" class=" " style="" title=""></div>
5-
<select class="select">
5+
<select class="select" style>
66
<option value="test">Tester</option>
77
<option value="tester">tester</option>
88
</select>

0 commit comments

Comments
 (0)