Skip to content

Commit

Permalink
Fixed issue with spaces befor closing tag
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
paquettg committed Mar 20, 2016
1 parent 80f5474 commit a9257d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PHPHtmlParser/Dom.php
Expand Up @@ -358,6 +358,10 @@ protected function clean($str)
return $str;
}

// remove white space before closing tags
$str = mb_eregi_replace("'\s+>", "'>", $str);
$str = mb_eregi_replace('"\s+>', '">', $str);

// clean out the \n\r
$replace = ' ';
if ($this->options->get('preserveLineBreaks')) {
Expand Down
7 changes: 7 additions & 0 deletions tests/DomTest.php
Expand Up @@ -295,4 +295,11 @@ public function testMultipleSingleQuotes()
$dom->load("<a title='Ain't this the best' href=\"http://www.example.com\">Hello</a>");
$this->assertEquals("Ain't this the best", $dom->getElementsByTag('a')[0]->title);
}

public function testBeforeClosingTag()
{
$dom = new Dom;
$dom->load("<div class=\"stream-container \" > <div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\">");
$this->assertEquals("<div class=\"stream-container \"> <div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\"></div></div>", (string) $dom);
}
}
30 changes: 30 additions & 0 deletions tests/Options/PreserveLineBreaks.php
@@ -0,0 +1,30 @@
<?php

use PHPHtmlParser\Dom;

class PreserveLineBreaks extends PHPUnit_Framework_TestCase {

public function testPreserveLineBreakTrue()
{
$dom = new Dom;
$dom->setOptions([
'preserveLineBreaks' => true,
]);
$dom->load("<div class=\"stream-container \">
<div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\">");

$this->assertEquals("<div class=\"stream-container \">\n<div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\"></div></div>", (string) $dom);
}

public function testPreserveLineBreakBeforeClosingTag()
{
$dom = new Dom;
$dom->setOptions([
'preserveLineBreaks' => true,
]);
$dom->load("<div class=\"stream-container \"
><div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\">");

$this->assertEquals("<div class=\"stream-container \"><div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\"></div></div>", (string) $dom);
}
}

0 comments on commit a9257d0

Please sign in to comment.