Skip to content

Commit

Permalink
improved Mojo::DOM::HTML to generate better HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 3, 2014
1 parent a6dc994 commit baf84d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

4.76 2014-02-03
4.76 2014-02-04
- Updated IO::Socket::IP requirement to 0.20 for certain bug fixes.
- Improved Mojo::DOM::HTML to generate better HTML.

4.75 2014-02-02
- Fixed and readded support for permessage-deflate WebSocket compression.
Expand Down
20 changes: 10 additions & 10 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -70,7 +70,7 @@ $END{$_} = ['p'] for @PARAGRAPH;
my %TABLE = map { $_ => 1 } qw(colgroup tbody td tfoot th thead tr);

# HTML elements without end tags
my %VOID = map { $_ => 1 } (
my %EMPTY = map { $_ => 1 } (
qw(area base br col embed hr img input keygen link menuitem meta param),
qw(source track wbr)
);
Expand Down Expand Up @@ -124,7 +124,7 @@ sub parse {

# Element without end tag
_end($start, $xml, \$current)
if (!$xml && $VOID{$start}) || $attr =~ m!/\s*$!;
if (!$xml && $EMPTY{$start}) || $attr =~ m!/\s*$!;

# Relaxed "script" or "style" HTML elements
next if $xml || ($start ne 'script' && $start ne 'style');
Expand Down Expand Up @@ -225,12 +225,12 @@ sub _render {
return '<?' . $tree->[1] . '?>' if $type eq 'pi';

# Start tag
my $content = '';
my $result = '';
if ($type eq 'tag') {

# Open tag
my $tag = $tree->[1];
$content .= "<$tag";
$result .= "<$tag";

# Attributes
my @attrs;
Expand All @@ -242,24 +242,24 @@ sub _render {
# Key and value
push @attrs, qq{$key="} . xml_escape($value) . '"';
}
$content .= join ' ', '', @attrs if @attrs;
$result .= join ' ', '', @attrs if @attrs;

# Element without end tag
return $xml || $VOID{$tag} ? "$content />" : "$content></$tag>"
return $xml ? "$result />" : $EMPTY{$tag} ? "$result>" : "$result></$tag>"
unless $tree->[4];

# Close tag
$content .= '>';
$result .= '>';
}

# Render whole tree
$content .= _render($tree->[$_], $xml)
$result .= _render($tree->[$_], $xml)
for ($type eq 'root' ? 1 : 4) .. $#$tree;

# End tag
$content .= '</' . $tree->[1] . '>' if $type eq 'tag';
$result .= '</' . $tree->[1] . '>' if $type eq 'tag';

return $content;
return $result;
}

sub _start {
Expand Down
14 changes: 7 additions & 7 deletions t/mojo/dom.t
Expand Up @@ -107,7 +107,7 @@ is "$dom", <<EOF, 'right result';
<?boom lalalala ?>
<a bit broken little>
&lt; very broken
<br />
<br>
more text
</a></foo>
EOF
Expand Down Expand Up @@ -685,7 +685,7 @@ is_deeply \@div, [qw(A B 0)], 'found all div elements with id';

# Empty tags
$dom = Mojo::DOM->new->parse('<hr /><br/><br id="br"/><br />');
is "$dom", '<hr /><br /><br id="br" /><br />', 'right result';
is "$dom", '<hr><br><br id="br"><br>', 'right result';
is $dom->at('br')->content_xml, '', 'empty result';

# Inner XML
Expand Down Expand Up @@ -1736,9 +1736,9 @@ is $dom, <<EOF, 'right result';
<body>
<table>
<tr>
<td><font><br />te<br />st<br />1</font></td>
<td>x1</td><td><img />tes<br />t2</td>
<td>x2</td><td><font>t<br />est3</font></td>
<td><font><br>te<br>st<br>1</font></td>
<td>x1</td><td><img>tes<br>t2</td>
<td>x2</td><td><font>t<br>est3</font></td>
</tr>
</table>
</body>
Expand Down Expand Up @@ -1786,9 +1786,9 @@ $element = $dom->at('XMLTest')->children->[0];
is $element->type, 'Element', 'right child';
is $element->parent->type, 'XMLTest', 'right parent';
ok $element->root->xml, 'XML mode active';
$dom->replace('<XMLTest2 />');
$dom->replace('<XMLTest2 /><XMLTest3 just="works" />');
ok $dom->xml, 'XML mode active';
is $dom, '<XMLTest2 />', 'right result';
is $dom, '<XMLTest2 /><XMLTest3 just="works" />', 'right result';

# Ensure HTML semantics
ok !Mojo::DOM->new->xml(undef)->parse('<?xml version="1.0"?>')->xml,
Expand Down

0 comments on commit baf84d8

Please sign in to comment.