Skip to content

Instantly share code, notes, and snippets.

/xml.diff Secret

Created September 24, 2015 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e45853c9baef8c3bb9a9 to your computer and use it in GitHub Desktop.
Save anonymous/e45853c9baef8c3bb9a9 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/DOM/HTML.pm b/lib/Mojo/DOM/HTML.pm
index 016989a..48d3f8f 100644
--- a/lib/Mojo/DOM/HTML.pm
+++ b/lib/Mojo/DOM/HTML.pm
@@ -228,8 +228,9 @@ sub _render {
# Attributes
for my $key (sort keys %{$tree->[2]}) {
- $result .= " $key" and next unless defined(my $value = $tree->[2]{$key});
- $result .= " $key" . '="' . xml_escape($value) . '"';
+ my $value = $tree->[2]{$key};
+ $result .= " $key" and next if !defined $value && !$xml;
+ $result .= " $key" . '="' . xml_escape($value // $key) . '"';
}
# No children
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 14867c9..52d7e2f 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -1918,7 +1918,8 @@ is $element->parent->tag, 'XMLTest', 'right parent';
ok $element->root->xml, 'XML mode active';
$dom->replace('<XMLTest2 /><XMLTest3 just="works" />');
ok $dom->xml, 'XML mode active';
-is $dom, '<XMLTest2 /><XMLTest3 just="works" />', 'right result';
+$dom->at('XMLTest2')->{foo} = undef;
+is $dom, '<XMLTest2 foo="foo" /><XMLTest3 just="works" />', 'right result';
# Ensure HTML semantics
ok !Mojo::DOM->new->xml(undef)->parse('<?xml version="1.0"?>')->xml,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment