|
20 | 20 | }
|
21 | 21 | });
|
22 | 22 |
|
23 |
| -runTest('Select empty attributes using complicated XPath function', function($d) { |
| 23 | +runTest('Select empty attributes using XPath string functions', function($d) { |
24 | 24 | $xpath = new \DOMXPath($d);
|
25 | 25 | foreach (['style', 'class'] as $attr) {
|
26 | 26 | /** @var \DOMElement $el */
|
|
30 | 30 | }
|
31 | 31 | });
|
32 | 32 |
|
| 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 | + |
33 | 50 | runTest('Remove whitespace v1', function($d) {
|
34 | 51 | $x = new \DOMXPath($d);
|
35 | 52 | $keep_whitespace_in = ['pre', 'style', 'script'];
|
|
121 | 138 | $node->nodeValue = str_replace(' ', ' ', $node->nodeValue);
|
122 | 139 | }
|
123 | 140 |
|
| 141 | + |
124 | 142 | // 1. "Trim" each text node by removing its leading and trailing spaces and newlines.
|
125 | 143 | if (!($node->previousSibling && in_array($node->previousSibling->nodeName, $keep_whitespace_around))) {
|
126 |
| - $node->nodeValue = ltrim($node->nodeValue); |
| 144 | + $node->nodeValue = ltrim($node->nodeValue); |
127 | 145 | }
|
128 | 146 |
|
129 | 147 | if (!($node->nextSibling && in_array($node->nextSibling->nodeName, $keep_whitespace_around))) {
|
|
136 | 154 | }
|
137 | 155 | });
|
138 | 156 |
|
| 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 | + |
139 | 190 | ////// helper funtions //////
|
140 | 191 |
|
141 | 192 | function runTest($description, callable $function)
|
|
0 commit comments