##
#!perl
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
{ ### Browser specialization ###################################################
package TweakedBrowser;
use parent 'WWW::Mechanize::Firefox';
sub new {
my ($class, %args) = @_;
my $self = $class->SUPER::new(%args); # call superclass ctor
$self->{wrapped_xpath} = $self->repl->declare(<<'JS');
function(doc, q, ref, type) {
var xpr = doc.evaluate(q, ref, null, type, null);
var r = { resultType: xpr.resultType };
switch(xpr.resultType) {
case XPathResult.NUMBER_TYPE:
r.numberValue = xpr.numberValue;
break;
case XPathResult.STRING_TYPE:
r.stringValue = xpr.stringValue;
break;
case XPathResult.BOOLEAN_TYPE:
r.booleanValue = xpr.booleanValue;
break;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
r.nodeSet = [];
var n;
while (n = xpr.iterateNext()) {
r.nodeSet.push(n);
}
break;
case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
r.nodeSet = [];
for (var i = 0 ; i < xpr.snapshotLength; i++ ) {
r.nodeSet[i] = xpr.snapshotItem(i);
}
break;
case XPathResult.ANY_UNORDERED_NODE_TYPE:
case XPathResult.FIRST_ORDERED_NODE_TYPE:
r.singleNodeValue = xpr.singleNodeValue;
break;
default:
break;
}
return r;
}
JS
$self->{XpathResultTypes} = {
ANY_TYPE => $self->repl->constant('XPathResult.ANY_TYPE'),
NUMBER_TYPE => $self->repl->constant('XPathResult.NUMBER_TYPE'),
STRING_TYPE => $self->repl->constant('XPathResult.STRING_TYPE'),
BOOLEAN_TYPE => $self->repl->constant('XPathResult.BOOLEAN_TYPE'),
UNORDERED_NODE_ITERATOR_TYPE => $self->repl->constant('XPathResult.UNORDERED_NODE_ITERATOR_TYPE'),
ORDERED_NODE_ITERATOR_TYPE => $self->repl->constant('XPathResult.ORDERED_NODE_ITERATOR_TYPE'),
UNORDERED_NODE_SNAPSHOT_TYPE => $self->repl->constant('XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE'),
ORDERED_NODE_SNAPSHOT_TYPE => $self->repl->constant('XPathResult.ORDERED_NODE_SNAPSHOT_TYPE'),
ANY_UNORDERED_TYPE => $self->repl->constant('XPathResult.ANY_UNORDERED_NODE_TYPE'),
FIRST_ORDERED_NODE_TYPE => $self->repl->constant('XPathResult.FIRST_ORDERED_NODE_TYPE'),
};
$self->{XpathResultTypenames} = { };
while(my ($n, $c) = each %{$self->{XpathResultTypes}}) {
$self->{XpathResultTypenames}{$c} = $n;
}
bless($self, $class); # rebless to our class
}
sub xpathResultType { $_[0]->{XpathResultTypenames}{$_[1]}; }
sub xpathResult { $_[0]->{XpathResultTypes}{$_[1]}; }
sub wrapped_xpath {
my ($self, $query, %options) = @_;
if ($options{node}) {
$options{document} ||= $options{node}->{ownerDocument};
#warn "Have node, searching below node";
} else {
$options{document} ||= $self->document;
$options{node} = $options{document};
};
$options{type} ||= $self->{XpathResult}{ANY_TYPE};
$self->{wrapped_xpath}($options{document}, $query, $options{node}, $options{type});
}
}
################################################################################
#my $mech = WWW::Mechanize::Firefox->new(activate => 1);
my $mech = TweakedBrowser->new(activate => 1);
$mech->autoclose_tab(0);
$mech->update_html(<<'HTML');
Hello Firefox!
Hello World!
Hello WWW::Mechanize::Firefox Goob bye
- Item #1
- Item #2
- Item #3
- Item #4
- Item #5
- Item #6
- Item #7
- Item #8
- Item #9
HTML
test($mech, '//p');
test($mech, '//p/text()');
test($mech, 'substring(//p,1,4)'); # expected String: Hell
test($mech, 'string-length(//p)'); # expected Number: 38
test($mech, '//ul[@class="our_items"]');
sub test {
my ($mech, $xpq, %opts) = @_;
test_xpath($mech, $xpq, %opts);
test_wrapped($mech, $xpq, %opts);
print "#" x 80, "\n";
}
sub test_xpath {
my ($mech, $xpq, %opts) = @_;
my @xpr;
eval { @xpr = $mech->xpath($xpq, %opts); };
my %results = (
query => $xpq,
exception => $@,
innerHTML => scalar(@xpr) ? [ map { $_->{innerHTML} } @xpr ] : undef,
textContent => scalar(@xpr) ? [ map { $_->{textContent} } @xpr ] : undef,
nodeValue => scalar(@xpr) ? [ map { $_->{nodeValue} } @xpr ] : undef
);
print Data::Dumper->Dump([\%results], ['results(xpath)']);
}
sub test_wrapped {
my ($mech, $xpq, %opts) = @_;
my $xpr;
eval { $xpr = $mech->wrapped_xpath($xpq, %opts); };
my %results = (
query => $xpq,
exception => $@,
resultType => $xpr->{resultType} . " (" . $mech->xpathResultType($xpr->{resultType}) . ")",
numberValue => ($xpr->{resultType} == $mech->xpathResult('NUMBER_TYPE')) ? $xpr->{numberValue} : undef,
stringValue => ($xpr->{resultType} == $mech->xpathResult('STRING_TYPE')) ? $xpr->{stringValue} : undef,
booleanValue => ($xpr->{resultType} == $mech->xpathResult('BOOLEAN_TYPE')) ? $xpr->{booleanValue} : undef,
);
my @nodes = @{$xpr->{nodeSet}} if $xpr->{nodeSet};
$results{nodeCount} = scalar @nodes;
$results{innerHTML} = scalar(@nodes) ? [ map { $_->{innerHTML} } @nodes ] : undef;
$results{textContent} = scalar(@nodes) ? [ map { $_->{textContent} } @nodes ] : undef;
$results{nodeValue} = scalar(@nodes) ? [ map { $_->{nodeValue} } @nodes ] : undef;
if ($xpr->{singleNodeValue}) {
$results{innerHTML} = $xpr->{innerHTML};
$results{textContent} = $xpr->{textContent};
$results{nodeValue} = $xpr->{nodeValue};
}
print Data::Dumper->Dump([\%results], ['results(wrapped_xpath)']);
}
##
##
#monkbar { display: none; }
h3.other, h3.superdoc, h3.categorized_answer, h3.categorized_question {
font-size: 200%;
font-style: italic;
font-family: Georgia, serif;
padding: 10px;
}
pre, tt {
font-family: "Bitstream Vera Sans Mono", monospace;
}
.topnavmenu, #replies_table font { font-size: 100%; }
textarea {
width: 100%;
height: 25em;
}
body {
color: black;
background-color: rgb(240,240,240); }
a { color: rgb(39,78,144); }
a:link { text-decoration: underline; }
a:hover, a.titlebar:hover { text-decoration: underline; }
a:visited, a.titlebar:visited { text-decoration: none; }
td { color: black; }
tr.titlebar { background-color: rgb(100,135,220); }
td.titlebar { color: white; }
tr.section_title {
color: white;
background-color: rgb(0,51,153); }
td.section_title {
color: white;
background-color: rgb(0,51,153); }
tr.post_head, tr.highlight {
background-color: /*rgb(140,170,230)*/ rgb(212,208,200); }
.code {
border: 1px solid #666;
padding: 10px;
color: rgb(39,78,144);
background-color: white;
display: block;
}
table.nodelet_container { background-color: rgb(0,51,153); }
tbody.nodelet th, th.nodehead {
color: white;
background-color: rgb(0,51,153);
/* color: rgb(39,78,144);
background-color: rgb(140,170,230);*/ }
tbody.nodelet td, td.nodebody {
color: rgb(39,78,144);
background-color: rgb(240,240,240); }
table#replies_table {
background-color: /*rgb(240,240,240)*/transparent;
}
table#reply_tables th a {
background-color: rgb(0,51,153); }
table#replies_table th font {
color: rgb(253,160,91);
}
table#replies_table td[colspan="2"] {
background-color: rgb(212,208,200);
}
table#replies_table td {
background-color: transparent;
}
#approval_nodelet input[type="text"] {
background-color: rgb(255,200,200);
}
a[href="http://pair.com/"] { display: none; }