Skip to content

Commit 9760a7d

Browse files
author
Christoph Singer
committed
benchmark for another possible removeEmptyAttribute implementation
1 parent 1c7eb5e commit 9760a7d

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

Tests/benchmark.php

+53-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
});
2222

23-
runTest('Select empty attributes using complicated XPath function', function($d) {
23+
runTest('Select empty attributes using XPath string functions', function($d) {
2424
$xpath = new \DOMXPath($d);
2525
foreach (['style', 'class'] as $attr) {
2626
/** @var \DOMElement $el */
@@ -30,6 +30,23 @@
3030
}
3131
});
3232

33+
runTest('Select empty attributes using XPath | operator and PHP test for empty string', function($d) {
34+
$xpath = new \DOMXPath($d);
35+
$query = '';
36+
foreach (['style', 'class'] as $no => $attr) {
37+
$query .= ($no == 0 ? '' : ' | ') . '//@' . $attr;
38+
}
39+
40+
// echo "Query: $query \n";
41+
/** @var \DOMNode $attr */
42+
foreach ($xpath->query($query) as $no => $attr) {
43+
// if ($no < 10) echo "Found attr " . $attr->nodeName . " with value: " . $attr->textContent . "\n";
44+
if (trim($attr->textContent) == '') {
45+
$attr->parentNode->removeAttribute($attr->nodeName);
46+
}
47+
}
48+
});
49+
3350
runTest('Remove whitespace v1', function($d) {
3451
$x = new \DOMXPath($d);
3552
$keep_whitespace_in = ['pre', 'style', 'script'];
@@ -121,9 +138,10 @@
121138
$node->nodeValue = str_replace(' ', ' ', $node->nodeValue);
122139
}
123140

141+
124142
// 1. "Trim" each text node by removing its leading and trailing spaces and newlines.
125143
if (!($node->previousSibling && in_array($node->previousSibling->nodeName, $keep_whitespace_around))) {
126-
$node->nodeValue = ltrim($node->nodeValue);
144+
$node->nodeValue = ltrim($node->nodeValue);
127145
}
128146

129147
if (!($node->nextSibling && in_array($node->nextSibling->nodeName, $keep_whitespace_around))) {
@@ -136,6 +154,39 @@
136154
}
137155
});
138156

157+
runTest('Remove whitespace v4', function($d) {
158+
$x = new \DOMXPath($d);
159+
$keep_whitespace_in = ['pre', 'style', 'script'];
160+
$keep_whitespace_around = ['a', 'b', 'i'];
161+
$nodeList = $x->query("//text()");
162+
foreach($nodeList as $node) {
163+
/** @var \DOMNode $node */
164+
165+
if (in_array($node->parentNode->nodeName, $keep_whitespace_in)) {
166+
continue;
167+
};
168+
169+
$node->nodeValue = str_replace(["\r", "\n", "\t"], ' ', $node->nodeValue);
170+
while (strpos($node->nodeValue, ' ') !== false) {
171+
$node->nodeValue = str_replace(' ', ' ', $node->nodeValue);
172+
}
173+
174+
175+
// 1. "Trim" each text node by removing its leading and trailing spaces and newlines.
176+
if (substr($node->nodeValue, 0, 1) == ' ' && !($node->previousSibling && in_array($node->previousSibling->nodeName, $keep_whitespace_around))) {
177+
$node->nodeValue = ltrim($node->nodeValue);
178+
}
179+
180+
if (substr($node->nodeValue, -1) == ' ' && !($node->nextSibling && in_array($node->nextSibling->nodeName, $keep_whitespace_around))) {
181+
$node->nodeValue = rtrim($node->nodeValue);
182+
}
183+
184+
if((strlen($node->nodeValue) == 0)) {
185+
$node->parentNode->removeChild($node);
186+
}
187+
}
188+
});
189+
139190
////// helper funtions //////
140191

141192
function runTest($description, callable $function)

0 commit comments

Comments
 (0)