http://www.perlmonks.org?node_id=1033940


in reply to Re^3: Mojo::URL returns incorrect absolute path
in thread Mojo::URL returns incorrect absolute path

#!/usr/bin/perl -- use strict; use warnings; use Mojo::DOM; use Mojo::URL; my $dom = Mojo::DOM->new( '<div> <base href="http://example.com/foo/page"> <a href="/bar/">/bar/</a> <a href="baz/biz">baz/biz</a> <a href="#burp">#burp bug </a> </div>' ); #~ $dom->at('base[href]')->each( ## grr $dom->find('base[href]')->each( sub { my( $base , $count ) = @_; $base = $base->attrs('href'); if(length $base ){ $dom->find('a[href]')->each( sub { my ( $link, $count ) = @_; my $href = $link->attrs('href'); $href = Mojo::URL->new($href)->base( Mojo::URL->new($base) ) +->to_abs; #~ $href and $link->attrs(href => $href ); $href and $link->attrs( href => "$href" ); return; }, ); } }, ); for my $l ( $dom->find('a[href]')->each ) { print $l->attrs('href'), "\n"; } __END__ http://example.com/bar/ http://example.com/foo/baz/biz http://example.com/foo/page#burp