Skip to content

Commit

Permalink
[TASK] Remove HtmlParser->caseShift()
Browse files Browse the repository at this point in the history
The method `HtmlParser->caseShift()` is used to normalize the casing of
values that are about to get compared, depending on whether a
case-sensitive comparison is wanted at all which isn't, by default [1].

Also, the method had a runtime cache that doesn't have a great benefit.
Therefore, the method is removed and normalization is done at the former
method calls.

[1] https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Functions/HtmlparserTags.html#fixattrib-attribute-casesensitivecomp

Resolves: #103565
Releases: main
Change-Id: I260daeaa677418e5f7a619176e1fae17e5f68bb2
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83704
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
andreaskienast authored and lolli42 committed Apr 8, 2024
1 parent 7e83da8 commit 48f5960
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions typo3/sysext/core/Classes/Html/HtmlParser.php
Expand Up @@ -24,8 +24,6 @@
*/
class HtmlParser
{
protected array $caseShift_cache = [];

// Void elements that do not have closing tags, as defined by HTML5, except link element
public const VOID_ELEMENTS = 'area|base|br|col|command|embed|hr|img|input|keygen|meta|param|source|track|wbr';

Expand Down Expand Up @@ -539,7 +537,14 @@ public function HTMLcleaner($content, $tags = [], $keepAll = 0, $hSC = 0, $addCo
$tagAttrib[0][$attr] = $params['list'][0];
}
} else {
if (!in_array($this->caseShift($tagAttrib[0][$attr] ?? '', $params['casesensitiveComp'] ?? false), (array)$this->caseShift($params['list'], $params['casesensitiveComp'] ?? false, $tagName))) {
$normalizedSearchWord = $tagAttrib[0][$attr] ?? '';
$normalizedSearchList = $params['list'];
if (!($params['casesensitiveComp'] ?? false)) {
// Case-sensitive comparison is not wanted, normalize all values
$normalizedSearchWord = strtoupper($tagAttrib[0][$attr] ?? '');
array_walk($normalizedSearchList, strtoupper(...));
}
if (!in_array($normalizedSearchWord, $normalizedSearchList, true)) {
$tagAttrib[0][$attr] = $params['list'][0];
}
}
Expand All @@ -550,11 +555,18 @@ public function HTMLcleaner($content, $tags = [], $keepAll = 0, $hSC = 0, $addCo
) {
unset($tagAttrib[0][$attr]);
}
if (
(string)($params['removeIfEquals'] ?? '') !== ''
&& $this->caseShift($tagAttrib[0][$attr], (bool)($params['casesensitiveComp'] ?? false)) === $this->caseShift($params['removeIfEquals'], (bool)($params['casesensitiveComp'] ?? false))
) {
unset($tagAttrib[0][$attr]);
if ((string)($params['removeIfEquals'] ?? '') !== '') {
$normalizedAttribute = $tagAttrib[0][$attr];
$normalizedRemoveIfEquals = $params['removeIfEquals'];
if (!($params['casesensitiveComp'] ?? false)) {
// Case-sensitive comparison is not wanted, normalize all values
$normalizedAttribute = strtoupper($tagAttrib[0][$attr]);
$normalizedRemoveIfEquals = strtoupper($params['removeIfEquals']);
}

if ($normalizedAttribute === $normalizedRemoveIfEquals) {
unset($tagAttrib[0][$attr]);
}
}
if ($params['prefixRelPathWith'] ?? false) {
$urlParts = parse_url($tagAttrib[0][$attr]);
Expand Down Expand Up @@ -816,36 +828,6 @@ public function prefixRelPath($prefix, $srcVal, $suffix = '')
return $srcVal;
}

/**
* Internal function for case shifting of a string or whole array
*
* @param mixed $str Input string/array
* @param bool $caseSensitiveComparison If this value is FALSE, the string is returned in uppercase
* @param string $cacheKey Key string used for internal caching of the results. Could be an MD5 hash of the serialized version of the input $str if that is an array.
* @return array|string Output string, processed
* @internal
*/
public function caseShift($str, $caseSensitiveComparison, $cacheKey = '')
{
if ($caseSensitiveComparison) {
return $str;
}
if (is_array($str)) {
// Fetch from runlevel cache
if ($cacheKey && isset($this->caseShift_cache[$cacheKey])) {
$str = $this->caseShift_cache[$cacheKey];
} else {
array_walk($str, strtoupper(...));
if ($cacheKey) {
$this->caseShift_cache[$cacheKey] = $str;
}
}
} else {
$str = strtoupper($str);
}
return $str;
}

/**
* Compiling an array with tag attributes into a string
*
Expand Down

0 comments on commit 48f5960

Please sign in to comment.