Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/WWW/Mechanize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3500,7 +3500,11 @@ sub _link_from_token {
my $text;
my $name;
if ( $tag eq 'a' ) {
$text = $parser->get_trimmed_text("/$tag");
# Only get text content if this is not a self-closing tag
# Self-closing tags (e.g., <a name="anchor"/>) have a '/' key in attrs
if ( !$attrs->{'/'} ) {
$text = $parser->get_trimmed_text("/$tag");
}
$text = q{} unless defined $text;

my $onClick = $attrs->{onclick};
Expand Down
11 changes: 11 additions & 0 deletions t/anchor_name_bug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Test for anchor name bug</title>
</head>
<body>
<h1>hello world</h1>
<a name="anchor"/>
<p><a href="http://www.url1.com/gi1?a=1">test1</a></p>
<p><a href="http://www.url2.com/gi2?a=2">test2</a></p>
</body>
</html>
45 changes: 45 additions & 0 deletions t/anchor_name_bug.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!perl

use warnings;
use strict;

use Test::More;
use URI::file ();

BEGIN {
use_ok('WWW::Mechanize');
}

# Test for issue #15: Mechanize seemed to discard the first URL after
# an <a name="anchor"/> tag in a html page.
# See: http://code.google.com/p/www-mechanize/issues/detail?id=15

my $mech = WWW::Mechanize->new( cookie_jar => undef, max_redirect => 0 );
isa_ok( $mech, 'WWW::Mechanize' );

my $uri = URI::file->new_abs('t/anchor_name_bug.html')->as_string;

$mech->get($uri);
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};

# The bug was that the first link after <a name="anchor"/> was being discarded
my @links = $mech->find_all_links();

# We should find exactly 2 links (test1 and test2), NOT just 1
is( scalar(@links), 2, 'Should find 2 links, not just 1' );

# Verify first link is test1
my $link1 = $mech->find_link( text => 'test1' );
isa_ok( $link1, 'WWW::Mechanize::Link', 'First link (test1) should exist' );
is( $link1->url, 'http://www.url1.com/gi1?a=1', 'First link URL is correct' );

# Verify second link is test2
my $link2 = $mech->find_link( text => 'test2' );
isa_ok( $link2, 'WWW::Mechanize::Link', 'Second link (test2) should exist' );
is( $link2->url, 'http://www.url2.com/gi2?a=2', 'Second link URL is correct' );

# Verify links are in correct order
is( $links[0]->url, 'http://www.url1.com/gi1?a=1', 'First link in order' );
is( $links[1]->url, 'http://www.url2.com/gi2?a=2', 'Second link in order' );

done_testing();