Skip to content

Commit e85e379

Browse files
committed
Added double space option
fixes paquettg#80 close paquettg#80
1 parent 35de083 commit e85e379

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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

169169
Preserves 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.
171174
Static Facade
172175
-------------
173176

src/PHPHtmlParser/Dom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

src/PHPHtmlParser/Dom/TextNode.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff 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);

src/PHPHtmlParser/Options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Options
2525
'removeScripts' => true,
2626
'removeStyles' => true,
2727
'preserveLineBreaks' => false,
28+
'removeDoubleSpace' => true,
2829
];
2930

3031
/**

tests/DomTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)