ported fixes from Mojolicious
[catagits/DOM-Tiny.git] / lib / DOM / Tiny.pm
index febc2a5..0eecb5e 100644 (file)
@@ -170,6 +170,11 @@ sub val {
   return defined($self->{value}) ? $self->{value} : $self->text
     if (my $tag = $self->tag) eq 'option';
 
+  # "input" ("type=checkbox" and "type=radio")
+  my $type = $self->{type} || '';
+  return defined $self->{value} ? $self->{value} : 'on'
+    if $tag eq 'input' && ($type eq 'radio' || $type eq 'checkbox');
+
   # "textarea", "input" or "button"
   return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';
 
@@ -235,7 +240,7 @@ sub _content {
   my $tree = $self->tree;
   unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') {
     my $old = $self->content;
-    return $self->content($start ? "$old$new" : "$new$old");
+    return $self->content($start ? $old . $new : $new . $old);
   }
 
   $start  = $start  ? ($#$tree + 1) : _start($tree);
@@ -364,8 +369,8 @@ sub _text {
 sub _wrap {
   my ($self, $content, $new) = @_;
 
-  $content = 1 if (my $tree = $self->tree)->[0] eq 'root';
-  $content = 0 if $tree->[0] ne 'root' && $tree->[0] ne 'tag';
+  return $self if (my $tree = $self->tree)->[0] eq 'root' && !$content;
+  return $self if $tree->[0] ne 'root' && $tree->[0] ne 'tag' && $content;
 
   # Find innermost tag
   my $current;
@@ -782,7 +787,7 @@ objects. All selectors listed in L</"SELECTORS"> are supported.
 
   $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
 
-Append HTML/XML fragment to this node.
+Append HTML/XML fragment to this node (for all node types other than C<root>).
 
   # "<div><h1>Test</h1><h2>123</h2></div>"
   $dom->parse('<div><h1>Test</h1></div>')
@@ -1050,7 +1055,7 @@ before this node as L<DOM::Tiny> objects.
 
   $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
 
-Prepend HTML/XML fragment to this node.
+Prepend HTML/XML fragment to this node (for all node types other than C<root>).
 
   # "<div><h1>Test</h1><h2>123</h2></div>"
   $dom->parse('<div><h2>123</h2></div>')
@@ -1234,7 +1239,7 @@ C<selected> attribute and return an array reference with all values, or
 C<undef> if none could be found.
 
   # "a"
-  $dom->parse('<input name="test" value="a">')->at('input')->val;
+  $dom->parse('<input name=test value=a>')->at('input')->val;
 
   # "b"
   $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
@@ -1250,12 +1255,15 @@ C<undef> if none could be found.
   $dom->parse('<select multiple><option selected>e</option></select>')
     ->at('select')->val->[0];
 
+  # "on"
+  $dom->parse('<input name=test type=checkbox>')->at('input')->val;
+
 =head2 wrap
 
   $dom = $dom->wrap('<div></div>');
 
-Wrap HTML/XML fragment around this node, placing it as the last child of the
-first innermost element.
+Wrap HTML/XML fragment around this node (for all node types other than C<root>),
+placing it as the last child of the first innermost element.
 
   # "<p>123<b>Test</b></p>"
   $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
@@ -1273,8 +1281,8 @@ first innermost element.
 
   $dom = $dom->wrap_content('<div></div>');
 
-Wrap HTML/XML fragment around this node's content, placing it as the last
-children of the first innermost element.
+Wrap HTML/XML fragment around this node's content (for C<root> and C<tag>
+nodes), placing it as the last children of the first innermost element.
 
   # "<p><b>123Test</b></p>"
   $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;