@@ -29,11 +29,14 @@ class Compiler
29
29
30
30
/** @var string[] */
31
31
protected $ banner ;
32
+
32
33
/**
33
34
* @var TwigBuilder
34
35
*/
35
36
protected $ builder ;
36
37
38
+ protected $ stripWhitespace = true ;
39
+
37
40
public function __construct (DOMDocument $ document , LoggerInterface $ logger )
38
41
{
39
42
$ this ->builder = new TwigBuilder ();
@@ -75,6 +78,10 @@ public function convert(): string
75
78
76
79
$ html = $ this ->replacePlaceholders ($ html );
77
80
81
+ if ($ this ->stripWhitespace ) {
82
+ $ html = $ this ->stripWhitespace ($ html );
83
+ }
84
+
78
85
if (!empty ($ this ->banner )) {
79
86
$ html = $ this ->addBanner ($ html );
80
87
}
@@ -420,7 +427,7 @@ protected function addBanner(string $html)
420
427
421
428
$ bannerLines [] = ' #} ' ;
422
429
423
- $ html = implode ("\n" , $ bannerLines )."\n" .$ html ;
430
+ $ html = implode (PHP_EOL , $ bannerLines ).PHP_EOL .$ html ;
424
431
425
432
return $ html ;
426
433
}
@@ -438,4 +445,33 @@ public function refactorTemplateString($value)
438
445
439
446
return $ value ;
440
447
}
448
+
449
+ public function stripWhitespace ($ html )
450
+ {
451
+ $ html = preg_replace ('/(\s)+/s ' , '\\1 ' , $ html );
452
+ $ html = str_replace ("\n" , '' , $ html );
453
+
454
+ // Trim node text
455
+ $ html = preg_replace ('/\>[^\S ]+/s ' , "> " , $ html );
456
+ $ html = preg_replace ('/[^\S ]+\</s ' , "< " , $ html );
457
+
458
+ $ html = preg_replace ('/> </s ' , '>< ' , $ html );
459
+ $ html = preg_replace ('/} </s ' , '}< ' , $ html );
460
+ $ html = preg_replace ('/> {/s ' , '>{ ' , $ html );
461
+ $ html = preg_replace ('/} {/s ' , '}{ ' , $ html );
462
+
463
+ return $ html ;
464
+ }
465
+
466
+ /**
467
+ * @param bool $stripWhitespace
468
+ *
469
+ * @return Compiler
470
+ */
471
+ public function setStripWhitespace (bool $ stripWhitespace ): Compiler
472
+ {
473
+ $ this ->stripWhitespace = $ stripWhitespace ;
474
+
475
+ return $ this ;
476
+ }
441
477
}
0 commit comments