X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fselectors.t;h=e6be2c229704013e613a8d3cac6a110797d3d8e6;hb=24725e7b32b680b376c06b1f9d641ec11fc1d067;hp=01e11cffaa335e46e512502c8cb0a8c1fe6e5e36;hpb=3de6bc4440a5c081930d9a4c5762016d24395ada;p=catagits%2FHTML-Zoom.git diff --git a/t/selectors.t b/t/selectors.t index 01e11cf..e6be2c2 100644 --- a/t/selectors.t +++ b/t/selectors.t @@ -4,85 +4,157 @@ use Test::More; use HTML::Zoom; +my $tmpl = < +
+ Bob + Builder +
+
+ +END + +my $stub = '
'; + # el#id -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div#yo') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E#id works' ); # el.class1 -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div.yo') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E.class works' ); # el[attr] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[frew]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr] works' ); # el[attr="foo"] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[frew="yo"]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr="val"] works' ); +# el[attr=foo] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[frew=yo]') + ->replace_content('grg') + ->to_html, + '
grg
'.$stub, + 'E[attr=val] works' ); + +# el[attr!="foo"] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[class!="waargh"]') + ->replace_content('grg') + ->to_html, + '
grg
grg
'.$stub, + 'E[attr!="val"] works' ); + # el[attr*="foo"] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[f*="oo"]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr*="val"] works' ); # el[attr^="foo"] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[f^="foo"]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr^="val"] works' ); # el[attr$="foo"] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[f$="bar"]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr$="val"] works' ); # el[attr*="foo"] -is( HTML::Zoom->from_html('
') +is( HTML::Zoom->from_html('
'.$stub) ->select('div[f*="bar"]') ->replace_content('grg') ->to_html, - '
grg
', + '
grg
'.$stub, 'E[attr*="val"] works' ); +# el[attr~="foo"] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[frew~="bar"]') + ->replace_content('grg') + ->to_html, + '
grg
'.$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('') + ->select('[att=bar') + ->replace_content('cats') + ->to_html; +}; +like( $@, qr/Error parsing dispatch specification/, + 'Malformed attribute selector ([att=bar) results in a helpful error' ); + + +TODO: { +local $TODO = "descendant selectors doesn't work yet"; # sel1 sel2 -is( HTML::Zoom->from_html('
') +is( eval { HTML::Zoom->from_html('
') ->select('table tr') - ->replace_content(\'') - ->to_html, + ->replace_content('') + ->to_html }, '
', 'sel1 sel2 works' ); - +diag($@) if $@; # sel1 sel2 sel3 -is( HTML::Zoom->from_html('
') +is( eval { HTML::Zoom->from_html('
') ->select('table tr td') ->replace_content('frew') - ->to_html, + ->to_html }, '
frew
frew
', 'sel1 sel2 sel3 works' ); +diag($@) if $@; +} 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; +}