Add support for [attr~="value"] selector (attribute contains word)
[catagits/HTML-Zoom.git] / t / selectors.t
index 01e11cf..13a8154 100644 (file)
@@ -4,70 +4,119 @@ use Test::More;
 
 use HTML::Zoom;
 
+my $tmpl = <<END;
+<body>
+  <div class="main">
+    <span prop='moo' class="hilight name">Bob</span>
+    <span class="career">Builder</span>
+    <hr />
+  </div>
+</body>
+END
+
+my $stub = '<div class="waargh"></div>';
+
 # el#id
-is( HTML::Zoom->from_html('<div id="yo"></div>')
+is( HTML::Zoom->from_html('<div id="yo"></div>'.$stub)
    ->select('div#yo')
       ->replace_content('grg')
    ->to_html,
-   '<div id="yo">grg</div>',
+   '<div id="yo">grg</div>'.$stub,
    'E#id works' );
 
 # el.class1
-is( HTML::Zoom->from_html('<div class="yo"></div>')
+is( HTML::Zoom->from_html('<div class="yo"></div>'.$stub)
    ->select('div.yo')
       ->replace_content('grg')
    ->to_html,
-   '<div class="yo">grg</div>',
+   '<div class="yo">grg</div>'.$stub,
    'E.class works' );
 
 # el[attr]
-is( HTML::Zoom->from_html('<div frew="yo"></div>')
+is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
    ->select('div[frew]')
       ->replace_content('grg')
    ->to_html,
-   '<div frew="yo">grg</div>',
+   '<div frew="yo">grg</div>'.$stub,
    'E[attr] works' );
 
 # el[attr="foo"]
-is( HTML::Zoom->from_html('<div frew="yo"></div>')
+is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
    ->select('div[frew="yo"]')
       ->replace_content('grg')
    ->to_html,
-   '<div frew="yo">grg</div>',
+   '<div frew="yo">grg</div>'.$stub,
    'E[attr="val"] works' );
 
+# el[attr=foo]
+is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
+    ->select('div[frew=yo]')
+    ->replace_content('grg')
+    ->to_html,
+    '<div frew="yo">grg</div>'.$stub,
+    'E[attr=val] works' );
+
 # el[attr*="foo"]
-is( HTML::Zoom->from_html('<div f="frew goog"></div>')
+is( HTML::Zoom->from_html('<div f="frew goog"></div>'.$stub)
    ->select('div[f*="oo"]')
       ->replace_content('grg')
    ->to_html,
-   '<div f="frew goog">grg</div>',
+   '<div f="frew goog">grg</div>'.$stub,
    'E[attr*="val"] works' );
 
 # el[attr^="foo"]
-is( HTML::Zoom->from_html('<div f="foobar"></div>')
+is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub)
    ->select('div[f^="foo"]')
       ->replace_content('grg')
    ->to_html,
-   '<div f="foobar">grg</div>',
+   '<div f="foobar">grg</div>'.$stub,
    'E[attr^="val"] works' );
 
 # el[attr$="foo"]
-is( HTML::Zoom->from_html('<div f="foobar"></div>')
+is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub)
    ->select('div[f$="bar"]')
       ->replace_content('grg')
    ->to_html,
-   '<div f="foobar">grg</div>',
+   '<div f="foobar">grg</div>'.$stub,
    'E[attr$="val"] works' );
 
 # el[attr*="foo"]
-is( HTML::Zoom->from_html('<div f="foo bar"></div>')
+is( HTML::Zoom->from_html('<div f="foo bar"></div>'.$stub)
    ->select('div[f*="bar"]')
       ->replace_content('grg')
    ->to_html,
-   '<div f="foo bar">grg</div>',
+   '<div f="foo bar">grg</div>'.$stub,
    'E[attr*="val"] works' );
 
+# el[attr~="foo"]
+is( HTML::Zoom->from_html('<div frew="foo bar baz"></div>'.$stub)
+   ->select('div[frew~="bar"]')
+      ->replace_content('grg')
+   ->to_html,
+   '<div frew="foo bar baz">grg</div>'.$stub,
+   'E[attr~="val"] works' );
+
+# [attr=bar]
+ok( check_select( '[prop=moo]'), '[attr=bar]' );
+
+# el[attr=bar],[prop=foo]
+is( check_select('span[class=career],[prop=moo]'), 2,
+    'Multiple selectors: el[attr=bar],[attr=foo]');
+
+
+# selector parse error test:
+eval{
+    HTML::Zoom->from_html('<span att="bar"></span>')
+      ->select('[att=bar')
+      ->replace_content('cats')
+          ->to_html;
+};
+like( $@, qr/Error parsing dispatch specification/,
+      'Malformed attribute selector ([att=bar) results in a helpful error' );
+
+=pod
+
 # sel1 sel2
 is( HTML::Zoom->from_html('<table><tr></tr><tr></tr></table>')
    ->select('table tr')
@@ -85,4 +134,21 @@ is( HTML::Zoom->from_html('<table><tr><td></td></tr><tr><td></td></tr></table>')
    '<table><tr><td>frew</td></tr><tr><td>frew</td></tr></table>',
    'sel1 sel2 sel3 works' );
 
+
+
+=cut
+
 done_testing;
+
+
+sub check_select{
+    # less crude?:
+    my $output = HTML::Zoom
+    ->from_html($tmpl)
+    ->select(shift)->replace("the monkey")->to_html;
+    my $count = 0;
+    while ( $output =~ /the monkey/g ){
+        $count++;
+    }
+    return $count;
+}