File tree Expand file tree Collapse file tree 5 files changed +21
-5
lines changed Expand file tree Collapse file tree 5 files changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -138,7 +138,7 @@ $dom->load('http://google.com', [
138138$dom->load('http://gmail.com'); // will not have whitespaceTextNode set to false.
139139```
140140
141- At the moment we support 7 options.
141+ At the moment we support 8 options.
142142
143143** Strict**
144144
@@ -168,6 +168,9 @@ Set this to `false` to skip removing of style tags from the document body. This
168168
169169Preserves Line Breaks if set to ` true ` . If set to ` false ` line breaks are cleaned up as part of the input clean up process. Defaults to ` false ` .
170170
171+ ** removeDoubleSpace**
172+
173+ Set this to ` false ` if you want to preserver whitespace inside of text nodes. It is set to ` true ` by default.
171174Static Facade
172175-------------
173176
Original file line number Diff line number Diff line change @@ -565,7 +565,7 @@ protected function parse()
565565 trim ($ str ) != ''
566566 ) {
567567 // we found text we care about
568- $ textNode = new TextNode ($ str );
568+ $ textNode = new TextNode ($ str, $ this -> options -> removeDoubleSpace );
569569 $ activeNode ->addChild ($ textNode );
570570 }
571571 }
Original file line number Diff line number Diff line change @@ -35,10 +35,12 @@ class TextNode extends LeafNode
3535 *
3636 * @param string $text
3737 */
38- public function __construct ($ text )
38+ public function __construct ($ text, $ removeDoubleSpace = true )
3939 {
40- // remove double spaces
41- $ text = mb_ereg_replace ('\s+ ' , ' ' , $ text );
40+ if ($ removeDoubleSpace ) {
41+ // remove double spaces
42+ $ text = mb_ereg_replace ('\s+ ' , ' ' , $ text );
43+ }
4244
4345 // restore line breaks
4446 $ text = str_replace (' ' , "\n" , $ text );
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ class Options
2525 'removeScripts ' => true ,
2626 'removeStyles ' => true ,
2727 'preserveLineBreaks ' => false ,
28+ 'removeDoubleSpace ' => true ,
2829 ];
2930
3031 /**
Original file line number Diff line number Diff line change @@ -377,4 +377,14 @@ public function testFindByIdNotFountEleement()
377377 $ result = $ dom ->findById (8 );
378378 $ this ->assertFalse ($ result );
379379 }
380+
381+ public function testWhitespaceInText ()
382+ {
383+ $ dom = new Dom ();
384+ $ dom ->setOptions (array (
385+ 'removeDoubleSpace ' => false ,
386+ ));
387+ $ dom ->load ('<pre> Hello world</pre> ' );
388+ $ this ->assertEquals ('<pre> Hello world</pre> ' , (string ) $ dom );
389+ }
380390}
You can’t perform that action at this time.
0 commit comments