X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fselectors.t;h=c8b5b11630c01bdd2016f26ba6d4d2cbd2e9ebe1;hb=5bfa7aeb8328819f71cc7b3603a195a943e27b45;hp=01e11cffaa335e46e512502c8cb0a8c1fe6e5e36;hpb=3de6bc4440a5c081930d9a4c5762016d24395ada;p=catagits%2FHTML-Zoom.git diff --git a/t/selectors.t b/t/selectors.t index 01e11cf..c8b5b11 100644 --- a/t/selectors.t +++ b/t/selectors.t @@ -1,88 +1,205 @@ -use strict; -#use warnings FATAL => 'all'; +use strictures 1; 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.class\.1 +is( HTML::Zoom->from_html('
'.$stub) + ->select('div.yo\.yo') + ->replace_content('grg') + ->to_html, + '
grg
'.$stub, + 'E.class\.0 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' ); +# *[attr] +is( HTML::Zoom->from_html('
'.$stub) + ->select('*[frew]') + ->replace_content('grg') + ->to_html, + '
grg
grg'.$stub, + '*[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' ); + +{ + local $TODO = 'mixed-case attribute names are broken'; + # el[Attr=foo] + is( HTML::Zoom->from_html('
'.$stub) + ->select('div[FreW=yo]') + ->replace_content('grg') + ->to_html, + '
grg
'.$stub, + 'E[attr=val] works with mixed-case attribute names' ); +} + +# el[attr=foo\.bar] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[frew=yo\.yo]') + ->replace_content('grg') + ->to_html, + '
grg
'.$stub, + 'E[attr=foo\.bar] 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' ); + +# el[attr|="foo"] +is( HTML::Zoom->from_html('
'. + '
'.$stub) + ->select('div[lang|="en"]') + ->replace_content('grg') + ->to_html, + '
'. + '
grg
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; +}