Skip to content

Commit e060b02

Browse files
committed
Optimised how the urlskip setting is handled in tag::minify().
Updated `tag` class to use `str_starts_with()`. Updated `tag::hasAttribute()` to check the values case-insensitively.
1 parent b8975ce commit e060b02

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

phpunit.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache"
3+
displayDetailsOnTestsThatTriggerDeprecations="true"
4+
displayDetailsOnTestsThatTriggerErrors="true"
5+
displayDetailsOnTestsThatTriggerNotices="true"
6+
displayDetailsOnTestsThatTriggerWarnings="true">
37
<coverage>
48
<report>
59
<clover outputFile="./coverage/result.xml"/>

src/tokens/tag.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public function parse(tokenise $tokens) : void {
136136
case 'attributevalue':
137137
if ($attr) {
138138
$value = \trim($token['value'], "= \t\r\n");
139-
if (($pos = \strpos($value, '"')) === 0 || \strpos($value, "'") === 0) {
140-
$value = \trim($value, $pos === 0 ? '"' : "'");
139+
if (($double = \str_starts_with($value, '"')) === true || \str_starts_with($value, "'")) {
140+
$value = \trim($value, $double ? '"' : "'");
141141
}
142142
$attributes[$attr] = \html_entity_decode($value, ENT_QUOTES | ENT_HTML5);
143143
$attr = null;
@@ -416,6 +416,7 @@ public function minify(array $minify) : void {
416416
// minify attributes
417417
$tag = $this->tagName;
418418
$attributes = $this->attributes;
419+
$skipurl = isset($attr['urlskip'][$tag]) && !$this->hasAttribute($attributes, $attr['urlskip'][$tag]);
419420
$host = null;
420421
foreach ($attributes AS $key => $value) {
421422

@@ -427,7 +428,7 @@ public function minify(array $minify) : void {
427428
}
428429

429430
// minify url attributes when not in list or match attribute
430-
if ($minify['urls'] && $attributes[$key] && \in_array($key, $attr['urls'], true) && (!\in_array($tag, \array_keys($attr['urlskip']), true) || $this->hasAttribute($attributes, $attr['urlskip'][$tag]))) {
431+
if ($minify['urls'] && $value && \in_array($key, $attr['urls'], true) && !$skipurl) {
431432

432433
// make folder variables
433434
if ($folder === null && isset($_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'])) {
@@ -440,7 +441,7 @@ public function minify(array $minify) : void {
440441
}
441442

442443
// strip scheme from absolute URL's if the same as current scheme
443-
if ($minify['urls']['scheme'] && \mb_strpos($attributes[$key], $scheme) === 0) {
444+
if ($minify['urls']['scheme'] && \str_starts_with($attributes[$key], $scheme)) {
444445
$attributes[$key] = \mb_substr($attributes[$key], \mb_strlen($scheme)-2);
445446
}
446447

@@ -469,7 +470,7 @@ public function minify(array $minify) : void {
469470
if ($minify['urls']['relative'] && $folder) {
470471

471472
// minify
472-
if (\mb_strpos($attributes[$key], $folder) === 0 && ($folder !== '/' || \mb_strpos($attributes[$key], '//') !== 0)) {
473+
if (\str_starts_with($attributes[$key], $folder) && ($folder !== '/' || !\str_starts_with($attributes[$key], '//'))) {
473474
if ($attributes[$key] === $folder && $attributes[$key] !== $_SERVER['REQUEST_URI']) {
474475
$attributes[$key] = './';
475476
} else {
@@ -479,7 +480,7 @@ public function minify(array $minify) : void {
479480
}
480481

481482
// use parent folders if it is shorter
482-
if ($minify['urls']['parent'] && $dirs && \mb_strpos($attributes[$key], '/') === 0 && !\str_contains($attributes[$key], '//')) {
483+
if ($minify['urls']['parent'] && $dirs && \str_starts_with($attributes[$key], '/') && !\str_contains($attributes[$key], '//')) {
483484
$isDir = \mb_strrpos($attributes[$key], '/') === \mb_strlen($attributes[$key])-1;
484485
$compare = \explode('/', \trim($isDir ? $attributes[$key] : \dirname($attributes[$key]), '/'));
485486
$update = false;
@@ -617,7 +618,8 @@ public function minify(array $minify) : void {
617618

618619
protected function hasAttribute(array $attr, array $items) {
619620
foreach ($items AS $key => $item) {
620-
if (!isset($attr[$key]) || !\in_array($attr[$key], $item, true)) {
621+
$lower = \mb_strtolower($key);
622+
if (!isset($attr[$lower]) || !\in_array($attr[$lower], $item, true)) {
621623
return false;
622624
}
623625
}
@@ -755,7 +757,7 @@ public function find(array $selector, bool $searchChildren = true) : array {
755757
// match subcode
756758
case '|=':
757759
if ($item['sensitive']) {
758-
if ($current !== $item['value'] && \mb_strpos($current, $item['value'].'-') !== 0) {
760+
if ($current !== $item['value'] && !\str_starts_with($current, $item['value'].'-')) {
759761
$match = false;
760762
break;
761763
}

0 commit comments

Comments
 (0)