Skip to content

Commit

Permalink
fix encoding of empty tag in simple format
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Jun 9, 2015
1 parent 39d2ced commit a1ccc1e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ Changelog for XML-Struct

{{$NEXT}}
- fix builder support of Moo 1.0
- fix encoding of empty tag in simple format as {}

0.25 2015-05-22 15:05:07 CEST
- allow resetting to/handler of writers
Expand Down
6 changes: 3 additions & 3 deletions lib/XML/Struct/Simple.pm
Expand Up @@ -65,10 +65,10 @@ sub transform_content {
# no element children
unless ( first { ref $_ } @$children ) {
my $content = join "", @$children;
if (!%$attributes) {
return $content;
} elsif ($content eq '') {
if ($content eq '') {
return { %$attributes };
} elsif (!%$attributes) {
return $content;
} else {
return { %$attributes, $self->content => $content };
}
Expand Down
19 changes: 14 additions & 5 deletions t/reader.t
Expand Up @@ -99,12 +99,21 @@ is_deeply \@nodes, [ { n => 1 }, { n => 2 }, { n => 4 } ], 'read simple as loop'

# read from DOM
my $dom = XML::LibXML->load_xml( string => "<root><element/></root>" );
$data = readXML($dom);
is_deeply $data, [ root => { }, [ [ element => { }, [ ] ] ] ],
is_deeply readXML($dom),
[ root => { }, [ [ element => { }, [ ] ] ] ],
'read from XML::LibXML::Document';
$dom = $dom->documentElement();
$data = readXML($dom);
is_deeply $data, [ root => { }, [ [ element => { }, [ ] ] ] ],

is_deeply readXML($dom, simple => 1, root => 1),
{ root => { element => {} } },
'empty tags in simple format';

is_deeply readXML($dom->documentElement),
[ root => { }, [ [ element => { }, [ ] ] ] ],
'read from XML::LibXML::Element';

$dom = XML::LibXML->load_xml( string => "<root/>" );
is_deeply readXML( $dom, simple => 1, root => 1 ),
{ root => {} },
'empty tag as root';

done_testing;
20 changes: 17 additions & 3 deletions t/simple.t
Expand Up @@ -41,16 +41,30 @@ foreach ('remove','0') {
'remove attributes';
}

is_deeply( XML::Struct::Simple->new->transform( [ root => [ ['text'] ] ] ),
is_deeply(
XML::Struct::Simple->new->transform(
[ root => [ ['text'] ] ] ),
{ text => {} }, 'empty tag');

# this was a bug until 0.25
is_deeply(
XML::Struct::Simple->new->transform(
[ root => [ ['text', {} ] ] ] ),
{ text => {} }, 'empty tag, no attributes');

is_deeply(
XML::Struct::Simple->new( root => 1 )->transform( [ 'root' ] ),
{ root => {} }, 'empty <root/>');

is_deeply( XML::Struct::Simple->new->transform( [ root => ['text'] ] ),
{ root => 'text' }, 'special case <root>text</root>');

is_deeply( XML::Struct::Simple->new->transform( [ root => { x => 1 }, [] ] ),
is_deeply( XML::Struct::Simple->new->transform(
[ root => { x => 1 }, [] ] ),
{ x => 1 }, 'attributes only');

is_deeply( XML::Struct::Simple->new->transform( [ root => { x => 1 }, ['text'] ] ),
is_deeply( XML::Struct::Simple->new->transform(
[ root => { x => 1 }, ['text'] ] ),
{ x => 1, content => 'text' }, 'mix attributes and text content');

done_testing;

0 comments on commit a1ccc1e

Please sign in to comment.