Skip to content

Commit 5b77a3b

Browse files
author
Christoph Singer
committed
Merge branch 'ttk-master'
2 parents 450abc3 + c104488 commit 5b77a3b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

PrettyMin.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,21 @@ protected function removeComments($exception_patterns = null)
244244
protected function removeWhitespace() {
245245
// Retrieve all text nodes using XPath
246246
$x = new \DOMXPath($this->doc);
247-
$nodeList = $x->query("//text()");
248-
foreach($nodeList as $node) {
247+
248+
// Ignore child nodes where we need to preserve whitespace on ancestor
249+
$x_filter = array_map(function ($nodeName) {
250+
return "ancestor::$nodeName";
251+
}, $this->options['keep_whitespace_in']);
252+
253+
$x_filter = implode(' or ', $x_filter);
254+
255+
if ($x_filter) {
256+
$nodeList = $x->query("//*[not($x_filter)]/text()");
257+
} else {
258+
$nodeList = $x->query("//text()");
259+
}
260+
261+
foreach ($nodeList as $node) {
249262
/** @var \DOMNode $node */
250263

251264
if (in_array($node->parentNode->nodeName, $this->options['keep_whitespace_in'])) {

Tests/PrettyMinTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,39 @@ function () {
136136

137137
$this->assertEquals($expected, $pm->saveHtml());
138138
}
139+
140+
public function testPre()
141+
{
142+
$pre = <<<HTML
143+
if test
144+
do this
145+
endif
146+
<code>
147+
if test
148+
do this
149+
endif
150+
</code>
151+
<samp>
152+
if test
153+
do this
154+
endif
155+
</samp>
156+
<kbd>
157+
if test
158+
do this
159+
endif
160+
</kbd>
161+
HTML;
162+
163+
$html = "<!DOCTYPE html><html><body><pre>{$pre}</pre></body></html>";
164+
165+
$pm = new PrettyMin();
166+
$pm->load($html);
167+
$pm->indent();
168+
169+
// Contents of the <pre> section needs to match perfectly
170+
preg_match('#<pre>(.*)</pre>#ms', $pm->saveHtml(), $match);
171+
172+
$this->assertEquals(trim($pre), trim($match[1])); // Trailing and leading whitespace is allowed to be different
173+
}
139174
}

0 commit comments

Comments
 (0)