Skip to content

Commit

Permalink
Optimised how the urlskip setting is handled in tag::minify().
Browse files Browse the repository at this point in the history
Updated `tag` class to use `str_starts_with()`.
Updated `tag::hasAttribute()` to check the values case-insensitively.
  • Loading branch information
hexydec committed Apr 16, 2024
1 parent b8975ce commit e060b02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0"?>
<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">
<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"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true">
<coverage>
<report>
<clover outputFile="./coverage/result.xml"/>
Expand Down
18 changes: 10 additions & 8 deletions src/tokens/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function parse(tokenise $tokens) : void {
case 'attributevalue':
if ($attr) {
$value = \trim($token['value'], "= \t\r\n");
if (($pos = \strpos($value, '"')) === 0 || \strpos($value, "'") === 0) {
$value = \trim($value, $pos === 0 ? '"' : "'");
if (($double = \str_starts_with($value, '"')) === true || \str_starts_with($value, "'")) {
$value = \trim($value, $double ? '"' : "'");
}
$attributes[$attr] = \html_entity_decode($value, ENT_QUOTES | ENT_HTML5);
$attr = null;
Expand Down Expand Up @@ -416,6 +416,7 @@ public function minify(array $minify) : void {
// minify attributes
$tag = $this->tagName;
$attributes = $this->attributes;
$skipurl = isset($attr['urlskip'][$tag]) && !$this->hasAttribute($attributes, $attr['urlskip'][$tag]);
$host = null;
foreach ($attributes AS $key => $value) {

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

// minify url attributes when not in list or match attribute
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]))) {
if ($minify['urls'] && $value && \in_array($key, $attr['urls'], true) && !$skipurl) {

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

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

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

// minify
if (\mb_strpos($attributes[$key], $folder) === 0 && ($folder !== '/' || \mb_strpos($attributes[$key], '//') !== 0)) {
if (\str_starts_with($attributes[$key], $folder) && ($folder !== '/' || !\str_starts_with($attributes[$key], '//'))) {
if ($attributes[$key] === $folder && $attributes[$key] !== $_SERVER['REQUEST_URI']) {
$attributes[$key] = './';
} else {
Expand All @@ -479,7 +480,7 @@ public function minify(array $minify) : void {
}

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

protected function hasAttribute(array $attr, array $items) {
foreach ($items AS $key => $item) {
if (!isset($attr[$key]) || !\in_array($attr[$key], $item, true)) {
$lower = \mb_strtolower($key);
if (!isset($attr[$lower]) || !\in_array($attr[$lower], $item, true)) {
return false;
}
}
Expand Down Expand Up @@ -755,7 +757,7 @@ public function find(array $selector, bool $searchChildren = true) : array {
// match subcode
case '|=':
if ($item['sensitive']) {
if ($current !== $item['value'] && \mb_strpos($current, $item['value'].'-') !== 0) {
if ($current !== $item['value'] && !\str_starts_with($current, $item['value'].'-')) {
$match = false;
break;
}
Expand Down

0 comments on commit e060b02

Please sign in to comment.