ported fixes from Mojolicious
Dan Book [Sat, 12 Mar 2016 01:03:55 +0000 (20:03 -0500)]
- the val method returns "on" by default for checkbox or radio inputs with no value

Changes
META.json
Makefile.PL
README.pod
lib/DOM/Tiny.pm
lib/DOM/Tiny/_CSS.pm
t/dom.t

diff --git a/Changes b/Changes
index 52c9ae0..4e70a3a 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@
   - Merge several CSS bugfixes and improvements from Mojolicious 6.31 and 6.32.
   - Merge wrap and wrap_content bugfixes from Mojolicious 6.34.
   - Fix small html_unescape bug in DOM::Tiny::Entities
+  - Merge handling of "on" default values in val method from Mojolicious 6.49.
 
 0.002     2015-11-09 19:28:42 EST
   - Support perl 5.8 (mst)
index ee5941e..7b81c13 100644 (file)
--- a/META.json
+++ b/META.json
@@ -4,7 +4,7 @@
       "Dan Book <dbook@cpan.org>"
    ],
    "dynamic_config" : 0,
-   "generated_by" : "Dist::Zilla version 5.042, CPAN::Meta::Converter version 2.150005",
+   "generated_by" : "Dist::Zilla version 5.043, CPAN::Meta::Converter version 2.150005",
    "license" : [
       "artistic_2"
    ],
index 7b1e928..dd69f3d 100644 (file)
@@ -1,4 +1,4 @@
-# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.042.
+# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.043.
 use strict;
 use warnings;
 
index a033942..fc667fe 100644 (file)
@@ -846,7 +846,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;
@@ -862,6 +862,9 @@ 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>');
index f7da74c..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);
@@ -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,6 +1255,9 @@ 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>');
index 1e890dc..91a7705 100644 (file)
@@ -166,7 +166,7 @@ sub _equation {
   # "n", "4n", "+4n", "-4n", "n+1", "4n-1", "+4n-1" (and other variations)
   return [0, 0]
     unless $equation =~ /^\s*((?:\+|-)?(?:\d+)?)?n\s*((?:\+|-)\s*\d+)?\s*$/i;
-  return [$1 eq '-' ? -1 : $1 eq '' ? 1 : $1, join('', split(' ', $2 || 0))];
+  return [$1 eq '-' ? -1 : !length $1 ? 1 : $1, join('', split(' ', $2 || 0))];
 }
 
 sub _match {
diff --git a/t/dom.t b/t/dom.t
index 0a55239..e7ad2ec 100644 (file)
--- a/t/dom.t
+++ b/t/dom.t
@@ -2214,8 +2214,13 @@ $dom = DOM::Tiny->new(<<EOF);
 <form action="/foo">
   <p>Test</p>
   <input type="text" name="a" value="A" />
+  <input type="checkbox" name="q">
   <input type="checkbox" checked name="b" value="B">
+  <input type="radio" name="r">
   <input type="radio" checked name="c" value="C">
+  <input name="s">
+  <input type="checkbox" name="t" value="">
+  <input type=text name="u">
   <select multiple name="f">
     <option value="F">G</option>
     <optgroup>
@@ -2251,6 +2256,11 @@ is_deeply $dom->find('select')->last->at('option')->val, 'R', 'right value';
 is $dom->at('textarea')->val, 'M', 'right value';
 is $dom->at('button')->val,   'O', 'right value';
 is $dom->find('form input')->last->val, 'P', 'right value';
+is $dom->at('input[name=q]')->val, 'on',  'right value';
+is $dom->at('input[name=r]')->val, 'on',  'right value';
+is $dom->at('input[name=s]')->val, undef, 'no value';
+is $dom->at('input[name=t]')->val, '',    'right value';
+is $dom->at('input[name=u]')->val, undef, 'no value';
 
 # PoCo example with whitespace sensitive text
 $dom = DOM::Tiny->new(<<EOF);