Skip to content

Commit

Permalink
fixed smart whitespace trimming bug in Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 19, 2013
1 parent 8b324fa commit 1f82baa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.40 2013-09-19
- Fixed smart whitespace trimming bug in Mojo::DOM.
- Fixed table parsing bug in Mojo::DOM::HTML.

4.39 2013-09-17
Expand Down
7 changes: 7 additions & 0 deletions lib/Mojo/DOM.pm
Expand Up @@ -329,6 +329,13 @@ sub _sibling {
sub _text {
my ($nodes, $recurse, $trim) = @_;

# Merge successing text nodes
my $i = 0;
while (my $next = $nodes->[$i + 1]) {
++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text';
splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]];
}

my $text = '';
for my $n (@$nodes) {
my $type = $n->[0];
Expand Down
7 changes: 1 addition & 6 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -84,12 +84,7 @@ sub parse {

# Text (and runaway "<")
$text .= '<' if defined $runaway;
if (length $text) {
$text = html_unescape $text;
my $sibling = $current->[-1];
if (ref $sibling && $sibling->[0] eq 'text') { $sibling->[1] .= $text }
else { push @$current, ['text', $text] }
}
push @$current, ['text', html_unescape $text] if length $text;

# DOCTYPE
if ($doctype) { push @$current, ['doctype', $doctype] }
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/dom.t
Expand Up @@ -1100,7 +1100,7 @@ is "$dom", <<EOF, 'right result';
<div>D</div>works
EOF
$dom->at('li')->prepend_content('A3<p>A2</p>')->prepend_content('A4');
is $dom->at('li')->text, 'A4 A3 A', 'right text';
is $dom->at('li')->text, 'A4A3 A', 'right text';
is "$dom", <<EOF, 'right result';
<ul>
24<div>A-1</div>works25<li>A4A3<p>A2</p>A</li><p>A1</p>23
Expand All @@ -1109,13 +1109,13 @@ is "$dom", <<EOF, 'right result';
</ul>
<div>D</div>works
EOF
$dom->find('li')->[1]->append_content('<p>C2</p>C3')->append_content('C4');
$dom->find('li')->[1]->append_content('<p>C2</p>C3')->append_content(' C4');
is $dom->find('li')->[1]->text, 'C C3 C4', 'right text';
is "$dom", <<EOF, 'right result';
<ul>
24<div>A-1</div>works25<li>A4A3<p>A2</p>A</li><p>A1</p>23
<p>B</p>
<li>C<p>C2</p>C3C4</li>
<li>C<p>C2</p>C3 C4</li>
</ul>
<div>D</div>works
EOF
Expand Down

0 comments on commit 1f82baa

Please sign in to comment.