Skip to content

Instantly share code, notes, and snippets.

@aereal
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/2fa75027f407a4aa7701 to your computer and use it in GitHub Desktop.
Save aereal/2fa75027f407a4aa7701 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Section::Simple qw( get_data_section );
use HTML::TreeBuilder::Select;
use HTML::Selector::XPath qw( selector_to_xpath );
use Test::More;
my $tree = HTML::TreeBuilder::Select->new();
$tree->parse_content(get_data_section('index.html'));
my $section1 = $tree->select('#section-1');
my $section2 = $tree->select('#section-2');
ok $section1;
ok $section2;
ok $section1->select('.description');
ok ! $section2->select('.description'), '#section-2 以下に .description はない';
subtest 'selector => xpath' => sub {
my $selector = '#section-1 .description';
my $xpath = selector_to_xpath($selector);
unlike $xpath, qr{\A//}, 'ドキュメント全体から探索しないでほしいんだけど……';
};
done_testing;
__DATA__
@@ index.html
<div id="section-1">
<p class="description">hello</p>
</div>
<div id="section-2">
<p>no content</p>
</div>
@aereal
Copy link
Author

aereal commented Aug 15, 2014

sub findnodes            { my( $elt, $path)= @_; return xp->findnodes(            $path, $elt); }

https://github.com/mirod/HTML--TreeBuilder--XPath/blob/d04414e96152a50ac4356c1457027a44db88c317/lib/HTML/TreeBuilder/XPath.pm#L113

XML::XPathEngine#findnodes に $context を渡しているところまでは読んだ。

  • $context に実際にどんな値が与えられているか
  • XML::XPathEngine#findnodes が $context をどのように扱っているか

はまだ見ていない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment