blib/lib/CSS/Inliner.pm untested

filter: Statementstmt 0.0% 0 / 477 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 159 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 859 SCAR 87.8
CC
80
Cov
0%
_validate_html
156.0
new
110.0
__changelink_relative
90.0
CRAP 6480.0
1 package CSS::Inliner;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '4018';
6  
7 use Carp;
8 use Encode;
9 use LWP::UserAgent;
10 use HTML::Query 'query';
11 use URI;
12  
13 use CSS::Inliner::Parser;
14 use CSS::Inliner::TreeBuilder;
15  
16 =pod
17  
18 =head1 NAME
19  
20 CSS::Inliner - Library for converting CSS <style> blocks to inline styles.
21  
22 =head1 SYNOPSIS
23  
24 use CSS::Inliner;
25  
26 my $inliner = new CSS::Inliner();
27  
28 $inliner->read_file({ filename => 'myfile.html' });
29  
30 print $inliner->inlinify();
31  
32 =head1 DESCRIPTION
33  
34 Library for converting CSS style blocks into inline styles in an HTML
35 document. Specifically this is intended for the ease of generating
36 HTML emails. This is useful as certain email clients don't support top
37 level <style> declarations despite it being 2017.
38  
39 NOTE: The development of the this module is currently suspended as the
40 sponsoring entity, MailerMailer LLC, has been sold to j2 Global.
41  
42 =cut
43  
44 BEGIN {
45 my $members = ['stylesheet','css','html','html_tree','query','strip_attrs','relaxed','leave_style','warns_as_errors','content_warnings','agent','fixlatin'];
46  
47 #generate all the getter/setter we need
48 foreach my $member (@{$members}) {
49 no strict 'refs';
50  
51 *{'_' . $member} = sub {
52 my ($self,$value) = @_;
53  
54 $self->_check_object();
55  
56 $self->{$member} = $value if defined($value);
57  
58 return $self->{$member};
59 }
60 }
61 }
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Instantiates the Inliner object. Sets up class variables that are used
68 during file parsing/processing. Possible options are:
69  
70 html_tree - (optional) Pass in a fresh unparsed instance of HTML::Treebuilder
71  
72 NOTE: Any passed references to HTML::TreeBuilder will be substantially altered by passing it in here...
73  
74 strip_attrs - (optional) Remove all "id" and "class" attributes during inlining
75  
76 leave_style - (optional) Leave style/link tags alone within <head> during inlining
77  
78 relaxed - (optional) Relaxed HTML parsing which will attempt to interpret non-HTML4 documents.
79  
80 NOTE: This argument is not compatible with passing an html_tree.
81  
82 agent - (optional) Pass in a string containing a preferred user-agent, overrides the internal default provided by the module for handling remote documents
83  
84 =cut
85  
86 sub new {
87 my ($proto, $params) = @_;
88  
89 my $class = ref($proto) || $proto;
90  
91 # passed in html_tree argument must be of correct type
92 # TODO: make sure tree has no content already
93 if (defined $$params{html_tree} && $$params{html_tree} && ref $$params{html_tree} ne 'HTML::TreeBuilder') {
94 croak 'Incompatible argument passed to new: "html_tree"';
95 }
96  
97 # check to make sure caller is not trying to pass both an html_tree and setting relaxed flag
98 # relaxed flag requires our own internal TreeBuilder, not HTML::TreeBuilder
99 if (defined $$params{html_tree} && $$params{html_tree} && defined $$params{relaxed} && $$params{relaxed}) {
100 croak 'Incompatible argument passed to new: "html_tree"';
101 }
102  
103 my $self = {
104 stylesheet => undef,
105 css => CSS::Inliner::Parser->new({ warns_as_errors => $$params{warns_as_errors} }),
106 html => undef,
107 html_tree => defined($$params{html_tree}) ? $$params{html_tree} : CSS::Inliner::TreeBuilder->new(),
108 query => undef,
109 content_warnings => {},
110 strip_attrs => (defined($$params{strip_attrs}) && $$params{strip_attrs}) ? 1 : 0,
111 relaxed => (defined($$params{relaxed}) && $$params{relaxed}) ? 1 : 0,
112 leave_style => (defined($$params{leave_style}) && $$params{leave_style}) ? 1 : 0,
113 warns_as_errors => (defined($$params{warns_as_errors}) && $$params{warns_as_errors}) ? 1 : 0,
114 agent => (defined($$params{agent}) && $$params{agent}) ? $$params{agent} : 'Mozilla/4.0',
115 fixlatin => eval { require Encoding::FixLatin; return 1; } ? 1 : 0
116 };
117  
118 bless $self, $class;
119  
120 $self->_configure_tree({ tree => $self->_html_tree });
121  
122 return $self;
123 }
124  
125 =head2 fetch_file
126  
127 Fetches a remote HTML file that supposedly contains both HTML and a
128 style declaration, properly tags the data with the proper charset
129 as provided by the remote webserver (if any). Subsequently calls the
130 read method automatically.
131  
132 This method expands all relative urls, as well as fully expands the
133 stylesheet reference within the document.
134  
135 This method requires you to pass in a params hash that contains a
136 url argument for the requested document. For example:
137  
138 $self->fetch_file({ url => 'http://www.example.com' });
139  
140 Note that you can specify a user-agent to override the default user-agent
141 of 'Mozilla/4.0' within the constructor. Doing so may avoid certain issues
142 with agent filtering related to quirky webserver configs.
143  
144 Input Parameters:
145 url - the desired url for a remote asset presumably containing both html and css
146 charset - (optional) programmer specified charset for the pass url
147  
148 =cut
149  
150 sub fetch_file {
151 my ($self,$params) = @_;
152  
153 $self->_check_object();
154  
155 unless ($params && $$params{url}) {
156 croak 'You must pass in hash params that contain a url argument';
157 }
158  
159 # fetch and retrieve the remote content
160 my ($content,$baseref,$ctcharset) = $self->_fetch_url({ url => $$params{url} });
161  
162 my $charset = $self->detect_charset({ content => $content, charset => $$params{charset}, ctcharset => $ctcharset });
163  
164 my $decoded_html;
165 if ($charset) {
166 $decoded_html = $self->decode_characters({ content => $content, charset => $charset });
167 }
168 else {
169 # no good hints found, do the best we can
170  
171 if ($self->_fixlatin()) {
172 Encoding::FixLatin->import('fix_latin');
173 $decoded_html = fix_latin($content);
174 }
175 else {
176 $decoded_html = $self->decode_characters({ content => $content, charset => 'ascii' });
177 }
178 }
179  
180 my $html = $self->_absolutize_references({ content => $decoded_html, baseref => $baseref });
181  
182 $self->read({ html => $html, charset => $charset });
183  
184 return();
185 }
186  
187 =head2 read_file
188  
189 Opens and reads an HTML file that supposedly contains both HTML and a
190 style declaration, properly tags the data with the proper charset
191 if specified. It subsequently calls the read() method automatically.
192  
193 This method requires you to pass in a params hash that contains a
194 filename argument. For example:
195  
196 $self->read_file({ filename => 'myfile.html' });
197  
198 Additionally you can specify the character encoding within the file, for
199 example:
200  
201 $self->read_file({ filename => 'myfile.html', charset => 'utf8' });
202  
203 Input Parameters:
204 filename - name of local file presumably containing both html and css
205 charset - (optional) programmer specified charset of the passed file
206  
207 =cut
208  
209 sub read_file {
210 my ($self,$params) = @_;
211  
212 $self->_check_object();
213  
214 unless ($params && $$params{filename}) {
215 croak "You must pass in hash params that contain a filename argument";
216 }
217  
218 open FILE, "<", $$params{filename} or die $!;
219 my $content = do { local( $/ ) ; <FILE> };
220  
221 my $charset = $self->detect_charset({ content => $content, charset => $$params{charset} });
222  
223 my $decoded_html;
224 if ($charset) {
225 $decoded_html = $self->decode_characters({ content => $content, charset => $charset });
226 }
227 else {
228 # no good hints found, do the best we can
229  
230 if ($self->_fixlatin()) {
231 Encoding::FixLatin->import('fix_latin');
232 $decoded_html = fix_latin($content);
233 }
234 else {
235 $decoded_html = $self->decode_characters({ content => $content, charset => 'ascii' });
236 }
237 }
238  
239 $self->read({ html => $decoded_html, charset => $charset });
240  
241 return();
242 }
243  
244 =head2 read
245  
246 Reads passed html data and parses it. The intermediate data is stored in
247 class variables.
248  
249 The <style> block is ripped out of the html here, and stored
250 separately. Class/ID/Names used in the markup are left alone.
251  
252 This method requires you to pass in a params hash that contains scalar
253 html data. For example:
254  
255 $self->read({ html => $html });
256  
257 NOTE: You are required to pass a properly encoded perl reference to the
258 html data. This method does *not* do the dirty work of encoding the html
259 as utf8 - do that before calling this method.
260  
261 Input Parameters:
262 html - scalar presumably containing both html and css
263 charset - (optional) scalar representing the original charset of the passed html
264  
265 =cut
266  
267 sub read {
268 my ($self,$params) = @_;
269  
270 $self->_check_object();
271  
272 unless ($params && $$params{html}) {
273 croak 'You must pass in hash params that contains html data';
274 }
275  
276 if ($params && $$params{charset} && !find_encoding($$params{charset})) {
277 croak "Invalid charset passed to read()";
278 }
279  
280 $self->_html_tree->parse_content($$params{html});
281  
282 $self->_init_query();
283  
284 #suck in the styles for later use from the head section - stylesheets anywhere else are invalid
285 my $stylesheet = $self->_parse_stylesheet();
286  
287 #save the data
288 $self->_html($$params{html});
289 $self->_stylesheet($stylesheet);
290  
291 return();
292 }
293  
294 =head2 detect_charset
295  
296 Detect the charset of the passed content.
297  
298 The algorithm present here is roughly based off of the HTML5 W3C working group document,
299 which lays out a recommendation for determining the character set of a received document, which
300 can be seen here under the "determining the character encoding" section:
301 http://www.w3.org/TR/html5/syntax.html
302  
303 NOTE: In the event that no charset can be identified the library will handle the content as a mix of
304 UTF-8/CP-1252/8859-1/ASCII by attempting to use the Encoding::FixLatin module, as this combination
305 is relatively common in the wild. Finally, if Encoding::FixLatin is unavailable the content will be
306 treated as ASCII.
307  
308 Input Parameters:
309 content - scalar presumably containing both html and css
310 charset - (optional) programmer specified charset for the passed content
311 ctcharset - (optional) content-type specified charset for content retrieved via a url
312  
313 =cut
314  
315 sub detect_charset {
316 my ($self,$params) = @_;
317  
318 $self->_check_object();
319  
320 unless ($params && $$params{content}) {
321 croak "You must pass content for content character decoding";
322 }
323  
324 my $charset;
325 if (exists($$params{charset}) && $$params{charset} && find_encoding($$params{charset})) {
326 # precedence given to programmer provided charset
327 $charset = $$params{charset};
328 }
329 elsif (exists($$params{ctcharset}) && $$params{ctcharset} && find_encoding($$params{ctcharset})) {
330 # use the Content-Type charset if available
331 $charset = $$params{ctcharset};
332 }
333 else {
334 # analyze the document to scan for any meta charset hints we can use
335 my $meta_charset = $self->_extract_meta_charset({ content => $$params{content} });
336  
337 if ($meta_charset && find_encoding($meta_charset)) {
338 # use the meta charset from the document if available
339 $charset = $meta_charset;
340 }
341 else {
342 # no hints found...
343 }
344 }
345  
346 return $charset;
347 }
348  
349 =head2 decode_characters
350  
351 Implement the character decoding algorithm for HTML as outlined by the various working groups
352  
353 Basically apply best practices for determining the applied character encoding and properly decode it
354  
355 It is expected that this method will be called before any calls to read()
356  
357 Input Parameters:
358 content - scalar presumably containing both html and css
359 charset - known charset for the passed content
360  
361 =cut
362  
363 sub decode_characters {
364 my ($self,$params) = @_;
365  
366 $self->_check_object();
367  
368 unless ($params && $$params{content}) {
369 croak "You must pass content for content character decoding";
370 }
371  
372 unless ($params && $$params{charset}) {
373 croak "You must pass the charset type of the content to decode";
374 }
375  
376 my $content = $$params{content};
377 my $charset = $$params{charset};
378  
379 my $decoded_html;
380 eval {
381 $decoded_html = decode($charset,$content);
382 };
383  
384 if (!$decoded_html) {
385 croak('Error decoding content with character set "'.$$params{charset}.'"');
386 }
387  
388 return $decoded_html;
389 }
390  
391 =head2 inlinify
392  
393 Processes the html data that was entered through either 'read' or
394 'read_file', returns a scalar that contains a composite chunk of html
395 that has inline styles instead of a top level <style> declaration.
396  
397 =cut
398  
399 sub inlinify {
400 my ($self,$params) = @_;
401  
402 $self->_check_object();
403  
404 $self->_content_warnings({}); # overwrite any existing warnings
405  
406 unless ($self->_html() && $self->_html_tree()) {
407 croak 'You must instantiate and read in your content before inlinifying';
408 }
409  
410 # perform a check to see how bad this html is...
411 $self->_validate_html({ html => $self->_html() });
412  
413 my $html;
414 if (defined $self->_css()) {
415 #parse and store the stylesheet as a hash object
416  
417 $self->_css->read({ css => $self->_stylesheet() });
418  
419 my @css_warnings = @{$self->_css->content_warnings()};
420 foreach my $css_warning (@css_warnings) {
421 $self->_report_warning({ info => $css_warning });
422 }
423  
424 my %matched_elements;
425 my $count = 0;
426  
427 foreach my $entry (@{$self->_css->get_rules()}) {
428 next unless exists $$entry{selector} && $$entry{declarations};
429  
430 my $selector = $$entry{selector};
431 my $declarations = $$entry{declarations};
432  
433 #skip over the following pseudo selectors, these particular ones are not inlineable
434 if ($selector =~ /(?:^|[\w\._\*\]])::?(?:([\w\-]+))\b/io && $1 !~ /first-child|last-child/i) {
435 $self->_report_warning({ info => "The pseudo-class ':$1' cannot be supported inline" });
436 next;
437 }
438  
439 #skip over @import or anything else that might start with @ - not inlineable
440 if ($selector =~ /^\@/io) {
441 $self->_report_warning({ info => "The directive '$selector' cannot be supported inline" });
442 next;
443 }
444  
445 my $query_result;
446  
447 #check to see if query fails, possible for jacked selectors
448 eval {
449 $query_result = $self->query({ selector => $selector });
450 };
451  
452 if ($@) {
453 $self->_report_warning({ info => $@->info() });
454 next;
455 }
456  
457 # CSS rules cascade based on the specificity and order
458 my $specificity = $self->specificity({ selector => $selector });
459  
460 #if an element matched a style within the document store the rule, the specificity
461 #and the actually CSS attributes so we can inline it later
462 foreach my $element (@{$query_result->get_elements()}) {
463  
464 $matched_elements{$element->address()} ||= [];
465 my %match_info = (
466 rule => $selector,
467 element => $element,
468 specificity => $specificity,
469 position => $count,
470 css => $declarations
471 );
472  
473 push(@{$matched_elements{$element->address()}}, \%match_info);
474 $count++;
475 }
476 }
477  
478 #process all elements
479 foreach my $matches (values %matched_elements) {
480 my $element = $matches->[0]->{element};
481 # rules are sorted by specificity, and if there's a tie the position is used
482 # we sort with the lightest items first so that heavier items can override later
483 my @sorted_matches = sort { $a->{specificity} <=> $b->{specificity} || $a->{position} <=> $b->{position} } @$matches;
484  
485 my @new_style;
486 my @new_important_style;
487  
488 foreach my $match (@sorted_matches) {
489 push @new_style, @{$match->{css}};
490 push @new_important_style, _grep_important_declarations($match->{css});
491 }
492  
493 # styles already inlined have greater precedence
494 if (defined($element->attr('style'))) {
495 my $cur_style = $self->_split({ style => $element->attr('style') });
496 push @new_style, @$cur_style;
497 push @new_important_style, _grep_important_declarations($cur_style);
498 }
499  
500 # override styles with !important styles
501 push @new_style, @new_important_style;
502  
503 $element->attr('style', $self->_expand({ declarations => \@new_style }));
504 }
505  
506 #at this point we have a document that contains the expanded inlined stylesheet
507 #BUT we need to collapse the declarations to remove duplicate overridden styles
508 $self->_collapse_inline_styles();
509  
510 # dump out the final processed html for returning to the caller
511 $html = $self->_html_tree->as_HTML('',' ',{});
512 }
513 else {
514 $html = $self->{html};
515 }
516  
517 return $html . "\n";
518 }
519  
520 =head2 query
521  
522 Given a particular selector return back the applicable styles
523  
524 =cut
525  
526 sub query {
527 my ($self,$params) = @_;
528  
529 $self->_check_object();
530  
531 unless ($self->_query()) {
532 $self->_init_query();
533 }
534  
535 return $self->_query->query($$params{selector});
536 }
537  
538 =head2 specificity
539  
540 Given a particular selector return back the associated selectivity
541  
542 =cut
543  
544 sub specificity {
545 my ($self,$params) = @_;
546  
547 $self->_check_object();
548  
549 unless ($self->_query()) {
550 $self->_init_query();
551 }
552  
553 return $self->_query->get_specificity($$params{selector});
554 }
555  
556 =head2 content_warnings
557  
558 Return back any warnings thrown while inlining a given block of content.
559  
560 Note: content warnings are initialized at inlining time, not at read time. In
561 order to receive back content feedback you must perform inlinify first
562  
563 =cut
564  
565 sub content_warnings {
566 my ($self,$params) = @_;
567  
568 $self->_check_object();
569  
570 my @content_warnings = keys %{$self->_content_warnings()};
571  
572 return \@content_warnings;
573 }
574  
575 ####################################################################
576 # #
577 # The following are all private methods and are not for normal use #
578 # I am working to finalize the get/set methods to make them public #
579 # #
580 ####################################################################
581  
582  
583 sub _check_object {
584 my ($self, $params) = @_;
585  
586 unless (ref $self) {
587 croak 'You must instantiate this class in order to properly use it';
588 }
589  
590 return();
591 }
592  
593 sub _report_warning {
594 my ($self,$params) = @_;
595  
596 $self->_check_object();
597  
598 if ($self->_warns_as_errors()) {
599 croak $$params{info};
600 }
601 else {
602 my $warnings = $self->_content_warnings();
603 $$warnings{$$params{info}} = 1;
604 }
605  
606 return();
607 }
608  
609 sub _configure_tree {
610 my ($self,$params) = @_;
611  
612 $self->_check_object();
613  
614 my $tree = $$params{tree};
615  
616 # configure tree
617 $tree->store_comments(1);
618 $tree->attr_encoded(1);
619 $tree->no_expand_entities(1);
620 if ($self->_relaxed()) {
621 $tree->ignore_unknown(0);
622 $tree->implicit_tags(0);
623 }
624  
625 return();
626 }
627  
628 sub _fetch_url {
629 my ($self,$params) = @_;
630  
631 $self->_check_object();
632  
633 # Create a user agent object
634 my $ua = LWP::UserAgent->new;
635  
636 $ua->agent($self->_agent()); # masquerade as Mozilla/4.0 unless otherwise specified in the constructor
637 $ua->protocols_allowed( ['http','https'] );
638  
639 # set URI internal flag such that leading dot edge-case urls work
640 local $URI::ABS_REMOTE_LEADING_DOTS = 1;
641  
642 # Create a request
643 my $uri = URI->new($$params{url});
644  
645 my $req = HTTP::Request->new('GET', $uri, [ 'Accept' => 'text/html, */*' ]);
646  
647 # Pass request to the user agent and get a response back
648 my $res = $ua->request($req);
649  
650 # if not successful
651 if (!$res->is_success()) {
652 croak 'There was an error in fetching the document for ' . $uri . ' : ' . $res->message;
653 }
654  
655 # Is it a HTML document
656 if ($res->content_type ne 'text/html' && $res->content_type ne 'text/css') {
657 croak 'The web site address you entered is not an HTML document.';
658 }
659  
660 # record the content, charset and baseref of the response
661 my $ctcharset = $res->content_type_charset();
662 my $content = $res->content || '';
663 my $baseref = $res->base;
664  
665 return ($content,$baseref,$ctcharset);
666 }
667  
668 sub _absolutize_references {
669 my ($self,$params) = @_;
670  
671 $self->_check_object();
672  
673 # parse the protected document, we need to localize it
674 my $absolutize_tree = new CSS::Inliner::TreeBuilder();
675 $self->_configure_tree({ tree => $absolutize_tree });
676  
677 $absolutize_tree->parse_content($$params{content});
678  
679 # Change relative links to absolute links
680 $self->__changelink_relative({ content => $absolutize_tree->content(), baseref => $$params{baseref} });
681  
682 $self->__expand_stylesheet({ content => $absolutize_tree, html_baseref => $$params{baseref} });
683  
684 # dump out the final processed html for reading
685 my $absolutized_content = $absolutize_tree->as_HTML('',' ',{});
686  
687 return $absolutized_content;
688 }
689  
690 sub __changelink_relative {
691 my ($self,$params) = @_;
692  
693 $self->_check_object();
694  
695 my $base = $$params{baseref};
696  
697 foreach my $i (@{$$params{content}}) {
698  
699 next unless ref $i eq 'HTML::Element';
700  
701 if ($i->tag eq 'img' or $i->tag eq 'frame' or $i->tag eq 'input' or $i->tag eq 'script') {
702  
703 if ($i->attr('src') and $base) {
704 # Construct a uri object for the attribute 'src' value
705 my $uri = URI->new($i->attr('src'));
706 $i->attr('src',$uri->abs($base));
707 } # end 'src' attribute
708 }
709 elsif ($i->tag eq 'form' and $base) {
710 # Construct a new uri for the 'action' attribute value
711 my $uri = URI->new($i->attr('action'));
712 $i->attr('action', $uri->abs($base));
713 }
714 elsif (($i->tag eq 'a' or $i->tag eq 'area' or $i->tag eq 'link') and $i->attr('href') and $i->attr('href') !~ /^\#/) {
715 # Construct a new uri for the 'href' attribute value
716 my $uri = URI->new($i->attr('href'));
717  
718 # Expand URLs to absolute ones if base uri is defined.
719 my $newuri = $base ? $uri->abs($base) : $uri;
720  
721 $i->attr('href', $newuri->as_string());
722 }
723 elsif ($i->tag eq 'td' and $i->attr('background') and $base) {
724 # adjust 'td' background
725 my $uri = URI->new($i->attr('background'));
726 $i->attr('background',$uri->abs($base));
727 } # end tag choices
728  
729 # Recurse down tree
730 if (defined $i->content) {
731 $self->__changelink_relative({ content => $i->content, baseref => $base });
732 }
733 }
734 }
735  
736 sub __fix_relative_url {
737 my ($self,$params) = @_;
738  
739 $self->_check_object();
740  
741 my $uri = URI->new($$params{url});
742  
743 return $$params{prefix} . "'" . $uri->abs($$params{base})->as_string ."'";
744 }
745  
746 sub __expand_stylesheet {
747 my ($self,$params) = @_;
748  
749 $self->_check_object();
750  
751 my $doc = $$params{content};
752  
753 my $stylesheets = ();
754  
755 my (@style,@link);
756 if ($self->_relaxed()) {
757 #get the <style> nodes
758 @style = $doc->look_down('_tag','style');
759  
760 #get the <link> nodes
761 @link = $doc->look_down('_tag','link','href',qr/./);
762 }
763 else {
764 #get the head section of the document
765 my $head = $doc->look_down('_tag', 'head'); # there should only be one
766  
767 #get the <style> nodes underneath the head section - that's the only place stylesheets are allowed to live
768 @style = $head->look_down('_tag','style');
769  
770 #get the <link> nodes underneath the head section - there should be *none* at this step in the process
771 @link = $head->look_down('_tag','link','href',qr/./);
772 }
773  
774 foreach my $i (@link) {
775 # determine attribute values for the link tag, assign some defaults to avoid comparison warnings later
776 my $rel = defined($i->attr('rel')) ? $i->attr('rel') : '';
777 my $type = defined($i->attr('type')) ? $i->attr('type') : '';
778 my $href = defined($i->attr('href')) ? $i->attr('href') : '';
779  
780 # if we don't match the signature of an inlineable stylesheet, skip over
781 next unless (($rel eq 'stylesheet') || ($type eq 'text/css') || ($href =~ m/\.css$/));
782  
783 my ($content,$baseref) = $self->_fetch_url({ url => $i->attr('href') });
784  
785 #absolutized the assetts within the stylesheet that are relative
786 $content =~ s/(url\()["']?((?:(?!https?:\/\/)(?!\))[^"'])*)["']?(?=\))/$self->__fix_relative_url({ prefix => $1, url => $2, base => $baseref })/exsgi;
787  
788 my $stylesheet = HTML::Element->new('style', type => 'text/css', rel=> 'stylesheet');
789 $stylesheet->push_content($content);
790  
791 $i->replace_with($stylesheet);
792 }
793  
794 foreach my $i (@style) {
795 #use the baseref from the original document fetch
796 my $baseref = $$params{html_baseref};
797  
798 my @content = $i->content_list();
799 my $content = join('',grep {! ref $_ } @content);
800  
801 # absolutize the assets within the stylesheet that are relative
802 $content =~ s/(url\()["']?((?:(?!https?:\/\/)(?!\))[^"'])*)["']?(?=\))/$self->__fix_relative_url({ prefix => $1, url => $2, base => $baseref })/exsgi;
803  
804 my $stylesheet = HTML::Element->new('style', type => 'text/css', rel=> 'stylesheet');
805 $stylesheet->push_content($content);
806  
807 $i->replace_with($stylesheet);
808 }
809  
810 return();
811 }
812  
813 sub _validate_html {
814 my ($self,$params) = @_;
815  
816 my $validator_tree = new CSS::Inliner::TreeBuilder();
817  
818 $validator_tree->ignore_unknown(0);
819 $validator_tree->implicit_tags(0);
820 $validator_tree->parse_content($$params{html});
821  
822 if ($self->_relaxed()) {
823 # TODO: print out any html issues that would cause the relaxed parsing to modify the document or might cause
824 # a rendering problem of some type
825 #
826 # Currently there are no known issues, but when found they would go here
827 }
828 else {
829 # The following are inconsistencies that can easily be found by scanning our document using the internal Treebuilder
830 # The standard TreeBuilder actually alters the document in an attempt to create something "valid", but by doing so
831 # all sorts of weird things can happen, so we add them to the warnings report so the caller knows what's going on.
832  
833 # count up the major structural components of the document
834 my @html_nodes = $validator_tree->look_down('_tag', 'html');
835 my @head_nodes = $validator_tree->look_down('_tag', 'head');
836 my @body_nodes = $validator_tree->look_down('_tag', 'body');
837  
838 # we should have exactly 2 root nodes, the treebuilder inserted one and the nested one from the document...
839 if (scalar @html_nodes == 0) {
840 $self->_report_warning({ info => 'Unexpected absence of html root node, force inserted' });
841 }
842 elsif (scalar @html_nodes > 1) {
843 $self->_report_warning({ info => 'Unexpected spurious html root node(s) found within referenced document, coalesced' });
844 }
845  
846 if (scalar @head_nodes > 1) {
847 $self->_report_warning({ info => 'Unexpected spurious head node(s) found within referenced document, coalesced' });
848 }
849  
850 if (scalar @body_nodes > 1) {
851 $self->_report_warning({ info => 'Unexpected spurious body node(s) found within referenced document, coalesced' });
852 }
853  
854 my @link = $validator_tree->look_down('_tag','link','href',qr/./);
855  
856 foreach my $i (@link) {
857 my $rel = defined($i->attr('rel')) && $i->attr('rel') ? $i->attr('rel') : '';
858 my $type = defined($i->attr('type')) && $i->attr('type') ? $i->attr('type') : '';
859 my $href = defined($i->attr('href')) && $i->attr('href') ? $i->attr('href') : '';
860  
861 # link references to stylesheets at this point in the workflow means the caller is doing something improper
862 # currently such references are only chased down if you fetch a document, not if you feed one in
863 if ($rel eq 'stylesheet' || $type eq 'text/css' || $href =~ m/.css$/) {
864 $self->_report_warning({ info => 'Unexpected reference to remote stylesheet was not inlined' });
865 last;
866 }
867 }
868  
869 my $body = $self->_html_tree->look_down('_tag', 'body');
870  
871 if ($body) {
872 # located spurious <style> tags that won't be handled
873 my @spurious_style = $body->look_down('_tag','style','type','text/css');
874  
875 if (scalar @spurious_style) {
876 $self->_report_warning({ info => 'Unexpected reference to stylesheet within document body skipped' });
877 }
878 }
879 }
880  
881 return();
882 }
883  
884 sub _parse_stylesheet {
885 my ($self,$params) = @_;
886  
887 $self->_check_object();
888  
889 my $stylesheet = '';
890  
891 # figure out where to look for styles
892 my $stylesheet_root = $self->_relaxed() ? $self->_html_tree() : $self->_html_tree->look_down('_tag', 'head');
893  
894 # get the <style> nodes
895 my @style = $stylesheet_root->look_down('_tag','style','type','text/css');
896  
897 foreach my $i (@style) {
898 #process this node if the html media type is screen, all or undefined (which defaults to screen)
899 if (($i->tag eq 'style') && (!$i->attr('media') || $i->attr('media') =~ m/\b(all|screen)\b/)) {
900  
901 foreach my $item ($i->content_list()) {
902 # remove HTML comment markers
903 $item =~ s/<!--//mg;
904 $item =~ s/-->//mg;
905  
906 $stylesheet .= $item;
907 }
908 }
909  
910 unless ($self->_leave_style()) {
911 $i->delete();
912 }
913 }
914  
915 return $stylesheet;
916 }
917  
918 sub _collapse_inline_styles {
919 my ($self,$params) = @_;
920  
921 $self->_check_object();
922  
923 #check if we were passed a node to recurse from, otherwise use the root of the tree
924 my $content = exists($$params{content}) ? $$params{content} : [$self->_html_tree()];
925  
926 foreach my $i (@{$content}) {
927  
928 next unless (ref $i && $i->isa('HTML::Element'));
929  
930 if ($i->attr('style')) {
931  
932 #flatten out the styles currently in place on this entity
933 my $existing_styles = $i->attr('style');
934 $existing_styles =~ tr/\n\t/ /;
935  
936 # hold the property value pairs
937 my $styles = $self->_split({ style => $existing_styles });
938  
939 my $collapsed_style = '';
940  
941 # loop in reverse order as the latter rule wins
942 my %exist;
943 for (my $i = $#$styles; $i > 0; $i -= 2) {
944 my $property = $styles->[$i-1];
945 my $value = $styles->[$i];
946 next if $exist{$property}++; # the former rule with the same name is overridden
947 $collapsed_style = "$property: $value; " . $collapsed_style;
948 }
949  
950 $collapsed_style =~ s/\s*$//;
951 $i->attr('style', $collapsed_style);
952 }
953  
954 #if we have specifically asked to remove the inlined attrs, remove them
955 if ($self->_strip_attrs()) {
956 $i->attr('id',undef);
957 $i->attr('class',undef);
958 }
959  
960 # Recurse down tree
961 if (defined $i->content) {
962 $self->_collapse_inline_styles({ content => $i->content() });
963 }
964 }
965 }
966  
967 sub _extract_meta_charset {
968 my ($self,$params) = @_;
969  
970 $self->_check_object();
971  
972 # we are going to parse an html document as ascii that is not necessarily ascii - silence the warning
973 local $SIG{__WARN__} = sub { my $warning = shift; warn $warning unless $warning =~ /^Parsing of undecoded UTF-8/ };
974  
975 # parse document and pull out key header elements
976 my $extract_tree = new CSS::Inliner::TreeBuilder();
977 $self->_configure_tree({ tree => $extract_tree });
978  
979 $extract_tree->parse_content($$params{content});
980  
981 my $head = $extract_tree->look_down("_tag", "head"); # there should only be one
982  
983 my $meta_charset;
984 if ($head) {
985 # pull key header meta elements
986 my $meta_charset_elem = $head->look_down('_tag','meta','charset',qr/./);
987 my $meta_equiv_charset_elem = $head->look_down('_tag','meta','http-equiv',qr/content-type/i,'content',qr/./);
988  
989 # assign meta charset, we give precedence to meta http_equiv content type
990 if ($meta_equiv_charset_elem) {
991 my $meta_equiv_content = $meta_equiv_charset_elem->attr('content');
992  
993 # leverage charset allowable chars from https://tools.ietf.org/html/rfc2978
994 if ($meta_equiv_content =~ /charset(?:\s*)=(?:\s*)([\w!#$%&'\-+^`{}~]+)/i) {
995 $meta_charset = find_encoding($1);
996 }
997 }
998  
999 if (!defined($meta_charset) && $meta_charset_elem) {
1000 $meta_charset = find_encoding($meta_charset_elem->attr('charset'));
1001 }
1002 }
1003  
1004 return $meta_charset;
1005 }
1006  
1007 sub _init_query {
1008 my ($self,$params) = @_;
1009  
1010 $self->_check_object();
1011  
1012 $self->{query} = HTML::Query->new($self->_html_tree());
1013  
1014 return();
1015 }
1016  
1017 sub _expand {
1018 my ($self, $params) = @_;
1019  
1020 $self->_check_object();
1021  
1022 my $declarations = $$params{declarations};
1023 my $inline = '';
1024 for ( my $i = 0; $i < @$declarations; $i += 2 ) {
1025 my $property = $declarations->[$i];
1026 my $value = $declarations->[$i+1];
1027 $inline .= "$property:$value;";
1028 }
1029  
1030 return $inline;
1031 }
1032  
1033 sub _split {
1034 my ($self, $params) = @_;
1035  
1036 $self->_check_object();
1037  
1038 my $style = $params->{style};
1039 my @split;
1040  
1041 # Split into properties/values
1042 foreach ( grep { /\S/ } split /\;/, $style ) {
1043 unless ( /^\s*([\w._-]+)\s*:\s*(.*?)\s*$/ ) {
1044 $self->_report_warning({ info => "Invalid or unexpected property '$_' in style '$style'" });
1045 }
1046 push @split, lc $1, $2;
1047 }
1048  
1049 return \@split;
1050 }
1051  
1052 sub _grep_important_declarations {
1053 my ($declarations) = @_;
1054  
1055 my @important;
1056 for ( my $i = 0; $i < @$declarations; $i += 2 ) {
1057 my $property = $declarations->[$i];
1058 my $value = $declarations->[$i+1];
1059 if ($value =~ /!\s*important\s*$/i) {
1060 push @important, $property, $value;
1061 }
1062 }
1063  
1064 return @important;
1065 }
1066  
1067 1;
1068  
1069 =head1 AUTHOR
1070  
1071 Kevin Kamel <kamelkev@underprint.com>
1072  
1073 =head1 CONTRIBUTORS
1074  
1075 Dave Gray <cpan@doesntsuck.com>
1076 Vivek Khera <vivek@khera.org>
1077 Michael Peters <wonko@cpan.org>
1078 Chelsea Rio <chelseario@gmail.com>
1079  
1080 =head1 LICENSE
1081  
1082 This module is Copyright 2017 Khera Communications, Inc. It is
1083 licensed under the same terms as Perl itself.
1084  
1085 =cut