CSS-Inliner
view release on metacpan or search on metacpan
lib/CSS/Inliner.pm view on Meta::CPAN
unless (ref $self) {
croak 'You must instantiate this class in order to properly use it';
}
return();
}
sub _report_warning {
my ($self,$params) = @_;
$self->_check_object();
if ($self->_warns_as_errors()) {
croak $$params{info};
}
else {
my $warnings = $self->_content_warnings();
$$warnings{$$params{info}} = 1;
}
return();
}
sub _configure_tree {
my ($self,$params) = @_;
$self->_check_object();
my $tree = $$params{tree};
# configure tree
$tree->store_comments(1);
$tree->attr_encoded(1);
$tree->no_expand_entities(1);
if ($self->_relaxed()) {
$tree->ignore_unknown(0);
$tree->implicit_tags(0);
}
return();
}
sub _fetch_url {
my ($self,$params) = @_;
$self->_check_object();
# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent($self->_agent()); # masquerade as Mozilla/4.0 unless otherwise specified in the constructor
$ua->protocols_allowed( ['http','https'] );
# set URI internal flag such that leading dot edge-case urls work
local $URI::ABS_REMOTE_LEADING_DOTS = 1;
# Create a request
my $uri = URI->new($$params{url});
my $req = HTTP::Request->new('GET', $uri, [ 'Accept' => 'text/html, */*' ]);
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# if not successful
if (!$res->is_success()) {
croak 'There was an error in fetching the document for ' . $uri . ' : ' . $res->message;
}
# Is it a HTML document
if ($res->content_type ne 'text/html' && $res->content_type ne 'text/css') {
croak 'The web site address you entered is not an HTML document.';
}
# record the content, charset and baseref of the response
my $ctcharset = $res->content_type_charset();
my $content = $res->content || '';
my $baseref = $res->base;
return ($content,$baseref,$ctcharset);
}
sub _absolutize_references {
my ($self,$params) = @_;
$self->_check_object();
# parse the protected document, we need to localize it
my $absolutize_tree = new CSS::Inliner::TreeBuilder();
$self->_configure_tree({ tree => $absolutize_tree });
$absolutize_tree->parse_content($$params{content});
# Change relative links to absolute links
$self->__changelink_relative({ content => $absolutize_tree->content(), baseref => $$params{baseref} });
$self->__expand_stylesheet({ content => $absolutize_tree, html_baseref => $$params{baseref} });
# dump out the final processed html for reading
my $absolutized_content = $absolutize_tree->as_HTML('',' ',{});
return $absolutized_content;
}
sub __changelink_relative {
my ($self,$params) = @_;
$self->_check_object();
my $base = $$params{baseref};
foreach my $i (@{$$params{content}}) {
next unless ref $i eq 'HTML::Element';
if ($i->tag eq 'img' or $i->tag eq 'frame' or $i->tag eq 'input' or $i->tag eq 'script') {
if ($i->attr('src') and $base) {
# Construct a uri object for the attribute 'src' value
my $uri = URI->new($i->attr('src'));
( run in 1.670 second using v1.01-cache-2.11-cpan-e1769b4cff6 )