@@ -12,6 +12,7 @@ use URI;
12
12
13
13
use CSS::Inliner::Parser;
14
14
use CSS::Inliner::TreeBuilder;
15
+ use HTML::Entities;
15
16
16
17
=pod
17
18
@@ -42,7 +43,7 @@ sponsoring entity, MailerMailer LLC, has been sold to j2 Global.
42
43
=cut
43
44
44
45
BEGIN {
45
- my $members = [' stylesheet' ,' css' ,' html' ,' html_tree' ,' query' ,' strip_attrs' ,' relaxed' ,' leave_style' ,' warns_as_errors' ,' content_warnings' ,' agent' ,' fixlatin' ];
46
+ my $members = [' stylesheet' ,' css' ,' html' ,' html_tree' ,' query' ,' strip_attrs' ,' relaxed' ,' leave_style' ,' warns_as_errors' ,' content_warnings' ,' agent' ,' fixlatin' , ' encode_entities ' , ' ignore_style_type_attr ' ];
46
47
47
48
# generate all the getter/setter we need
48
49
foreach my $member (@{$members }) {
@@ -77,6 +78,10 @@ leave_style - (optional) Leave style/link tags alone within <head> during inlini
77
78
78
79
relaxed - (optional) Relaxed HTML parsing which will attempt to interpret non-HTML4 documents.
79
80
81
+ encode_entities - (optional) Encode generated inline-styles (in case they contain HTML meta characters)
82
+
83
+ ignore_style_type_attr - (optional) Ignore the deprecated type attribute of "style" tag
84
+
80
85
NOTE: This argument is not compatible with passing an html_tree.
81
86
82
87
agent - (optional) Pass in a string containing a preferred user-agent, overrides the internal default provided by the module for handling remote documents
@@ -112,7 +117,9 @@ sub new {
112
117
leave_style => (defined ($$params {leave_style }) && $$params {leave_style }) ? 1 : 0,
113
118
warns_as_errors => (defined ($$params {warns_as_errors }) && $$params {warns_as_errors }) ? 1 : 0,
114
119
agent => (defined ($$params {agent }) && $$params {agent }) ? $$params {agent } : ' Mozilla/4.0' ,
115
- fixlatin => eval { require Encoding::FixLatin; return 1; } ? 1 : 0
120
+ fixlatin => eval { require Encoding::FixLatin; return 1; } ? 1 : 0,
121
+ encode_entities => (defined ($$params {encode_entities }) && $$params {encode_entities }) ? 1 : 0,
122
+ ignore_style_type_attr => (defined ($$params {ignore_style_type_attr }) && $$params {ignore_style_type_attr }) ? 1 : 0,
116
123
};
117
124
118
125
bless $self , $class ;
@@ -492,15 +499,22 @@ sub inlinify {
492
499
493
500
# styles already inlined have greater precedence
494
501
if (defined ($element -> attr(' style' ))) {
495
- my $cur_style = $self -> _split({ style => $element -> attr(' style' ) });
502
+ my $cur_style = $self -> _split(
503
+ {
504
+ style => $self -> _encode_entities
505
+ ? decode_entities($element -> attr(' style' ))
506
+ : $element -> attr(' style' )
507
+ }
508
+ );
496
509
push @new_style , @$cur_style ;
497
510
push @new_important_style , _grep_important_declarations($cur_style );
498
511
}
499
512
500
513
# override styles with !important styles
501
514
push @new_style , @new_important_style ;
502
515
503
- $element -> attr(' style' , $self -> _expand({ declarations => \@new_style }));
516
+ my $new_style = $self -> _expand({ declarations => \@new_style });
517
+ $element -> attr(' style' , $self -> _encode_entities ? encode_entities($new_style ) : $new_style );
504
518
}
505
519
506
520
# at this point we have a document that contains the expanded inlined stylesheet
@@ -785,7 +799,7 @@ sub __expand_stylesheet {
785
799
# absolutized the assetts within the stylesheet that are relative
786
800
$content =~ s / (url\( )["']?((?:(?!https?:\/\/ )(?!\) )[^"'])*)["']?(?=\) )/ $self ->__fix_relative_url({ prefix => $1 , url => $2 , base => $baseref })/ exsgi ;
787
801
788
- my $stylesheet = HTML::Element-> new(' style' , type => ' text/css' , rel => ' stylesheet ' );
802
+ my $stylesheet = HTML::Element-> new(' style' , $self -> _ignore_style_type_attr ? () : ( type => ' text/css' ) );
789
803
$stylesheet -> push_content($content );
790
804
791
805
$i -> replace_with($stylesheet );
@@ -801,7 +815,7 @@ sub __expand_stylesheet {
801
815
# absolutize the assets within the stylesheet that are relative
802
816
$content =~ s / (url\( )["']?((?:(?!https?:\/\/ )(?!\) )[^"'])*)["']?(?=\) )/ $self ->__fix_relative_url({ prefix => $1 , url => $2 , base => $baseref })/ exsgi ;
803
817
804
- my $stylesheet = HTML::Element-> new(' style' , type => ' text/css' , rel => ' stylesheet ' );
818
+ my $stylesheet = HTML::Element-> new(' style' , $self -> _ignore_style_type_attr ? () : ( type => ' text/css' ) );
805
819
$stylesheet -> push_content($content );
806
820
807
821
$i -> replace_with($stylesheet );
@@ -870,7 +884,7 @@ sub _validate_html {
870
884
871
885
if ($body ) {
872
886
# located spurious <style> tags that won't be handled
873
- my @spurious_style = $body -> look_down(' _tag' ,' style' ,' type' ,' text/css' );
887
+ my @spurious_style = $body -> look_down(' _tag' ,' style' , $self -> _ignore_style_type_attr ? () : ( ' type' ,' text/css' ) );
874
888
875
889
if (scalar @spurious_style ) {
876
890
$self -> _report_warning({ info => ' Unexpected reference to stylesheet within document body skipped' });
@@ -892,7 +906,7 @@ sub _parse_stylesheet {
892
906
my $stylesheet_root = $self -> _relaxed() ? $self -> _html_tree() : $self -> _html_tree-> look_down(' _tag' , ' head' );
893
907
894
908
# get the <style> nodes
895
- my @style = $stylesheet_root -> look_down(' _tag' ,' style' ,' type' ,' text/css' );
909
+ my @style = $stylesheet_root -> look_down(' _tag' ,' style' , $self -> _ignore_style_type_attr ? () : ( ' type' ,' text/css' ) );
896
910
897
911
foreach my $i (@style ) {
898
912
# process this node if the html media type is screen, all or undefined (which defaults to screen)
@@ -930,7 +944,7 @@ sub _collapse_inline_styles {
930
944
if ($i -> attr(' style' )) {
931
945
932
946
# flatten out the styles currently in place on this entity
933
- my $existing_styles = $i -> attr(' style' );
947
+ my $existing_styles = $self -> _encode_entities ? decode_entities( $i -> attr( ' style ' )) : $ i-> attr(' style' );
934
948
$existing_styles =~ tr / \n\t/ / ;
935
949
936
950
# hold the property value pairs
@@ -948,7 +962,7 @@ sub _collapse_inline_styles {
948
962
}
949
963
950
964
$collapsed_style =~ s /\s *$// ;
951
- $i -> attr(' style' , $collapsed_style );
965
+ $i -> attr(' style' , $self -> _encode_entities ? encode_entities( $collapsed_style ) : $ collapsed_style );
952
966
}
953
967
954
968
# if we have specifically asked to remove the inlined attrs, remove them
0 commit comments