ported fixes from Mojolicious
[catagits/DOM-Tiny.git] / t / dom.t
diff --git a/t/dom.t b/t/dom.t
index d80a3c3..e7ad2ec 100644 (file)
--- a/t/dom.t
+++ b/t/dom.t
@@ -3,6 +3,7 @@ use warnings;
 use utf8;
 use Test::More;
 use DOM::Tiny;
+use JSON::PP ();
 
 # Empty
 is(DOM::Tiny->new,                     '',    'right result');
@@ -242,6 +243,8 @@ is $dom->at('b')->child_nodes->[1]->next, undef, 'no siblings';
 is $dom->at('script')->child_nodes->first->wrap('<i>:)</i>')->root,
   '<script><i>:)a</i><b>fce</b>1<b>d</b></script>', 'right result';
 is $dom->at('i')->child_nodes->first->wrap_content('<b></b>')->root,
+  '<script><i>:)a</i><b>fce</b>1<b>d</b></script>', 'no changes';
+is $dom->at('i')->child_nodes->first->wrap('<b></b>')->root,
   '<script><i><b>:)</b>a</i><b>fce</b>1<b>d</b></script>', 'right result';
 is $dom->at('b')->child_nodes->first->ancestors->map('tag')->join(','),
   'b,i,script', 'right result';
@@ -978,28 +981,28 @@ $dom->find('li:nth-last-child(4n+1)')->each(sub { push @li, shift->text });
 is_deeply \@li, [qw(D H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child(4n+4)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(D H)], 'found the right li element';
+is_deeply \@li, [qw(D H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-last-child(4n+4)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A E)], 'found the right li element';
+is_deeply \@li, [qw(A E)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child(4n)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(D H)], 'found the right li element';
+is_deeply \@li, [qw(D H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child( 4n )')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(D H)], 'found the right li element';
+is_deeply \@li, [qw(D H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-last-child(4n)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A E)], 'found the right li element';
+is_deeply \@li, [qw(A E)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child(5n-2)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(C H)], 'found the right li element';
+is_deeply \@li, [qw(C H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child( 5n - 2 )')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(C H)], 'found the right li element';
+is_deeply \@li, [qw(C H)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-last-child(5n-2)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A F)], 'found the right li element';
+is_deeply \@li, [qw(A F)], 'found the right li elements';
 @li = ();
 $dom->find('li:nth-child(-n+3)')->each(sub { push @li, shift->text });
 is_deeply \@li, [qw(A B C)], 'found first three li elements';
@@ -1028,26 +1031,43 @@ is_deeply \@li, [qw(C F)], 'found every third li elements';
 $dom->find('li:Nth-Last-Child(3N)')->each(sub { push @li, shift->text });
 is_deeply \@li, [qw(C F)], 'found every third li elements';
 @li = ();
-$dom->find('li:nth-child(3)')->each(sub { push @li, shift->text });
+$dom->find('li:nth-child( 3 )')->each(sub { push @li, shift->text });
 is_deeply \@li, ['C'], 'found third li element';
 @li = ();
-$dom->find('li:nth-last-child(3)')->each(sub { push @li, shift->text });
+$dom->find('li:nth-last-child( +3 )')->each(sub { push @li, shift->text });
 is_deeply \@li, ['F'], 'found third last li element';
 @li = ();
 $dom->find('li:nth-child(1n+0)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
+@li = ();
+$dom->find('li:nth-child(1n-0)')->each(sub { push @li, shift->text });
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
+@li = ();
+$dom->find('li:nth-child(n+0)')->each(sub { push @li, shift->text });
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
+@li = ();
+$dom->find('li:nth-child(n)')->each(sub { push @li, shift->text });
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
 @li = ();
 $dom->find('li:nth-child(n+0)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
 @li = ();
 $dom->find('li:NTH-CHILD(N+0)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
 @li = ();
 $dom->find('li:Nth-Child(N+0)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
 @li = ();
 $dom->find('li:nth-child(n)')->each(sub { push @li, shift->text });
-is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
+is_deeply \@li, [qw(A B C D E F G)], 'found all li elements';
+@li = ();
+$dom->find('li:nth-child(0n+1)')->each(sub { push @li, shift->text });
+is_deeply \@li, [qw(A)], 'found first li element';
+is $dom->find('li:nth-child(0n+0)')->size,     0, 'no results';
+is $dom->find('li:nth-child(0)')->size,        0, 'no results';
+is $dom->find('li:nth-child()')->size,         0, 'no results';
+is $dom->find('li:nth-child(whatever)')->size, 0, 'no results';
+is $dom->find('li:whatever(whatever)')->size,  0, 'no results';
 
 # Even more pseudo-classes
 $dom = DOM::Tiny->new(<<EOF);
@@ -1126,6 +1146,9 @@ is_deeply \@e, ['I'], 'found last child';
 $dom->find('ul :nth-child(-n+3):not(li)')->each(sub { push @e, shift->text });
 is_deeply \@e, ['B'], 'found first p element';
 @e = ();
+$dom->find('ul :nth-child(-n+3):NOT(li)')->each(sub { push @e, shift->text });
+is_deeply \@e, ['B'], 'found first p element';
+@e = ();
 $dom->find('ul :nth-child(-n+3):not(:first-child)')
   ->each(sub { push @e, shift->text });
 is_deeply \@e, [qw(B C)], 'found second and third element';
@@ -1249,8 +1272,9 @@ is "$dom", <<EOF, 'right result';
 EOF
 is $dom->at('div')->text, 'A-1', 'right text';
 is $dom->at('iv'), undef, 'no result';
-$dom->prepend('l')->prepend('alal')->prepend('a');
-is "$dom", <<EOF, 'no change';
+is $dom->prepend('l')->prepend('alal')->prepend('a')->type, 'root',
+  'right type';
+is "$dom", <<EOF, 'no changes';
 <ul>
     24<div>A-1</div>25<li>A</li><p>A1</p>23
     <p>B</p>
@@ -1258,8 +1282,8 @@ is "$dom", <<EOF, 'no change';
 </ul>
 <div>D</div>
 EOF
-$dom->append('lalala');
-is "$dom", <<EOF, 'no change';
+is $dom->append('lalala')->type, 'root', 'right type';
+is "$dom", <<EOF, 'no changes';
 <ul>
     24<div>A-1</div>25<li>A</li><p>A1</p>23
     <p>B</p>
@@ -2104,7 +2128,10 @@ is $dom->all_text, 'DOM Test', 'right text';
 
 # Wrap elements
 $dom = DOM::Tiny->new('<a>Test</a>');
+is "$dom", '<a>Test</a>', 'right result';
 is $dom->wrap('<b></b>')->type, 'root', 'right type';
+is "$dom", '<a>Test</a>', 'no changes';
+is $dom->at('a')->wrap('<b></b>')->type, 'tag', 'right type';
 is "$dom", '<b><a>Test</a></b>', 'right result';
 is $dom->at('b')->strip->at('a')->wrap('A')->tag, 'a', 'right tag';
 is "$dom", '<a>Test</a>', 'right result';
@@ -2187,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>
@@ -2224,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);
@@ -2491,9 +2528,12 @@ is $dom->tree->[3][1], ' bad idea -- HTML5 ', 'right comment';
 is $dom->tree->[5][1], ' HTML4 ',             'right comment';
 is $dom->tree->[7][1], ' bad idea -- HTML4 ', 'right comment';
 
-# Huge number of attributes
-$dom = DOM::Tiny->new('<div ' . ('a=b ' x 32768) . '>Test</div>');
-is $dom->at('div[a=b]')->text, 'Test', 'right text';
+SKIP: {
+  skip 'Regex subexpression recursion causes SIGSEGV on 5.8', 1 unless $] >= 5.010000;
+  # Huge number of attributes
+  $dom = DOM::Tiny->new('<div ' . ('a=b ' x 32768) . '>Test</div>');
+  is $dom->at('div[a=b]')->text, 'Test', 'right text';
+}
 
 # Huge number of nested tags
 my $huge = ('<a>' x 100) . 'works' . ('</a>' x 100);
@@ -2501,4 +2541,7 @@ $dom = DOM::Tiny->new($huge);
 is $dom->all_text, 'works', 'right text';
 is "$dom", $huge, 'right result';
 
+# TO_JSON
+is +JSON::PP->new->convert_blessed->encode([DOM::Tiny->new('<a></a>')]), '["<a></a>"]', 'right result';
+
 done_testing();