File tree 2 files changed +50
-2
lines changed
2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -244,8 +244,21 @@ protected function removeComments($exception_patterns = null)
244
244
protected function removeWhitespace () {
245
245
// Retrieve all text nodes using XPath
246
246
$ 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 ) {
249
262
/** @var \DOMNode $node */
250
263
251
264
if (in_array ($ node ->parentNode ->nodeName , $ this ->options ['keep_whitespace_in ' ])) {
Original file line number Diff line number Diff line change @@ -136,4 +136,39 @@ function () {
136
136
137
137
$ this ->assertEquals ($ expected , $ pm ->saveHtml ());
138
138
}
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
+ }
139
174
}
You can’t perform that action at this time.
0 commit comments