Skip to content

Commit

Permalink
convert empty tags to self-closing. fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
John Lifsey committed Dec 20, 2018
1 parent b604ab8 commit 08de698
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/XML/Simple/Minded.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ use overload '""' => sub {
1;
}
my $sx = XML::Simple::Minded::Sorter->new( keep_root=>$self->_root ? 1 : 0, xml_decl=>'<?xml version="1.0" encoding="UTF-8"?>', no_indent=>1 );
return $sx->XMLout($rstruc);
my $xml_string = $sx->XMLout($rstruc);
$xml_string =~ s/<(\w+)><\/(\w+)>/&selfclose($1,$2)/gse;
sub selfclose {
my ($one, $two) = @_;
return ($one eq $two) ? "<$one/>" : "<$one></$two>"
}
return $xml_string;
};

our $AUTOLOAD;
Expand Down
7 changes: 5 additions & 2 deletions t/01-xml-simple-minded.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ BEGIN {
use_ok('XML::Simple::Minded');
}

my $xml = XML::Simple::Minded->new("<this that=\"option\">valueforthis</this>");
my $xml = XML::Simple::Minded->new("<this that=\"option\">valueforthis<empty></empty></this>");
isa_ok($xml, 'XML::Simple::Minded');
is($xml->this->content,'valueforthis', 'contents');
is($xml->this->that,'option','attributes');
my $xml_string = $xml.'';
ok($xml_string =~ m/<empty\/>/, 'empty tags are self-closing');
ok($xml_string !~ m/<\/empty>/, 'self-closing tags are clean');
$xml = undef;

$xml = XML::Simple::Minded->new("$FindBin::Bin/systems17.raw");
Expand All @@ -18,7 +21,7 @@ is($xml->system->config->mode,'auto','XML parses');
$xml->system->config->testitem(['test value']);
is($xml->system->config->testitem,'test value','New values added');

my $xml_string = $xml."";
$xml_string = $xml."";
ok($xml_string =~ m/^<\?xml/,'xml stringifies definition');
ok($xml_string =~ m/<\/system>$/,'xml stringifies system');
ok($xml_string =~ m/<testitem>test value<\/testitem>/,'new test item added');
Expand Down

0 comments on commit 08de698

Please sign in to comment.