Controller/SimpleCAS/Role/TextTranscode.pm

filter: Statementstmt 17.1% 48 / 280 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 45.4% 15 / 33 Podpod 100% 15 / 15 totaltotal 14.0% 78 / 557 CC 95 SCAR 43.4
CC
95
Cov
10%
convert_from_mhtml
210.0
normaliaze_rich_content
156.0
parse_html_get_styles
110.0
CRAP 76.4
↑ Summary Controller/SimpleCAS.pm »
1 package Catalyst::Controller::SimpleCAS::Role::TextTranscode;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use MooseX::MethodAttributes::Role 0.29;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 requires qw(Content fetch_content); # <-- methods of Catalyst::Controller::SimpleCAS
8  
9 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::Encoding 'encoding_from_html_document', 'encoding_from_byte_order_mark';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Email::MIME::CreateHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Catalyst::Controller::SimpleCAS::CSS::Simple; #<-- hack/workaround CSS::Simple busted on CPAN
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use String::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Catalyst::Controller::SimpleCAS::MimeUriResolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # FIXME - This is old and broken - file long gone from RapidApp ...
22 1 my $ISOLATE_CSS_RULE = ''; #'@import "/static/rapidapp/css/CssIsolation.css";';
23  
24 # Backend action for Ext.ux.RapidApp.Plugin.HtmlEditor.LoadHtmlFile
25 0 ▶sub transcode_html :Chained('base') :PathPart('texttranscode/transcode_html') {
Subroutine transcode_html: not called CC=6 SCAR=37.4
Pod documented
26 0 my ($self, $c) = @_;
27
28 0 ▶ my $upload = $c->req->upload('Filedata') or die "no upload object";
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $c->req->upload("Filedata") or die "no upload object" 0 0 0
29  
30 0 my $src_text = $self->normaliaze_rich_content($c,$upload,$upload->filename);
31
32 0 my $rct= $c->stash->{requestContentType};
33 0 ▶ if ($rct eq 'JSON' || $rct eq 'text/x-rapidapp-form-response') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rct eq "JSON" or $rct eq "text/x-rapidapp-form-response") 0 0 0
Condition0%0/3
$rct eq "JSON" or $rct eq "text/x-rapidapp-form-response"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rct eq "JSON" or $rct eq "text/x-rapidapp-form-response"
$rct eq "JSON" $rct eq "text/x-rapidapp-form-response"
Truth table0%0/3
A or B
A= $rct eq "JSON"B= $rct eq "text/x-rapidapp-form-response"
ABresult
1X1
011
000
34 0 $c->stash->{json}= { success => \1, content => $src_text };
35 0 return $c->forward('View::RapidApp::JSON');
36 }
37
38 # find out what encoding the user wants, defaulting to utf8
39 0 ▶ my $dest_encoding= ($c->req->params->{dest_encoding} || 'utf-8');
Condition0%0/2
$c->req->params->{'dest_encoding'} || 'utf-8'
l!l
00
MC/DC0%0/1
$c->req->params->{'dest_encoding'} || 'utf-8'
$c->req->params->{'dest_encoding'}
Truth table0%0/2
A
A= $c->req->params->{'dest_encoding'}
Aresult
11
00
40 0 ▶ my $out_codec= Encode::find_encoding($dest_encoding) or die "Unsupported encoding: $dest_encoding";
Branch: 1 branch
BranchTrueFalseTotal
my $out_codec = &Encode::find_encoding($dest_encoding) or die "Unsupported encoding: $dest_encoding" 0 0 0
41 0 my $dest_octets= $out_codec->encode($src_text);
42
43 # we need to set the charset here so that catalyst doesn't try to convert it further
44 0 $c->res->content_type('text/html; charset='.$dest_encoding);
45 0 return $c->res->body($dest_octets);
46 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Backend action for Ext.ux.RapidApp.Plugin.HtmlEditor.SaveMhtml
49 0 ▶sub generate_mhtml_download :Chained('base') :PathPart('texttranscode/generate_mhtml_download') {
Subroutine generate_mhtml_download: not called CC=4 SCAR=30.0
Pod documented
50 0 my ($self, $c) = @_;
51 0 ▶ die "No html content supplied" unless ($c->req->params->{html_enc});
Branch: 1 branch
BranchTrueFalseTotal
unless $c->req->params->{"html_enc"} 0 0 0
52 0 my $html = decode_json($c->req->params->{html_enc})->{data};
53  
54 # 'filename' param is optional and probably not supplied
55 0 $html = $self->normaliaze_rich_content($c,$html,$c->req->params->{filename});
56
57 0 ▶ my $filename = $self->get_strip_orig_filename(\$html) || 'content.mht';
Condition0%0/2
$self->get_strip_orig_filename(\$html) || 'content.mht'
l!l
00
MC/DC0%0/1
$self->get_strip_orig_filename(\$html) || 'content.mht'
$self->get_strip_orig_filename(\$html)
Truth table0%0/2
A
A= $self->get_strip_orig_filename(\$html)
Aresult
11
00
58 0 $filename =~ s/\"/\'/g; #<-- convert any " characters
59 0 my $disposition = 'attachment;filename="' . $filename . '"';
60
61 0 my $MIME = $self->html_to_mhtml($c,$html);
62  
63 0 $c->response->header( $_ => $MIME->header($_) ) for ($MIME->header_names);
64 0 $c->response->header('Content-Disposition' => $disposition);
65 0 return $c->res->body( $MIME->as_string );
66 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68  
69 # extracts filename previously embedded by normaliaze_rich_content in html comment
70 0 ▶sub get_strip_orig_filename {
Subroutine get_strip_orig_filename: not called CC=4 SCAR=30.0
Pod documented
71 0 my $self = shift;
72 0 my $htmlref = shift;
73 0 ▶ return undef unless (ref $htmlref);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $htmlref 0 0 0
74
75 0 $$htmlref =~ /(\/\*----ORIGINAL_FILENAME:(.+)----\*\/)/;
76 0 ▶ my $comment = $1 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $comment = $1 or return undef 0 0 0
77 0 ▶ my $filename = $2 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $2 or return undef 0 0 0
78
79 # strip comment:
80 0 $$htmlref =~ s/\Q${comment}\E//;
81
82 0 return $filename;
83 }
84  
85 0 ▶sub html_to_mhtml {
Subroutine html_to_mhtml: not called CC=6 SCAR=37.4
Pod documented
86 0 my $self = shift;
87 0 my $c = shift;
88 0 my $html = shift;
89
90 0 my $style = $self->parse_html_get_styles(\$html,1);
91
92 0 ▶ if($style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style) 0 0 0
93
94 # FIXME - this is broken:
95 # strip isolate css import rule:
96 0 $style =~ s/\Q$ISOLATE_CSS_RULE\E//g;
97
98 0 my $Css = Catalyst::Controller::SimpleCAS::CSS::Simple->new;
99 0 $Css->read({ css => $style });
100
101 #scream_color(BLACK.ON_RED,$Css->get_selectors);
102
103 # undo the cas selector wrap applied during Load Html:
104 0 foreach my $selector ($Css->get_selectors) {
105 0 my $new_selector = $selector;
106 0 ▶ if($selector =~ /^\#cas\-selector\-wrap\-\w+$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($selector =~ /^\#cas\-selector\-wrap\-\w+$/) { } 0 0 0
107 0 $new_selector = 'body';
108 }
109 else {
110 0 my @parts = split(/\s+/,$selector);
111 0 my $first = shift @parts;
112 0 ▶ next unless ($first =~ /^\#cas\-selector\-wrap\-/);
Branch: 1 branch
BranchTrueFalseTotal
unless $first =~ /^\#cas\-selector\-wrap\-/ 0 0 0
113 0 $new_selector = join(' ',@parts);
114 }
115 0 $Css->modify_selector({
116 selector => $selector,
117 new_selector => $new_selector
118 });
119 }
120
121 0 $style = $Css->write;
122 }
123
124 # TODO/FIXME: remove RapidApp/TT dependency/entanglement
125 0 $html = $c->template_render('templates/rapidapp/xhtml_document.tt',{
126 style => $style,
127 body => $html
128 });
129
130 0 my $UriResolver = Catalyst::Controller::SimpleCAS::MimeUriResolver->new({
131 Cas => $self,
132 base => ''
133 });
134
135 0 my $MIME = Email::MIME->create_html(
136 header => [],
137 body_attributes => { charset => 'UTF-8', encoding => 'quoted-printable' },
138 body => encode('UTF-8', $html),
139 resolver => $UriResolver
140 );
141
142 # Force wrap in a multipart/related
143 0 ▶ return Email::MIME->create(
Branch: 1 branch
BranchTrueFalseTotal
unless $MIME->subparts 0 0 0
144 attributes => {
145 content_type => "multipart/related",
146 disposition => "attachment"
147 },
148 parts => [ $MIME ]
149 ) unless ($MIME->subparts);
150
151 0 return $MIME;
152 }
153  
154  
155 0 ▶sub normaliaze_rich_content {
Subroutine normaliaze_rich_content: not called CC=12 SCAR=50.5
Pod documented
156 0 my $self = shift;
157 0 my $c = shift;
158 0 my $src_octets = shift;
159 0 my $filename = shift;
160
161 0 my $upload;
162 0 ▶ if(ref($src_octets)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src_octets) 0 0 0
163 0 $upload = $src_octets;
164 0 $src_octets = $upload->slurp;
165 }
166
167 0 my $content;
168
169 # Try to determine what text encoding the file content came from, and then detect if it
170 # is MIME or HTML.
171 #
172 # Note that if the content came from a file upload/post an encode/decode phase happened
173 # during the HTTP transfer of this file, but it should have been taken care of by Catalyst
174 # and now we have the original file on disk in its native 8-bit encoding.
175  
176 # If MIME (MTHML):
177 0 ▶ my $MIME = try{
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 # This will frequently produce uninitialized value warnings from Email::Simple::Header,
179 # and I haven't been able to figure out how to stop it
180 0 Email::MIME->new($src_octets)
181 0 };
182 0 ▶ if($MIME && $MIME->subparts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MIME and $MIME->subparts) { } 0 0 0
Condition0%0/3
$MIME and $MIME->subparts
!ll&&!rl&&r
000
MC/DC0%0/2
$MIME and $MIME->subparts
$MIME $MIME->subparts
Truth table0%0/3
A and B
A= $MIMEB= $MIME->subparts
ABresult
0X0
100
111
183 0 $content = $self->convert_from_mhtml($c,$MIME);
184 }
185 # If HTML or binary:
186 else {
187 0 ▶ if(!$upload || $upload->type =~ /^text/){
Branch: 1 branch
BranchTrueFalseTotal
if (not $upload or $upload->type =~ /^text/) { } 0 0 0
Condition0%0/3
not $upload or $upload->type =~ /^text/
l!l&&r!l&&!r
000
MC/DC0%0/2
not $upload or $upload->type =~ /^text/
not $upload $upload->type =~ /^text/
Truth table0%0/3
not(A) or B
A= not $uploadB= $upload->type =~ /^text/
ABresult
1X1
011
000
188 0 ▶ my $src_encoding= encoding_from_html_document($src_octets) || 'utf-8';
Condition0%0/2
encoding_from_html_document($src_octets) || 'utf-8'
l!l
00
MC/DC0%0/1
encoding_from_html_document($src_octets) || 'utf-8'
encoding_from_html_document($src_octets)
Truth table0%0/2
A
A= encoding_from_html_document($src_octets)
Aresult
11
00
189 0 ▶ my $in_codec= Encode::find_encoding($src_encoding) or die "Unsupported encoding: $src_encoding";
Branch: 1 branch
BranchTrueFalseTotal
my $in_codec = &Encode::find_encoding($src_encoding) or die "Unsupported encoding: $src_encoding" 0 0 0
190 0 ▶ $content = (utf8::is_utf8($src_octets)) ? $src_octets : $in_codec->decode($src_octets);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($src_octets) ? : 0 0 0
191 }
192 # Binary
193 else {
194 0 ▶ my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content";
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content" 0 0 0
195 0 my $Content = $self->Content($checksum,$upload->filename);
196 0 ▶ return $Content->imglink if ($Content->imglink);
Branch: 1 branch
BranchTrueFalseTotal
if $Content->imglink 0 0 0
197 0 return $Content->filelink;
198 }
199 }
200 # TODO: Detect other content types and add fallback logic
201
202 0 $content = $self->parse_html_get_style_body(\$content);
203 0 $self->convert_data_uri_scheme_links($c,\$content);
204
205 # Use style tags just as a safe place to store the original filename
206 # (switched to this after having issues with html comments)
207 0 ▶ $content = '<style>/*----ORIGINAL_FILENAME:' .
Branch: 1 branch
BranchTrueFalseTotal
if $filename 0 0 0
208 $filename .
209 '----*/</style>' . "\n" . $content if ($filename);
210  
211 0 return $content;
212 }
213  
214  
215 0 ▶sub convert_from_mhtml {
Subroutine convert_from_mhtml: not called CC=14 SCAR=53.5
Pod documented
216 0 my $self = shift;
217 0 my $c = shift;
218 0 my $MIME = shift;
219  
220 0 ▶ my ($SubPart) = $MIME->subparts or return;
Branch: 1 branch
BranchTrueFalseTotal
my($SubPart) = $MIME->subparts or return 0 0 0
221
222 ## -- Check for and remove extra outer MIME wrapper (exists in actual MIME EMails):
223 0 ▶ $MIME = $SubPart if (
Branch: 1 branch
BranchTrueFalseTotal
if $SubPart->content_type and $SubPart->content_type =~ m[multipart/related] 0 0 0
Condition0%0/3
$SubPart->content_type and $SubPart->content_type =~ m[multipart/related]
!ll&&!rl&&r
000
MC/DC0%0/2
$SubPart->content_type and $SubPart->content_type =~ m[multipart/related]
$SubPart->content_type $SubPart->content_type =~ m[multipart/related]
Truth table0%0/3
A and B
A= $SubPart->content_typeB= $SubPart->content_type =~ m[multipart/related]
ABresult
0X0
100
111
224 $SubPart->content_type &&
225 $SubPart->content_type =~ /multipart\/related/
226 );
227 ## --
228
229 0 ▶ my ($MainPart) = $MIME->subparts or return;
Branch: 1 branch
BranchTrueFalseTotal
my($MainPart) = $MIME->subparts or return 0 0 0
230  
231 ## ------
232 ## New: throw the kitchen sink at trying to figure out the charset/encoding
233 ##
234 ## This solves the long-standing problem where MHT files saved by Word 2010
235 ## would load garbled. These files are encoded as 'UTF-16LE', and the system
236 ## is not able to realize this out of the box (I think because it lists the
237 ## the charset ambiguously as ' charset="unicode" ' in the Content-Type
238 ## MIME header, but I'm no expert on Unicode). Below we're basically trying
239 ## all of the functions of HTML::Encoding until we find one that gives us
240 ## an answer, and if we do get an answer, we apply it to the MIME object before
241 ## calling ->body_str() which will then use it to decode to text.
242 ##
243 0 my $decoded = $MainPart->body; # <-- decodes from base64 (or whatever) to *bytes*
244  
245 0 ▶ my $char_set =
Condition0%0/21
HTML::Encoding::encoding_from_html_document($decoded) || HTML::Encoding::encoding_from_byte_order_mark($decoded) || HTML::Encoding::encoding_from_meta_element($decoded) || HTML::Encoding::xml_declaration_from_octets($decoded) || HTML::Encoding::encoding_from_first_chars($decoded) || HTML::Encoding::encoding_from_xml_declaration($decoded) || HTML::Encoding::encoding_from_content_type($decoded) || HTML::Encoding::encoding_from_xml_document($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded) or HTML::Encoding::encoding_from_meta_element($decoded) or HTML::Encoding::xml_declaration_from_octets($decoded) or HTML::Encoding::encoding_from_first_chars($decoded) or HTML::Encoding::encoding_from_xml_declaration($decoded) or HTML::Encoding::encoding_from_content_type($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded) or HTML::Encoding::encoding_from_meta_element($decoded) or HTML::Encoding::xml_declaration_from_octets($decoded) or HTML::Encoding::encoding_from_first_chars($decoded) or HTML::Encoding::encoding_from_xml_declaration($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded) or HTML::Encoding::encoding_from_meta_element($decoded) or HTML::Encoding::xml_declaration_from_octets($decoded) or HTML::Encoding::encoding_from_first_chars($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded) or HTML::Encoding::encoding_from_meta_element($decoded) or HTML::Encoding::xml_declaration_from_octets($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded) or HTML::Encoding::encoding_from_meta_element($decoded)
l!l&&r!l&&!r
000
HTML::Encoding::encoding_from_html_document($decoded) or HTML::Encoding::encoding_from_byte_order_mark($decoded)
l!l&&r!l&&!r
000
MC/DC0%0/8
HTML::Encoding::encoding_from_html_document($decoded) || HTML::Encoding::encoding_from_byte_order_mark($decoded) || HTML::Encoding::encoding_from_meta_element($decoded) || HTML::Encoding::xml_declaration_from_octets($decoded) || HTML::Encoding::encoding_from_first_chars($decoded) || HTML::Encoding::encoding_from_xml_declaration($decoded) || HTML::Encoding::encoding_from_content_type($decoded) || HTML::Encoding::encoding_from_xml_document($decoded)
HTML::Encoding::encoding_from_html_document($decoded) HTML::Encoding::encoding_from_byte_order_mark($decoded) HTML::Encoding::encoding_from_meta_element($decoded) HTML::Encoding::xml_declaration_from_octets($decoded) HTML::Encoding::encoding_from_first_chars($decoded) HTML::Encoding::encoding_from_xml_declaration($decoded) HTML::Encoding::encoding_from_content_type($decoded) HTML::Encoding::encoding_from_xml_document($decoded)
Truth table0%0/9
A or B or C or D or E or F or G || H
A= HTML::Encoding::encoding_from_html_document($decoded)B= HTML::Encoding::encoding_from_byte_order_mark($decoded)C= HTML::Encoding::encoding_from_meta_element($decoded)D= HTML::Encoding::xml_declaration_from_octets($decoded)E= HTML::Encoding::encoding_from_first_chars($decoded)F= HTML::Encoding::encoding_from_xml_declaration($decoded)G= HTML::Encoding::encoding_from_content_type($decoded)H= HTML::Encoding::encoding_from_xml_document($decoded)
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
246 HTML::Encoding::encoding_from_html_document ($decoded) ||
247 HTML::Encoding::encoding_from_byte_order_mark ($decoded) ||
248 HTML::Encoding::encoding_from_meta_element ($decoded) ||
249 HTML::Encoding::xml_declaration_from_octets ($decoded) ||
250 HTML::Encoding::encoding_from_first_chars ($decoded) ||
251 HTML::Encoding::encoding_from_xml_declaration ($decoded) ||
252 HTML::Encoding::encoding_from_content_type ($decoded) ||
253 HTML::Encoding::encoding_from_xml_document ($decoded);
254  
255 0 ▶ $MainPart->charset_set( $char_set ) if ($char_set);
Branch: 1 branch
BranchTrueFalseTotal
if $char_set 0 0 0
256 ## ------
257  
258 0 my $html = $MainPart->body_str; # <-- decodes to text using the character_set
259  
260 0 ▶ my $base_path = $self->parse_html_base_href(\$html) || $self->get_mime_part_base_path($MainPart);
Condition0%0/3
$self->parse_html_base_href(\$html) || $self->get_mime_part_base_path($MainPart)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->parse_html_base_href(\$html) || $self->get_mime_part_base_path($MainPart)
$self->parse_html_base_href(\$html) $self->get_mime_part_base_path($MainPart)
Truth table0%0/3
A || B
A= $self->parse_html_base_href(\$html)B= $self->get_mime_part_base_path($MainPart)
ABresult
1X1
011
000
261
262 0 my %ndx = ();
263 0 ▶ $MIME->walk_parts(sub{
Subroutine __ANON__: not called CC=6 SCAR=37.4
264 0 my $Part = shift;
265 0 ▶ return if ($Part == $MIME || $Part == $MainPart); #<-- ignore the outer and main/body parts
Branch: 1 branch
BranchTrueFalseTotal
if $Part == $MIME or $Part == $MainPart 0 0 0
Condition0%0/3
$Part == $MIME or $Part == $MainPart
l!l&&r!l&&!r
000
MC/DC0%0/2
$Part == $MIME or $Part == $MainPart
$Part == $MIME $Part == $MainPart
Truth table0%0/3
A or B
A= $Part == $MIMEB= $Part == $MainPart
ABresult
1X1
011
000
266
267 0 my $content_id = $Part->header('Content-ID');
268 0 ▶ if ($content_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_id) 0 0 0
269 0 $ndx{'cid:' . $content_id} = $Part;
270 0 $content_id =~ s/^\<//;
271 0 $content_id =~ s/\>$//;
272 0 $ndx{'cid:' . $content_id} = $Part;
273 }
274
275 0 my $content_location = $Part->header('Content-Location');
276 0 ▶ if($content_location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_location) 0 0 0
277 0 $ndx{$content_location} = $Part;
278 0 ▶ if($base_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_path) 0 0 0
279 0 $content_location =~ s/^\Q$base_path\E//;
280 0 $ndx{$content_location} = $Part;
281 }
282 }
283 0 });
284
285 0 $self->convert_mhtml_links_parts($c,\$html,\%ndx);
286 0 return $html;
287 }
288  
289 # Try to extract the 'body' from html to prevent causing DOM/parsing issues on the client side
290 0 ▶sub parse_html_get_style_body {
Subroutine parse_html_get_style_body: not called CC=8 SCAR=42.8
Pod documented
291 0 my $self = shift;
292 0 my $htmlref = shift;
293
294 0 ▶ my $body = $self->parse_html_get_body($htmlref) or return $$htmlref;
Branch: 1 branch
BranchTrueFalseTotal
my $body = $self->parse_html_get_body($htmlref) or return $$htmlref 0 0 0
295 0 my $style = $self->parse_html_get_styles($htmlref);
296
297 0 my $auto_css_pre = 'cas-selector-wrap-';
298 0 my $auto_css_id = $auto_css_pre . String::Random->new->randregex('[a-z0-9]{8}');
299
300 0 ▶ if($style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style) 0 0 0
301 0 my $Css = Catalyst::Controller::SimpleCAS::CSS::Simple->new;
302 0 $Css->read({ css => $style });
303
304 #scream_color(BLACK.ON_RED,$Css->get_selectors);
305
306 0 foreach my $selector ($Css->get_selectors) {
307 0 my @parts = split(/\s+/,$selector);
308 # strip selector wrap from previous content processing (when the user imports +
309 # exports + imports multiple times)
310 0 ▶ shift @parts if ($parts[0] =~ /^\#${auto_css_pre}/);
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] =~ /^\#$auto_css_pre/ 0 0 0
311 0 unshift @parts, '#' . $auto_css_id;
312 0 ▶ pop @parts if (lc($selector) eq 'body'); #<-- any 'body' selectors are replaced by the new div wrap below
Branch: 1 branch
BranchTrueFalseTotal
if lc $selector eq "body" 0 0 0
313
314 0 $Css->modify_selector({
315 selector => $selector,
316 new_selector => join(' ',@parts)
317 });
318 }
319
320 0 $style = $Css->write;
321 }
322  
323 0 ▶ if ($style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style) 0 0 0
324 # minify:
325 0 $style =~ s/\r?\n/ /gm;
326 0 $style =~ s/\s+/ /gm;
327 0 $style = "\n<style type=\"text/css\">\n$style\n</style>";
328 }
329
330 0 ▶ $style ||= '';
Condition0%0/2
$style ||= ""
l!l
00
MC/DC0%0/1
$style ||= ""
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
331 0 $style = "\n" . '<style type="text/css">' . "\n" .
332 " $ISOLATE_CSS_RULE\n" .
333 '</style>' . $style . "\n";
334  
335 0 return '<div class="isolate" id="' . $auto_css_id . '">' . "\n" .
336 $body . "\n" .
337 '</div>' . "\n$style";
338 }
339  
340  
341 # Try to extract the 'body' from html to prevent causing DOM/parsing issues on the client side
342 # Also strip html comments
343 0 ▶sub parse_html_get_body {
Subroutine parse_html_get_body: not called CC=8 SCAR=42.8
Pod documented
344 0 my $self = shift;
345 0 my $htmlref = shift;
346 0 my $parser = HTML::TokeParser::Simple->new($htmlref);
347 0 my $in_body = 0;
348 0 my $inner = '';
349 0 ▶ while (my $tag = $parser->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_token) 0 0 0
350 0 ▶ last if ($in_body && $tag->is_end_tag('body'));
Branch: 1 branch
BranchTrueFalseTotal
if $in_body and $tag->is_end_tag("body") 0 0 0
Condition0%0/3
$in_body and $tag->is_end_tag("body")
!ll&&!rl&&r
000
MC/DC0%0/2
$in_body and $tag->is_end_tag("body")
$in_body $tag->is_end_tag("body")
Truth table0%0/3
A and B
A= $in_bodyB= $tag->is_end_tag("body")
ABresult
0X0
100
111
351 0 ▶ $inner .= $tag->as_is if ($in_body && !$tag->is_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $in_body and not $tag->is_comment 0 0 0
Condition0%0/3
$in_body and not $tag->is_comment
!ll&&!rl&&r
000
MC/DC0%0/2
$in_body and not $tag->is_comment
$in_body not $tag->is_comment
Truth table0%0/3
A and not(B)
A= $in_bodyB= not $tag->is_comment
ABresult
0X0
100
111
352 0 ▶ $in_body = 1 if ($tag->is_start_tag('body'));
Branch: 1 branch
BranchTrueFalseTotal
if $tag->is_start_tag("body") 0 0 0
353 };
354 0 ▶ return undef if ($inner eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $inner eq "" 0 0 0
355 0 return $inner;
356 }
357  
358 0 ▶sub parse_html_get_styles {
Subroutine parse_html_get_styles: not called CC=10 SCAR=47.0
Pod documented
359 0 my $self = shift;
360 0 my $htmlref = shift;
361 0 my $strip = shift;
362 0 my $parser = HTML::TokeParser::Simple->new($htmlref);
363 0 my $in_style = 0;
364 0 my $styles = '';
365 0 my $newhtml = '';
366 0 ▶ while (my $tag = $parser->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_token) 0 0 0
367 0 ▶ if ($tag->is_end_tag('style')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->is_end_tag("style")) 0 0 0
368 0 $in_style = 0;
369 0 next;
370 }
371 0 ▶ $styles .= $tag->as_is and next if ($in_style);
Branch: 1 branch
BranchTrueFalseTotal
if $in_style 0 0 0
Condition0%0/5
$in_style && (($styles .= $tag->as_is) && (next))
!ll&&!rl&&r
000
$styles .= $tag->as_is and next
!ll
00
MC/DC0%0/2
$in_style && (($styles .= $tag->as_is) && (next))
$in_style $styles .= $tag->as_is
Truth table0%0/3
A && B
A= $in_styleB= $styles .= $tag->as_is
ABresult
0X0
100
111
372 0 ▶ if ($tag->is_start_tag('style')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->is_start_tag("style")) 0 0 0
373 0 $in_style = 1;
374 0 next;
375 }
376 0 ▶ $newhtml .= $tag->as_is if($strip && !$tag->is_tag('style'));
Branch: 1 branch
BranchTrueFalseTotal
if $strip and not $tag->is_tag("style") 0 0 0
Condition0%0/3
$strip and not $tag->is_tag("style")
!ll&&!rl&&r
000
MC/DC0%0/2
$strip and not $tag->is_tag("style")
$strip not $tag->is_tag("style")
Truth table0%0/3
A and not(B)
A= $stripB= not $tag->is_tag("style")
ABresult
0X0
100
111
377 };
378 0 ▶ return undef if ($styles eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $styles eq "" 0 0 0
379
380 0 ▶ $$htmlref = $newhtml if ($strip);
Branch: 1 branch
BranchTrueFalseTotal
if $strip 0 0 0
381
382 # Pull out html comment characters, ignored in css, but can interfere with CSS::Simple (rare cases)
383 0 $styles =~ s/\<\!\-\-//g;
384 0 $styles =~ s/\-\-\>//g;
385
386 0 return $styles;
387 }
388  
389  
390  
391 # Extracts the base file path from the 'base' tag of the MHTML content
392 0 ▶sub parse_html_base_href {
Subroutine parse_html_base_href: not called CC=4 SCAR=30.0
Pod documented
393 0 my $self = shift;
394 0 my $htmlref = shift;
395 0 my $parser = HTML::TokeParser::Simple->new($htmlref);
396 0 ▶ while (my $tag = $parser->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag) 0 0 0
397 0 ▶ if($tag->is_tag('base')){
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->is_tag("base")) 0 0 0
398 0 ▶ my $url = $tag->get_attr('href') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $tag->get_attr("href") or next 0 0 0
399 0 return $url;
400 }
401 };
402 0 return undef;
403 }
404  
405 # alternative method to identify a base path from a Mime Part
406 0 ▶sub get_mime_part_base_path {
Subroutine get_mime_part_base_path: not called CC=2 SCAR=17.9
Pod documented
407 0 my $self = shift;
408 0 my $Part = shift;
409
410 0 ▶ my $content_location = $Part->header('Content-Location') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $content_location = $Part->header("Content-Location") or return undef 0 0 0
411 0 my @parts = split(/\//,$content_location);
412 0 my $filename = pop @parts;
413 0 my $path = join('/',@parts) . '/';
414
415 0 return $path;
416 }
417  
418  
419 0 ▶sub convert_mhtml_links_parts {
Subroutine convert_mhtml_links_parts: not called CC=9 SCAR=45.0
Pod documented
420 0 my $self = shift;
421 0 my $c = shift;
422 0 my $htmlref = shift;
423 0 my $part_ndx = shift;
424
425 0 ▶ die "convert_mhtml_links_parts(): Invalid arguments!!" unless (ref $part_ndx eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $part_ndx eq "HASH" 0 0 0
426
427 0 my $parser = HTML::TokeParser::Simple->new($htmlref);
428
429 0 my $substitutions = {};
430
431 0 ▶ while (my $tag = $parser->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag) 0 0 0
432 0 ▶ next if($tag->is_tag('base')); #<-- skip the 'base' tag which we parsed earlier
Branch: 1 branch
BranchTrueFalseTotal
if $tag->is_tag("base") 0 0 0
433 0 for my $attr (qw(src href)){
434 0 ▶ my $url = $tag->get_attr($attr) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $tag->get_attr($attr) or next 0 0 0
435 0 ▶ my $Part = $part_ndx->{$url} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $Part = $part_ndx->{$url} or next 0 0 0
436 0 ▶ my $cas_url = $self->mime_part_to_cas_url($c,$Part) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cas_url = $self->mime_part_to_cas_url($c, $Part) or next 0 0 0
437
438 0 my $as_is = $tag->as_is;
439 0 $tag->set_attr( $attr => $cas_url );
440 0 $substitutions->{$as_is} = $tag->as_is;
441 }
442 }
443
444 0 foreach my $find (keys %$substitutions) {
445 0 my $replace = $substitutions->{$find};
446 0 $$htmlref =~ s/\Q$find\E/$replace/gm;
447 }
448 }
449  
450  
451  
452 # See http://en.wikipedia.org/wiki/Data_URI_scheme
453 0 ▶sub convert_data_uri_scheme_links {
Subroutine convert_data_uri_scheme_links: not called CC=8 SCAR=42.8
Pod documented
454 0 my $self = shift;
455 0 my $c = shift;
456 0 my $htmlref = shift;
457
458 0 my $parser = HTML::TokeParser::Simple->new($htmlref);
459
460 0 my $substitutions = {};
461
462 0 ▶ while (my $tag = $parser->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag) 0 0 0
463
464 0 my $attr;
465 0 ▶ if($tag->is_tag('img')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag->is_tag('img')) { } 0 0 0
elsif ($tag->is_tag('a')) { } 0 0 0
466 0 $attr = 'src';
467 }
468 elsif($tag->is_tag('a')) {
469 0 $attr = 'href';
470 }
471 else {
472 0 next;
473 }
474
475 0 ▶ my $url = $tag->get_attr($attr) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $tag->get_attr($attr) or next 0 0 0
476
477 # Support the special case where the src value is literal base64 data:
478 0 ▶ if ($url =~ /^data:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^data:/) 0 0 0
479 0 my $newurl = $self->embedded_src_data_to_url($c,$url);
480 0 ▶ $substitutions->{$url} = $newurl if ($newurl);
Branch: 1 branch
BranchTrueFalseTotal
if $newurl 0 0 0
481 }
482 }
483
484 0 foreach my $find (keys %$substitutions) {
485 0 my $replace = $substitutions->{$find};
486 0 $$htmlref =~ s/\Q$find\E/$replace/gm;
487 }
488 }
489  
490 0 ▶sub embedded_src_data_to_url {
Subroutine embedded_src_data_to_url: not called CC=5 SCAR=34.0
Pod documented
491 0 my $self = shift;
492 0 my $c = shift;
493 0 my $url = shift;
494
495 0 my ($pre,$content_type,$encoding,$base64_data) = split(/[\:\;\,]/,$url);
496
497 # we only know how to handle base64 currently:
498 0 ▶ return undef unless (lc($encoding) eq 'base64');
Branch: 1 branch
BranchTrueFalseTotal
unless lc $encoding eq "base64" 0 0 0
499
500 0 ▶ my $checksum = try{$self->Store->add_content_base64($base64_data)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
501 0 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = &try(sub { $self->Store->add_content_base64($base64_data); } ) or return undef 0 0 0
502
503 # This is RapidApp-specific
504 0 ▶ my $pfx = $c->can('mount_url') ? $c->mount_url || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->can('mount_url') ? : 0 0 0
Condition0%0/2
$c->mount_url or ""
l!l
00
MC/DC0%0/1
$c->mount_url or ""
$c->mount_url
Truth table0%0/2
A
A= $c->mount_url
Aresult
11
00
505
506 0 return join('/',$pfx,
507 $self->action_namespace($c),
508 'fetch_content', $checksum
509 );
510 }
511  
512 0 ▶sub mime_part_to_cas_url {
Subroutine mime_part_to_cas_url: not called CC=4 SCAR=30.0
Pod documented
513 0 my $self = shift;
514 0 my $c = shift;
515 0 my $Part = shift;
516
517 0 my $data = $Part->body;
518 0 my $filename = $Part->filename(1);
519 0 ▶ my $checksum = $self->Store->add_content($data) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content($data) or return undef 0 0 0
520
521 # This is RapidApp-specific
522 0 ▶ my $pfx = $c->can('mount_url') ? $c->mount_url || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->can('mount_url') ? : 0 0 0
Condition0%0/2
$c->mount_url or ""
l!l
00
MC/DC0%0/1
$c->mount_url or ""
$c->mount_url
Truth table0%0/2
A
A= $c->mount_url
Aresult
11
00
523
524 0 return join('/',$pfx,
525 $self->action_namespace($c),
526 'fetch_content', $checksum, $filename
527 );
528 }
529  
530 1 1;
531  
532 __END__
↑ Summary Controller/SimpleCAS.pm »