Labyrinth/MLUtils.pm

filter: Statementstmt 74.1% 278 / 375 Branchbran 55.1% 118 / 214 Conditioncond 56.0% 28 / 50 MC/DCmcdc 34.3% 11 / 32 Subroutinesub 76.1% 51 / 67 Podpod 100% 41 / 41 totaltotal 67.6% 527 / 779 CC 133 SCAR 25.8
CC
133
Cov
66%
cleanup_tag
67.8
LinkTitles
56.0
cleanup_attr_style
42.0
CRAP 13.2
« Labyrinth/DIUtils.pm ↑ Summary Labyrinth/Plugins.pm »
1 package Labyrinth::MLUtils;
2  
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 $VERSION = '5.32';
9  
10 =head1 NAME
11  
12 Labyrinth::MLUtils - Markup Language Utilities for Labyrinth.
13  
14 =head1 SYNOPSIS
15  
16 use Labyrinth::MLUtils;
17  
18 =cut
19  
20 # -------------------------------------
21 # Export Details
22  
23 14 require Exporter;
24 14 @ISA = qw(Exporter);
25 14 %EXPORT_TAGS = ( 'all' => [ qw(
26 LegalTag LegalTags CleanTags
27 CleanHTML SafeHTML CleanLink CleanWords LinkTitles
28 DropDownList DropDownListText
29 DropDownRows DropDownRowsText
30 DropDownMultiList DropDownMultiRows
31 ErrorText ErrorSymbol
32 LinkSpam
33  
34 create_inline_styles
35 demoroniser
36 process_html escape_html
37 ) ] );
38  
39 14 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
40 14 @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
41  
42 # -------------------------------------
43 # Library Modules
44  
45 14 ▶use Encode::ZapCP1252;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 14 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 14 ▶use Regexp::Common qw /profanity/;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 14 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 14 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # -------------------------------------
53 # Variables
54  
55 14 my $DEFAULTTAGS = 'p,a,br,b,strong,center,hr,ol,ul,li,i,img,u,em,strike,h1,h2,h3,h4,h5,h6,table,thead,tr,th,tbody,td,sup,address,pre';
56 14 my ($HTMLTAGS,%HTMLTAGS);
57  
58 # -------------------------------------
59 # The Public Interface Subs
60  
61 =head1 FUNCTIONS
62  
63 =head2 HTML Tag handling
64  
65 =over 4
66  
67 =item LegalTag
68  
69 Returns TRUE or FALSE as to whether the given HTML tag is accepted by the
70 system.
71  
72 =item LegalTags
73  
74 Returns the list of HTML tags that are accepted by the system.
75  
76 =item CleanTags
77  
78 For a given text string, attempts to clean the use of any HTML tags. Any HTML
79 tags found that are not accepted by the system are encoded into HTML entities.
80  
81 =item CleanHTML
82  
83 For a given text string, removes all existence of any HTML tag. Mostly used in
84 input text box cleaning.
85  
86 =item SafeHTML
87  
88 For a given text string, encodes all HTML tags to HTML entities. Mostly used in
89 input textarea edit preparation.
90  
91 =item CleanLink
92  
93 Attempts to remove known spam style links.
94  
95 =item CleanWords
96  
97 Attempts to remove known profanity words.
98  
99 =item LinkTitles
100  
101 Given a XHTML snippet, will look for basic links and add title attributes.
102 Titles are of rhe format 'External Site: $domain', where $domain is the domain
103 used in the link.
104  
105 =back
106  
107 =cut
108  
109 2 ▶sub LegalTag {
Subroutine LegalTag: called CC=2 SCAR=6.9
Pod documented
110 2 my $tag = lc shift;
111  
112 2 my %tags = _buildtags();
113 2 ▶ return 1 if($tags{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $tags{$tag} 1 1 2
114 1 return 0;
115 }
116  
117 1 ▶sub LegalTags {
Subroutine LegalTags: called CC=1 SCAR=0.0
Pod documented
118 1 my %tags = _buildtags();
119 1 my $tags = join(", ", sort keys %tags);
120 1 $tags =~ s/, ([^,]+)$/ and $1/;
121 1 return $tags;
122 }
123  
124 6 ▶sub CleanTags {
Subroutine CleanTags: called CC=4 SCAR=14.0
Pod documented
125 6 my $text = shift;
126 6 ▶ return '' unless($text);
Branch: 1 branch
BranchTrueFalseTotal
unless $text 2 4 6
127  
128 4 $text =~ s!</?(span|tbody)[^>]*>!!sig;
129 4 $text =~ s!<(br|hr)>!<$1 />!sig;
130 4 $text =~ s!<p>(?:\s|&nbsp;)+(?:</p>)?<(table|p|ul|ol|div|pre)!<$1!sig;
131 4 $text =~ s!\s+&\s+! &amp; !sg;
132 4 $text =~ s!&[lr]squo;!&quot;!mg;
133 4 $text =~ s{&(?!\#\d+;|[a-z0-9]+;)}{&amp;}sig;
134  
135 # decode TinyMCE encodings
136 4 $text =~ s!&lt;(.*?)&gt;!<$1>!sig;
137  
138 # clean paragraphs
139 4 $text =~ s!</p>\s+<p>!</p><p>!sig;
140 4 $text =~ s!\s*<br /><br />\s*!</p><p>!sig;
141  
142 4 my %tags = _buildtags();
143 4 my @found = ($text =~ m!</?(\w+)(?:\s+[^>]*)?>!gm);
144 4 for my $tag (@found) {
145 10 $tag = lc $tag;
146 10 ▶ next if($tags{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $tags{$tag} 10 0 10
147  
148 0 $text =~ s!<(/?$tag(?:[^>]*)?)>!&lt;$1&gt;!igm;
149 0 $tags{$tag} = 1;
150 }
151  
152 4 process_html($text,0,1);
153 }
154  
155 4 ▶sub CleanHTML {
Subroutine CleanHTML: called CC=2 SCAR=6.9
Pod documented
156 4 my $text = shift;
157 4 ▶ return '' unless($text);
Branch: 1 branch
BranchTrueFalseTotal
unless $text 2 2 4
158  
159 2 $text =~ s!<[^>]+>!!gm; # remove any tags
160 2 $text =~ s!\s{2,}! !mg;
161 2 $text =~ s!&[lr]squo;!&quot;!mg;
162 2 $text =~ s{&(?!\#\d+;|[a-z0-9]+;)}{&amp;}sig;
163  
164 2 process_html($text,0,0);
165 }
166  
167 3 ▶sub SafeHTML {
Subroutine SafeHTML: called CC=2 SCAR=6.9
Pod documented
168 3 my $text = shift;
169 3 ▶ return '' unless($text);
Branch: 1 branch
BranchTrueFalseTotal
unless $text 2 1 3
170  
171 1 $text =~ s!<!&lt;!gm;
172 1 $text =~ s!>!&gt;!gm;
173 1 $text =~ s!\s+&\s+! &amp; !mg;
174 1 $text =~ s!&[lr]squo;!&quot;!mg;
175 1 $text =~ s{&(?!\#\d+;|[a-z0-9]+;)}{&amp;}sig;
176  
177 1 process_html($text,0,0);
178 }
179  
180 3 ▶sub CleanLink {
Subroutine CleanLink: called CC=2 SCAR=6.9
Pod documented
181 3 my $text = shift;
182 3 ▶ return '' unless($text);
Branch: 1 branch
BranchTrueFalseTotal
unless $text 2 1 3
183  
184 # remove embedded script tags
185 1 $text =~ s!<script.*?/script>!!gis; # open and close script tags
186 1 $text =~ s!<script.*!!gis; # open, but no close, remove to the end of string
187 1 $text =~ s!.*/script>!!gis; # close, but on open, removed from te beginning of string
188  
189 # remove anything that looks like a link
190 1 $text =~ s!https?://[^\s]*!!gis;
191 1 $text =~ s!<a.*?/a>!!gis;
192 1 $text =~ s!\[url.*?url\]!!gis;
193 1 $text =~ s!\[link.*?link\]!!gis;
194 # $text =~ s!$settings{urlregex}!!gis;
195  
196 1 CleanTags($text);
197 }
198  
199 0 ▶sub CleanWords {
Subroutine CleanWords: not called CC=1 SCAR=6.9
Pod documented
200 0 my $text = shift;
201  
202 0 $text =~ s/$RE{profanity}//gis;
203 0 my $filter = join("|", map {$_->[1]} $dbi->GetQuery('array','AllBadWords'));
204 0 $text =~ s/$filter//gis;
205  
206 0 return $text;
207 }
208  
209 0 ▶sub LinkTitles {
Subroutine LinkTitles: not called CC=7 SCAR=40.3
Pod documented
210 0 my $text = shift;
211  
212 0 for my $href ($text =~ m!(<a href="https?://[^/"]+(?:/[^"]*)?">)!g) {
213 0 my ($link1,$path,$link2) = ($href =~ m!(<a href=")((?:https?://|/)?([^/"]+)(?:/[^"]*)?)">!);
214 0 $href =~ s!([\\\?\+\-\.()\[\]])!\\$1!sig;
215  
216 0 my $title;
217 0 ▶ $title ||= $settings{pathmap}{$path} if($settings{pathmap}{$path});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::MLUtils::settings{"pathmap"}{$path} 0 0 0
Condition0%0/3
$title ||= $Labyrinth::MLUtils::settings{"pathmap"}{$path}
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= $Labyrinth::MLUtils::settings{"pathmap"}{$path}
$title $Labyrinth::MLUtils::settings{"pathmap"}{$path}
Truth table0%0/3
A ||= B
A= $titleB= $Labyrinth::MLUtils::settings{"pathmap"}{$path}
ABresult
1X1
011
000
218 0 ▶ $title ||= $settings{titlemap}{$link2} if($settings{titlemap}{$link2});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::MLUtils::settings{"titlemap"}{$link2} 0 0 0
Condition0%0/3
$title ||= $Labyrinth::MLUtils::settings{"titlemap"}{$link2}
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= $Labyrinth::MLUtils::settings{"titlemap"}{$link2}
$title $Labyrinth::MLUtils::settings{"titlemap"}{$link2}
Truth table0%0/3
A ||= B
A= $titleB= $Labyrinth::MLUtils::settings{"titlemap"}{$link2}
ABresult
1X1
011
000
219 0 ▶ $title ||= "External Site: $link2";
Condition0%0/2
$title ||= "External Site: $link2"
l!l
00
MC/DC0%0/1
$title ||= "External Site: $link2"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
220 0 $text =~ s!$href!$link1$path" title="$title">!sgi;
221 }
222  
223 0 return $text;
224 }
225  
226 7 ▶sub _buildtags {
Subroutine _buildtags: called CC=5 SCAR=17.5
227 7 ▶ return %HTMLTAGS if(%HTMLTAGS);
Branch: 1 branch
BranchTrueFalseTotal
if %HTMLTAGS 6 1 7
228  
229 1 ▶ if(defined $settings{htmltags} && $settings{htmltags} =~ /^\+(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Labyrinth::MLUtils::settings{'htmltags'} and $Labyrinth::MLUtils::settings{'htmltags'} =~ /^\+(.*)/) { } 0 1 1
elsif (not $Labyrinth::MLUtils::settings{'htmltags'}) { } 1 0 1
Condition33%1/3
defined $Labyrinth::MLUtils::settings{"htmltags"} and $Labyrinth::MLUtils::settings{"htmltags"} =~ /^\+(.*)/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $Labyrinth::MLUtils::settings{"htmltags"} and $Labyrinth::MLUtils::settings{"htmltags"} =~ /^\+(.*)/
defined $Labyrinth::MLUtils::settings{"htmltags"} $Labyrinth::MLUtils::settings{"htmltags"} =~ /^\+(.*)/
Truth table33%1/3
A and B
A= defined $Labyrinth::MLUtils::settings{"htmltags"}B= $Labyrinth::MLUtils::settings{"htmltags"} =~ /^\+(.*)/
ABresult
0X0
100
111
230 0 $settings{htmltags} = $1 . ',' . $DEFAULTTAGS;
231 } elsif(!$settings{htmltags}) {
232 1 $settings{htmltags} = $DEFAULTTAGS;
233 }
234  
235 1 %HTMLTAGS = map {$_ => 1} split(",",$settings{htmltags});
236 1 return %HTMLTAGS;
237 }
238  
239 =head2 Drop Down Boxes
240  
241 =over 4
242  
243 =item DropDownList($opt,$name,@items)
244  
245 Returns a dropdown selection box given a list of numbers. Can optionally pass
246 a option value to be pre-selected. The name of the form element is used as
247 both the element name and id.
248  
249 =item DropDownListText($opt,$name,@items)
250  
251 Returns a dropdown selection box given a list of strings. Can optionally pass
252 a option value to be pre-selected. The name of the form element is used as
253 both the element name and id.
254  
255 =item DropDownRows($opt,$name,$index,$value,@items)
256  
257 Returns a dropdown selection box given a list of rows. Can optionally pass
258 a option value to be pre-selected. The name of the form element is used as
259 both the element name and id. The 'index' and 'value' refence the field names
260 within each row hash.
261  
262 =item DropDownRowsText($opt,$name,$index,$value,@items)
263  
264 Returns a dropdown selection box given a list of strings. Can optionally pass
265 a option value to be pre-selected. The name of the form element is used as
266 both the element name and id. The 'index' and 'value' refence the field names
267 within each row hash.
268  
269 =item DropDownMultiList($opts,$name,$count,@items)
270  
271 Returns a dropdown multi-selection box given a list of strings. The name of the
272 form element is used as both the element name and id. The default number of
273 rows visible is 5, but this can be changed by providing a value for 'count'.
274  
275 Can optionally pass an option value to be pre-selected. The option can be a
276 comma separated list (as a single string) of values or an arrayref to a list
277 of values.
278  
279 =item DropDownMultiRows($opts,$name,$index,$value,$count,@items)
280  
281 Returns a dropdown multi-selection box given a list of rows. The name of the
282 form element is used as both the element name and id. The default number of
283 rows visible is 5, but this can be changed by providing a value for 'count'.
284 The 'index' and 'value' refence the field names within each row hash.
285  
286 Can optionally pass an option value to be pre-selected. The option can be a
287 comma separated list (as a single string) of values or an arrayref to a list
288 of values.
289  
290 =back
291  
292 =cut
293  
294 1 ▶sub DropDownList {
Subroutine DropDownList: called CC=5 SCAR=17.2
Pod documented
295 1 my ($opt,$name,@items) = @_;
296 1 ▶ $opt = undef if(defined $opt && $opt !~ /^\d+$/); # opt must be a number
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt and not $opt =~ /^\d+$/ 0 1 1
Condition33%1/3
defined $opt and not $opt =~ /^\d+$/
!ll&&!rl&&r
010
MC/DC0%0/2
defined $opt and not $opt =~ /^\d+$/
defined $opt not $opt =~ /^\d+$/
Truth table33%1/3
A and not(B)
A= defined $optB= not $opt =~ /^\d+$/
ABresult
0X0
100
111
297  
298 return qq|<select id="$name" name="$name">| .
299 1 ▶ join("",(map { qq|<option value="$_"|.
Branch: 1 branch
BranchTrueFalseTotal
defined $opt && $opt == $_ ? : 1 2 3
Condition66%2/3
defined $opt and $opt == $_
!ll&&!rl&&r
021
MC/DC50%1/2
defined $opt and $opt == $_
defined $opt $opt == $_
Truth table66%2/3
A and B
A= defined $optB= $opt == $_
ABresult
0X0
100
111
300 (defined $opt && $opt == $_ ? ' selected="selected"' : '').
301 ">$_</option>" } @items)) .
302 "</select>";
303 }
304  
305 1 ▶sub DropDownListText {
Subroutine DropDownListText: called CC=3 SCAR=11.0
Pod documented
306 1 my ($opt,$name,@items) = @_;
307  
308 return qq|<select id="$name" name="$name">| .
309 1 ▶ join("",(map { qq|<option value="$_"|.
Branch: 1 branch
BranchTrueFalseTotal
defined $opt && $opt eq $_ ? : 1 2 3
Condition66%2/3
defined $opt and $opt eq $_
!ll&&!rl&&r
021
MC/DC50%1/2
defined $opt and $opt eq $_
defined $opt $opt eq $_
Truth table66%2/3
A and B
A= defined $optB= $opt eq $_
ABresult
0X0
100
111
310 (defined $opt && $opt eq $_ ? ' selected="selected"' : '').
311 ">$_</option>" } @items)) .
312 "</select>";
313 }
314  
315 13 ▶sub DropDownRows {
Subroutine DropDownRows: called CC=5 SCAR=16.2
Pod documented
316 13 my ($opt,$name,$index,$value,@items) = @_;
317 13 ▶ $opt = undef if(defined $opt && $opt !~ /^\d+$/); # opt must be a number
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt and not $opt =~ /^\d+$/ 0 13 13
Condition66%2/3
defined $opt and not $opt =~ /^\d+$/
!ll&&!rl&&r
580
MC/DC0%0/2
defined $opt and not $opt =~ /^\d+$/
defined $opt not $opt =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $optB= not $opt =~ /^\d+$/
ABresult
0X0
100
111
318  
319 return qq|<select id="$name" name="$name">| .
320 13 join("",(map { qq|<option value="$_->{$index}"|.
321 162 ▶ (defined $opt && $opt == $_->{$index} ? ' selected="selected"' : '').
Branch: 1 branch
BranchTrueFalseTotal
defined $opt && $opt == $_->{$index} ? : 8 154 162
Condition100%3/3
defined $opt and $opt == $_->{$index}
!ll&&!rl&&r
541008
MC/DC100%2/2
defined $opt and $opt == $_->{$index}
defined $opt $opt == $_->{$index}
Truth table100%3/3
A and B
A= defined $optB= $opt == $_->{$index}
ABresult
0X0
100
111
322 ">$_->{$value}</option>" } @items)) .
323 "</select>";
324 }
325  
326 4 ▶sub DropDownRowsText {
Subroutine DropDownRowsText: called CC=3 SCAR=11.0
Pod documented
327 4 my ($opt,$name,$index,$value,@items) = @_;
328  
329 return qq|<select id="$name" name="$name">| .
330 4 join("",(map { qq|<option value="$_->{$index}"|.
331 13 ▶ (defined $opt && $opt eq $_->{$index} ? ' selected="selected"' : '').
Branch: 1 branch
BranchTrueFalseTotal
defined $opt && $opt eq $_->{$index} ? : 1 12 13
Condition100%3/3
defined $opt and $opt eq $_->{$index}
!ll&&!rl&&r
391
MC/DC100%2/2
defined $opt and $opt eq $_->{$index}
defined $opt $opt eq $_->{$index}
Truth table100%3/3
A and B
A= defined $optB= $opt eq $_->{$index}
ABresult
0X0
100
111
332 ">$_->{$value}</option>" } @items)) .
333 "</select>";
334 }
335  
336 1 ▶sub DropDownMultiList {
Subroutine DropDownMultiList: called CC=7 SCAR=22.6
Pod documented
337 1 my ($opts,$name,$count,@items) = @_;
338 1 my %opts;
339  
340 1 ▶ if(defined $opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts) 1 0 1
341 1 ▶ if(ref($opts) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $opts eq 'ARRAY') { } 0 1 1
elsif ($opts =~ /,/) { } 0 1 1
elsif ($opts) { } 1 0 1
342 0 %opts = map {$_ => 1} @$opts;
343 } elsif($opts =~ /,/) {
344 0 %opts = map {$_ => 1} split(/,/,$opts);
345 } elsif($opts) {
346 1 %opts = ("$opts" => 1);
347 }
348 }
349  
350 return qq|<select id="$name" name="$name" multiple="multiple" size="$count">| .
351 1 join("",(map { qq|<option value="$_"|.
352 3 ▶ (defined $opts && $opts{$_} ? ' selected="selected"' : '').
Branch: 1 branch
BranchTrueFalseTotal
defined $opts && $opts{$_} ? : 1 2 3
Condition66%2/3
defined $opts and $opts{$_}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $opts and $opts{$_}
defined $opts $opts{$_}
Truth table66%2/3
A and B
A= defined $optsB= $opts{$_}
ABresult
0X0
100
111
353 ">$_</option>" } @items)) .
354 "</select>";
355 }
356  
357 1 ▶sub DropDownMultiRows {
Subroutine DropDownMultiRows: called CC=7 SCAR=22.6
Pod documented
358 1 my ($opts,$name,$index,$value,$count,@items) = @_;
359 1 my %opts;
360  
361 1 ▶ if(defined $opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts) 1 0 1
362 1 ▶ if(ref($opts) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $opts eq 'ARRAY') { } 0 1 1
elsif ($opts =~ /,/) { } 0 1 1
elsif ($opts) { } 1 0 1
363 0 %opts = map {$_ => 1} @$opts;
364 } elsif($opts =~ /,/) {
365 0 %opts = map {$_ => 1} split(/,/,$opts);
366 } elsif($opts) {
367 1 %opts = ("$opts" => 1);
368 }
369 }
370  
371 return qq|<select id="$name" name="$name" multiple="multiple" size="$count">| .
372 1 join("",(map { qq|<option value="$_->{$index}"|.
373 3 ▶ (defined $opts && $opts{$_->{$index}} ? ' selected="selected"' : '').
Branch: 1 branch
BranchTrueFalseTotal
defined $opts && $opts{$_->{$index}} ? : 1 2 3
Condition66%2/3
defined $opts and $opts{$_->{$index}}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $opts and $opts{$_->{$index}}
defined $opts $opts{$_->{$index}}
Truth table66%2/3
A and B
A= defined $optsB= $opts{$_->{$index}}
ABresult
0X0
100
111
374 ">$_->{$value}</option>" } @items)) .
375 "</select>";
376 }
377  
378 =head2 Error Functions
379  
380 =over 4
381  
382 =item ErrorText
383  
384 Returns the given error string in a HTML span tag, with the configured error
385 class, which by default is called "alert". In your CSS sytle sheet you will
386 need to specify an appropriate class declaration, such as:
387  
388 .alert { color: red; font-weight: bold; }
389  
390 Set the value of 'errorclass' in your site config file to change the class
391 name used.
392  
393 =item ErrorSymbol
394  
395 Flags to the system that an error has occured and returns the configured error
396 symbol, which by is the 'empty' symbol '&#8709;', which can then be used as the
397 error field indicator.
398  
399 Set the value of 'errorsymbol' in your site config file to change the symbol
400 used.
401  
402 =back
403  
404 =cut
405  
406 2 ▶sub ErrorText {
Subroutine ErrorText: called CC=2 SCAR=6.9
Pod documented
407 2 my $text = shift;
408 2 ▶ $settings{errorclass} ||= 'alert';
Condition100%2/2
$Labyrinth::MLUtils::settings{"errorclass"} ||= "alert"
l!l
11
MC/DC100%1/1
$Labyrinth::MLUtils::settings{"errorclass"} ||= "alert"
$Labyrinth::MLUtils::settings{"errorclass"}
Truth table100%2/2
A
A= $Labyrinth::MLUtils::settings{"errorclass"}
Aresult
11
00
409 2 return qq!<span class="$settings{errorclass}">$text</span>!;
410 }
411  
412 2 ▶sub ErrorSymbol {
Subroutine ErrorSymbol: called CC=2 SCAR=6.9
Pod documented
413 2 $tvars{errmess} = 1;
414 2 $tvars{errcode} = 'ERROR';
415 2 ▶ return $settings{errorsymbol} || '&#8709;';
Condition100%2/2
$Labyrinth::MLUtils::settings{'errorsymbol'} || '&#8709;'
l!l
11
MC/DC100%1/1
$Labyrinth::MLUtils::settings{'errorsymbol'} || '&#8709;'
$Labyrinth::MLUtils::settings{'errorsymbol'}
Truth table100%2/2
A
A= $Labyrinth::MLUtils::settings{'errorsymbol'}
Aresult
11
00
416 }
417  
418 =head2 Protection Functions
419  
420 =over 4
421  
422 =item LinkSpam
423  
424 Checks whether any links exist in the given text that could indicate comment spam.
425  
426 =back
427  
428 =cut
429  
430 6 ▶sub LinkSpam {
Subroutine LinkSpam: called CC=6 SCAR=17.9
Pod documented
431 6 my $text = shift;
432 6 ▶ return 1 if($text =~ m!https?://[^\s]*!is);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ m[https?://[^\s]*]is 1 5 6
433 5 ▶ return 1 if($text =~ m!<a.*?/a>!is);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ m[<a.*?/a>]is 1 4 5
434 4 ▶ return 1 if($text =~ m!\[url.*?url\]!is);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\[url.*?url\]/is 1 3 4
435 3 ▶ return 1 if($text =~ m!\[link.*?link\]!is);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\[link.*?link\]/is 1 2 3
436 2 ▶ return 1 if($text =~ m!$settings{urlregex}!is);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$Labyrinth::MLUtils::settings{"urlregex"}/is 1 1 2
437 1 return 0;
438 }
439  
440 =head2 CSS Handling Code
441  
442 =over 4
443  
444 =item create_inline_styles ( HASHREF )
445  
446 Create inline CSS style sheet block. Key value pairs should match the label
447 (tag, identifier or class patterns) and its contents. For example:
448  
449 my %css = ( '#label p' => 'font-weight: normal; color: #fff;' );
450  
451 or
452  
453 my %css = ( '#label p' => { 'font-weight' => 'normal', 'color' => '#fff' } );
454  
455  
456 The exception to this is the label 'media', which can be used to specify the
457 medium for which the CSS will be used. Typically these are 'screen' or 'print'.
458  
459 =back
460  
461 =cut
462  
463 4 ▶sub create_inline_styles {
Subroutine create_inline_styles: called CC=8 SCAR=20.8
Pod documented
464 4 ▶ my $hash = shift || return;
Condition100%2/2
shift() || (return)
l!l
31
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
465 3 ▶ my $media = $hash->{media} ? ' media="' . $hash->{media} . '"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'media'} ? : 1 2 3
466  
467 3 my $text = qq|<style type="text/css"$media>\n|;
468 3 for my $key (sort keys %$hash) {
469 4 ▶ next if($key eq 'media');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "media" 1 3 4
470 3 $text .= qq|$key {|;
471 3 ▶ if(ref($hash->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'HASH') { } 1 2 3
elsif (ref $hash->{$key} eq 'ARRAY') { } 1 1 2
472 1 for my $attr (keys %{$hash->{$key}}) {
473 1 $text .= qq| $attr: $hash->{$key}->{$attr};|
474 }
475 } elsif(ref($hash->{$key}) eq 'ARRAY') {
476 1 $text .= ' ' . join(',', @{$hash->{$key}});
477 } else {
478 1 $text .= qq| $hash->{$key}|;
479 }
480 3 $text .= qq| }\n|;
481 }
482 3 $text .= qq|</style>\n|;
483 3 return $text;
484 }
485  
486 =head2 HTML Demoroniser Code
487  
488 =over 4
489  
490 =item demoroniser ( INPUT )
491  
492 Given a string, will replace the Microsoft "smart" characters with sensible
493 ACSII versions.
494  
495 =back
496  
497 =cut
498  
499 0 ▶sub demoroniser {
Subroutine demoroniser: not called CC=1 SCAR=6.9
Pod documented
500 0 my $str = shift;
501  
502 0 zap_cp1252($str);
503  
504 0 $str =~ s/\xE2\x80\x9A/,/g; # 82
505 0 $str =~ s/\xE2\x80\x9E/,,/g; # 84
506 0 $str =~ s/\xE2\x80\xA6/.../g; # 85
507  
508 0 $str =~ s/\xCB\x86/^/g; # 88
509  
510 0 $str =~ s/\xE2\x80\x98/`/g; # 91
511 0 $str =~ s/\xE2\x80\x99/'/g; # 92
512 0 $str =~ s/\xE2\x80\x9C/"/g; # 93
513 0 $str =~ s/\xE2\x80\x9D/"/g; # 94
514 0 $str =~ s/\xE2\x80\xA2/*/g; # 95
515 0 $str =~ s/\xE2\x80\x93/-/g; # 96
516 0 $str =~ s/\xE2\x80\x94/-/g; # 97
517  
518 0 $str =~ s/\xE2\x80\xB9/</g; # 8B
519 0 $str =~ s/\xE2\x80\xBA/>/g; # 9B
520  
521 0 return $str;
522 }
523  
524 =head2 HTML Handling Code
525  
526 The following functions disassemble and reassemble the HTML code snippets,
527 validating and cleaning the code to fix any errors that may exist between the
528 template and content of the database.
529  
530 =over 4
531  
532 =item process_html ( INPUT [,LINE_BREAKS [,ALLOW]] )
533  
534 =item escape_html ( INPUT )
535  
536 =item unescape_html ( INPUT )
537  
538 =item cleanup_attr_style
539  
540 =item cleanup_attr_number
541  
542 =item cleanup_attr_multilength
543  
544 =item cleanup_attr_text
545  
546 =item cleanup_attr_length
547  
548 =item cleanup_attr_color
549  
550 =item cleanup_attr_uri
551  
552 =item cleanup_attr_tframe
553  
554 =item cleanup_attr_trules
555  
556 =item cleanup_html
557  
558 =item cleanup_tag
559  
560 =item cleanup_close
561  
562 =item cleanup_cdata
563  
564 =item cleanup_no_number
565  
566 =item check_url_valid
567  
568 =item cleanup_attr_inputtype
569  
570 =item cleanup_attr_method
571  
572 =item cleanup_attr_scriptlang
573  
574 =item cleanup_attr_scripttype
575  
576 =item strip_nonprintable
577  
578 =back
579  
580 =cut
581  
582 # Configuration
583 14 my $allow_html = 0;
584 14 my $line_breaks = 1;
585 # End configuration
586  
587 ##################################################################
588 #
589 # HTML handling code
590 #
591 # The code below provides some functions for manipulating HTML.
592 #
593 # process_html ( INPUT [,LINE_BREAKS [,ALLOW]] )
594 #
595 # Returns a modified version of the HTML string INPUT, with
596 # any potentially malicious HTML constructs (such as java,
597 # javascript and IMG tags) removed.
598 #
599 # If the LINE_BREAKS parameter is present and true then
600 # line breaks in the input will be converted to html <br />
601 # tags in the output.
602 #
603 # If the ALLOW parameter is present and true then most
604 # harmless tags will be left in, otherwise all tags will be
605 # removed.
606 #
607 # escape_html ( INPUT )
608 #
609 # Returns a copy of the string INPUT with any HTML
610 # metacharacters replaced with character escapes.
611 #
612 # unescape_html ( INPUT )
613 #
614 # Returns a copy of the string INPUT with HTML character
615 # entities converted to literal characters where possible.
616 # Note that some entites have no 8-bit character equivalent,
617 # see "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent"
618 # for some examples. unescape_html() leaves these entities
619 # in their encoded form.
620 #
621  
622 14 ▶use vars qw(%html_entities $html_safe_chars %escape_html_map $escape_html_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
623 14 ▶use vars qw(%safe_tags %safe_style %tag_is_empty %closetag_is_optional
Subroutine BEGIN: called CC=1 SCAR=0.0
624 %closetag_is_dependent %force_closetag %transpose_tag
625 14 $convert_nl %auto_deinterleave $auto_deinterleave_pattern);
626  
627 # check the validity of a URL.
628  
629 7 ▶sub process_html {
Subroutine process_html: called CC=3 SCAR=11.0
Pod documented
630 7 my ($text, $line_breaks, $allow_html) = @_;
631  
632 # cleanup erroneous XHTML patterns
633 7 ▶ if($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 7 0 7
634 7 $text =~ s!</pre><pre>!<br />!gsi;
635 7 $text =~ s!<ul>\s*<br />!<ul>!gsi;
636 7 $text =~ s!<br />\s*</ul>!</ul>!gsi;
637 7 $text =~ s!<ul>\s*</ul>!!gsi;
638 7 $text =~ s!<ol>\s*</ol>!!gsi;
639 }
640  
641 # clean text of any nasties
642 #$text =~ s/[\x201A\x2018\x2019`]/&#39;/g; # nasty single quotes
643 #$text =~ s/[\x201E\x201C\x201D]/&quot;/g; # nasty double quotes
644  
645 7 ▶ cleanup_html( $text, $line_breaks, ($allow_html ? \%safe_tags : {}));
Branch: 1 branch
BranchTrueFalseTotal
$allow_html ? : 4 3 7
646 }
647  
648 BEGIN
649 14 ▶{
Subroutine BEGIN: called CC=4 SCAR=19.2
650 14 %html_entities = (
651 'lt' => '<',
652 'gt' => '>',
653 'quot' => '"',
654 'amp' => '&',
655  
656 'nbsp' => "\240", 'iexcl' => "\241",
657 'cent' => "\242", 'pound' => "\243",
658 'curren' => "\244", 'yen' => "\245",
659 'brvbar' => "\246", 'sect' => "\247",
660 'uml' => "\250", 'copy' => "\251",
661 'ordf' => "\252", 'laquo' => "\253",
662 'not' => "\254", 'shy' => "\255",
663 'reg' => "\256", 'macr' => "\257",
664 'deg' => "\260", 'plusmn' => "\261",
665 'sup2' => "\262", 'sup3' => "\263",
666 'acute' => "\264", 'micro' => "\265",
667 'para' => "\266", 'middot' => "\267",
668 'cedil' => "\270", 'supl' => "\271",
669 'ordm' => "\272", 'raquo' => "\273",
670 'frac14' => "\274", 'frac12' => "\275",
671 'frac34' => "\276", 'iquest' => "\277",
672  
673 'Agrave' => "\300", 'Aacute' => "\301",
674 'Acirc' => "\302", 'Atilde' => "\303",
675 'Auml' => "\304", 'Aring' => "\305",
676 'AElig' => "\306", 'Ccedil' => "\307",
677 'Egrave' => "\310", 'Eacute' => "\311",
678 'Ecirc' => "\312", 'Euml' => "\313",
679 'Igrave' => "\314", 'Iacute' => "\315",
680 'Icirc' => "\316", 'Iuml' => "\317",
681 'ETH' => "\320", 'Ntilde' => "\321",
682 'Ograve' => "\322", 'Oacute' => "\323",
683 'Ocirc' => "\324", 'Otilde' => "\325",
684 'Ouml' => "\326", 'times' => "\327",
685 'Oslash' => "\330", 'Ugrave' => "\331",
686 'Uacute' => "\332", 'Ucirc' => "\333",
687 'Uuml' => "\334", 'Yacute' => "\335",
688 'THORN' => "\336", 'szlig' => "\337",
689  
690 'agrave' => "\340", 'aacute' => "\341",
691 'acirc' => "\342", 'atilde' => "\343",
692 'auml' => "\344", 'aring' => "\345",
693 'aelig' => "\346", 'ccedil' => "\347",
694 'egrave' => "\350", 'eacute' => "\351",
695 'ecirc' => "\352", 'euml' => "\353",
696 'igrave' => "\354", 'iacute' => "\355",
697 'icirc' => "\356", 'iuml' => "\357",
698 'eth' => "\360", 'ntilde' => "\361",
699 'ograve' => "\362", 'oacute' => "\363",
700 'ocirc' => "\364", 'otilde' => "\365",
701 'ouml' => "\366", 'divide' => "\367",
702 'oslash' => "\370", 'ugrave' => "\371",
703 'uacute' => "\372", 'ucirc' => "\373",
704 'uuml' => "\374", 'yacute' => "\375",
705 'thorn' => "\376", 'yuml' => "\377",
706 );
707  
708 #
709 # Build a map for representing characters in HTML.
710 #
711 14 $html_safe_chars = '()[]{}/?.,\\|;:@#~=+-_*^%$! ' . "\'\r\n\t";
712 14 $escape_html_map = qr{[\w\(\)\[\]\{\}\/\?\.\,\\\|;:\@#~=\+\-\*\^\%\$\!\s\']+};
713 %escape_html_map =
714 14 map {$_,$_} ( 'A'..'Z', 'a'..'z', '0'..'9',
715 split(//, $html_safe_chars)
716 );
717 14 foreach my $ent (keys %html_entities) {
718 1400 $escape_html_map{$html_entities{$ent}} = "&$ent;";
719 }
720 14 foreach my $c (0..255) {
721 3584 ▶ unless ( exists $escape_html_map{chr $c} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Labyrinth::MLUtils::escape_html_map{chr $c}) 882 2702 3584
722 882 $escape_html_map{chr $c} = sprintf '&#%d;', $c;
723 }
724 }
725  
726 #
727 # Tables for use by cleanup_html() (below).
728 #
729 # The main table is %safe_tags, which is a hash by tag name of
730 # all the tags that it's safe to leave in. The value for each
731 # tag is another hash, and each key of that hash defines an
732 # attribute that the tag is allowed to have.
733 #
734 # The values in the tag attribute hash can be undef (for an
735 # attribute that takes no value, for example the nowrap
736 # attribute in the tag <td align="left" nowrap>) or they can
737 # be coderefs pointing to subs for cleaning up the attribute
738 # values.
739 #
740 # These subs will called with the attribute value in $_, and
741 # they can return either a cleaned attribute value or undef.
742 # If undef is returned then the attribute will be deleted
743 # from the tag.
744 #
745 # The list of tags and attributes was taken from
746 # "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
747 #
748 # The %tag_is_empty table defines the set of tags that have
749 # no corresponding close tag.
750 #
751 # cleanup_html() moves close tags around to force all tags to
752 # be closed in the correct sequence. For example, the text
753 # "<h1><i>foo</h1>bar</i>" will be converted to the text
754 # "<h1><i>foo</i></h1>bar".
755 #
756 # The %auto_deinterleave table defines the set of tags which
757 # should be automatically reopened if they're closed early
758 # in this way. All the tags involved must be in
759 # %auto_deinterleave for the tag to be reopened. For example,
760 # the text "<b>bb<i>bi</b>ii</i>" will be converted into the
761 # text "<b>bb<i>bi</i></b><i>ii</i>" rather than into the
762 # text "<b>bb<i>bi</i></b>ii", because *both* "b" and "i" are
763 # in %auto_deinterleave.
764 #
765 %tag_is_empty = (
766 14 'hr' => 1, 'link' => 1, 'param' => 1, 'img' => 1,
767 'br' => 1, 'area' => 1, 'input' => 1, 'basefont' => 1
768 );
769 14 %closetag_is_optional = ( );
770 14 %closetag_is_dependent = ( );
771 14 %force_closetag = (
772 'pre' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
773 'p' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
774 'h1' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
775 'h2' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
776 'h3' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
777 'h4' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
778 'h5' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
779 'h6' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
780 'table' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'pre' => 1, 'ul' => 1, 'ol' => 1 },
781 'ul' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1 },
782 'ol' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1 },
783 'li' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'li' => 1 },
784 'form' => { 'p' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1 },
785 );
786 14 %transpose_tag = ( 'b' => 'strong', 'u' => 'em' );
787 14 %auto_deinterleave = map {$_,1} qw(
788 tt i b big small u s strike font basefont
789 em strong dfn code q sub sup samp kbd var
790 cite abbr acronym span
791 );
792 14 $auto_deinterleave_pattern = join '|', keys %auto_deinterleave;
793 14 my %attr = (
794 'style' => \&cleanup_attr_style,
795 'name' => \&cleanup_attr_text,
796 'id' => \&cleanup_attr_text,
797 'class' => \&cleanup_attr_text,
798 'title' => \&cleanup_attr_text,
799 'onmouseover' => \&cleanup_attr_text,
800 'onmouseout' => \&cleanup_attr_text,
801 'onclick' => \&cleanup_attr_text,
802 'onfocus' => \&cleanup_attr_text,
803 'ondblclick' => \&cleanup_attr_text,
804 );
805 my %font_attr = (
806 %attr,
807 0 ▶ size => sub { /^([-+]?\d{1,3})$/ ? $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^([-+]?\d{1,3})$/ ? : 0 0 0
808 0 ▶ face => sub { /^([\w\-, ]{2,100})$/ ? $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^([\w\-, ]{2,100})$/ ? : 0 0 0
809 14 color => \&cleanup_attr_color,
810 );
811 14 my %insdel_attr = (
812 %attr,
813 'cite' => \&cleanup_attr_uri,
814 'datetime' => \&cleanup_attr_text,
815 );
816 my %texta_attr = (
817 %attr,
818 0 ▶ align => sub { s/middle/center/i;
Subroutine __ANON__: not called CC=2 SCAR=17.9
819 0 ▶ /^(left|center|right|justify)$/i ? lc $1 : undef
Branch: 1 branch
BranchTrueFalseTotal
/^(left|center|right|justify)$/i ? : 0 0 0
820 },
821 14 );
822 my %cellha_attr = (
823 0 ▶ align => sub { s/middle/center/i;
Subroutine __ANON__: not called CC=2 SCAR=17.9
824 0 ▶ /^(left|center|right|justify|char)$/i
Branch: 1 branch
BranchTrueFalseTotal
/^(left|center|right|justify|char)$/i ? : 0 0 0
825 ? lc $1 : undef
826 },
827 0 ▶ char => sub { /^([\w\-])$/ ? $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^([\w\-])$/ ? : 0 0 0
828 14 charoff => \&cleanup_attr_length,
829 );
830 my %cellva_attr = (
831 0 ▶ valign => sub { s/center/middle/i;
Subroutine __ANON__: not called CC=2 SCAR=17.9
832 0 ▶ /^(top|middle|bottom|baseline)$/i ? lc $1 : undef
Branch: 1 branch
BranchTrueFalseTotal
/^(top|middle|bottom|baseline)$/i ? : 0 0 0
833 },
834 14 );
835 14 my %cellhv_attr = ( %attr, %cellha_attr, %cellva_attr );
836 14 my %col_attr = (
837 %attr,
838 width => \&cleanup_attr_multilength,
839 span => \&cleanup_attr_number,
840 %cellhv_attr,
841 );
842 my %thtd_attr = (
843 %attr,
844 abbr => \&cleanup_attr_text,
845 axis => \&cleanup_attr_text,
846 headers => \&cleanup_attr_text,
847 0 ▶ scope => sub { /^(row|col|rowgroup|colgroup)$/i ? lc $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^(row|col|rowgroup|colgroup)$/i ? : 0 0 0
848 14 rowspan => \&cleanup_attr_number,
849 colspan => \&cleanup_attr_number,
850 %cellhv_attr,
851 nowrap => undef,
852 bgcolor => \&cleanup_attr_color,
853 width => \&cleanup_attr_number,
854 height => \&cleanup_attr_number,
855 );
856 14 my $none = {};
857 %safe_tags = (
858 # FORM CONTROLS
859 'form' => { %attr,
860 'method' => \&cleanup_attr_method,
861 'action' => \&cleanup_attr_text,
862 'enctype' => \&cleanup_attr_text,
863 'onsubmit' => \&cleanup_attr_text,
864 },
865 'button' => { %attr,
866 'type' => \&cleanup_attr_inputtype,
867 },
868 'input' => { %attr,
869 'type' => \&cleanup_attr_inputtype,
870 'size' => \&cleanup_attr_number,
871 'maxlength' => \&cleanup_attr_number,
872 'value' => \&cleanup_attr_text,
873 'checked' => \&cleanup_attr_text,
874 'readonly' => \&cleanup_attr_text,
875 'disabled' => \&cleanup_attr_text,
876 'src' => \&cleanup_attr_uri,
877 'width' => \&cleanup_attr_length,
878 'height' => \&cleanup_attr_length,
879 'alt' => \&cleanup_attr_text,
880 'onchange' => \&cleanup_attr_text,
881 },
882 'select' => { %attr,
883 'size' => \&cleanup_attr_number,
884 'title' => \&cleanup_attr_text,
885 'value' => \&cleanup_attr_text,
886 'multiple' => \&cleanup_attr_text,
887 'disabled' => \&cleanup_attr_text,
888 'onchange' => \&cleanup_attr_text,
889 },
890 'option' => { %attr,
891 'value' => \&cleanup_attr_text,
892 'selected' => \&cleanup_attr_text,
893 },
894 'textarea' => { %attr,
895 'rows' => \&cleanup_attr_number,
896 'cols' => \&cleanup_attr_number,
897 },
898 'label' => { %attr,
899 'for' => \&cleanup_attr_text,
900 },
901  
902 # LAYOUT STYLE
903 'style' => {
904 'type' => \&cleanup_attr_text,
905 },
906 0 ▶ 'br' => { 'clear' => sub { /^(left|right|all|none)$/i ? lc $1 : undef }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^(left|right|all|none)$/i ? : 0 0 0
907 },
908 'hr' => \%attr,
909 'em' => \%attr,
910 'strong' => \%attr,
911 'dfn' => \%attr,
912 'code' => \%attr,
913 'samp' => \%attr,
914 'kbd' => \%attr,
915 'var' => \%attr,
916 'cite' => \%attr,
917 'abbr' => \%attr,
918 'acronym' => \%attr,
919 'q' => { %attr, 'cite' => \&cleanup_attr_uri },
920 'blockquote' => { %attr, 'cite' => \&cleanup_attr_uri },
921 'sub' => \%attr,
922 'sup' => \%attr,
923 'tt' => \%attr,
924 'i' => \%attr,
925 'b' => \%attr,
926 'big' => \%attr,
927 'small' => \%attr,
928 'u' => \%attr,
929 's' => \%attr,
930 'font' => \%font_attr,
931 'h1' => \%texta_attr,
932 'h2' => \%texta_attr,
933 'h3' => \%texta_attr,
934 'h4' => \%texta_attr,
935 'h5' => \%texta_attr,
936 'h6' => \%texta_attr,
937 'p' => \%texta_attr,
938 'div' => \%texta_attr,
939 'span' => \%texta_attr,
940 'ul' => { %attr,
941 0 ▶ 'type' => sub { /^(disc|square|circle)$/i ? lc $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^(disc|square|circle)$/i ? : 0 0 0
942 'compact' => undef,
943 },
944 'ol' => { %attr,
945 'type' => \&cleanup_attr_text,
946 'compact' => undef,
947 'start' => \&cleanup_attr_number,
948 },
949 'li' => { %attr,
950 'type' => \&cleanup_attr_text,
951 'value' => \&cleanup_no_number,
952 },
953 'dl' => { %attr, 'compact' => undef },
954 'dt' => \%attr,
955 'dd' => \%attr,
956 'address' => \%attr,
957 'pre' => { %attr, 'width' => \&cleanup_attr_number },
958 'center' => \%attr,
959 'nobr' => $none,
960  
961 # FUNCTIONAL TAGS
962 'iframe' => { %attr,
963 'src' => \&cleanup_attr_uri,
964 'width' => \&cleanup_attr_length,
965 'height' => \&cleanup_attr_length,
966 'border' => \&cleanup_attr_number,
967 'alt' => \&cleanup_attr_text,
968 0 ▶ 'align' => sub { s/middle/center/i;
Subroutine __ANON__: not called CC=2 SCAR=17.9
969 0 ▶ /^(left|center|right)$/i ? lc $1 : undef
Branch: 1 branch
BranchTrueFalseTotal
/^(left|center|right)$/i ? : 0 0 0
970 },
971 'title' => \&cleanup_attr_text,
972 },
973 'img' => { %attr,
974 'src' => \&cleanup_attr_uri,
975 'width' => \&cleanup_attr_length,
976 'height' => \&cleanup_attr_length,
977 'border' => \&cleanup_attr_number,
978 'alt' => \&cleanup_attr_text,
979 0 ▶ 'align' => sub { s/middle/center/i;
Subroutine __ANON__: not called CC=2 SCAR=17.9
980 0 ▶ /^(left|center|right)$/i ? lc $1 : undef
Branch: 1 branch
BranchTrueFalseTotal
/^(left|center|right)$/i ? : 0 0 0
981 },
982 'title' => \&cleanup_attr_text,
983 'usemap' => \&cleanup_attr_text,
984 },
985 'map' => { %attr,
986 },
987 'area' => { %attr,
988 'shape' => \&cleanup_attr_text,
989 'coords' => \&cleanup_attr_text,
990 'href' => \&cleanup_attr_uri,
991 },
992 'table' => { %attr,
993 'frame' => \&cleanup_attr_tframe,
994 'rules' => \&cleanup_attr_trules,
995 %texta_attr,
996 'bgcolor' => \&cleanup_attr_color,
997 'width' => \&cleanup_attr_length,
998 'cellspacing' => \&cleanup_attr_length,
999 'cellpadding' => \&cleanup_attr_length,
1000 'border' => \&cleanup_attr_number,
1001 'summary' => \&cleanup_attr_text,
1002 },
1003 'caption' => { %attr,
1004 0 ▶ 'align' => sub { /^(top|bottom|left|right)$/i ? lc $1 : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/^(top|bottom|left|right)$/i ? : 0 0 0
1005 },
1006 14 'colgroup' => \%col_attr,
1007 'col' => \%col_attr,
1008 'thead' => \%cellhv_attr,
1009 'tfoot' => \%cellhv_attr,
1010 'tbody' => \%cellhv_attr,
1011 'tr' => { %attr,
1012 bgcolor => \&cleanup_attr_color,
1013 %cellhv_attr,
1014 },
1015 'th' => \%thtd_attr,
1016 'td' => \%thtd_attr,
1017 'ins' => \%insdel_attr,
1018 'del' => \%insdel_attr,
1019 'a' => { %attr,
1020 href => \&cleanup_attr_uri,
1021 style => \&cleanup_attr_text,
1022 target => \&cleanup_attr_text,
1023 rel => \&cleanup_attr_text,
1024 },
1025  
1026 'script' => {
1027 language => \&cleanup_attr_scriptlang,
1028 type => \&cleanup_attr_scripttype,
1029 src => \&cleanup_attr_uri,
1030 },
1031 'noscript' => { %attr,
1032 },
1033 'link' => { %attr,
1034 href => \&cleanup_attr_uri,
1035 'rel' => \&cleanup_attr_text,
1036 'type' => \&cleanup_attr_text,
1037 'media' => \&cleanup_attr_text,
1038 },
1039 'object' => { %attr,
1040 'width' => \&cleanup_attr_length,
1041 'height' => \&cleanup_attr_length,
1042 style => \&cleanup_attr_text,
1043 type => \&cleanup_attr_text,
1044 data => \&cleanup_attr_text,
1045 classid => \&cleanup_attr_text,
1046 codebase => \&cleanup_attr_text,
1047 },
1048 'param' => {
1049 name => \&cleanup_attr_text,
1050 value => \&cleanup_attr_text,
1051 },
1052 'embed' => { %attr,
1053 'src' => \&cleanup_attr_uri,
1054 'bgcolor' => \&cleanup_attr_color,
1055 'width' => \&cleanup_attr_length,
1056 'height' => \&cleanup_attr_length,
1057 'pluginspage' => \&cleanup_attr_uri,
1058 flashvars => \&cleanup_attr_text,
1059 type => \&cleanup_attr_text,
1060 quality => \&cleanup_attr_text,
1061 allowScriptAccess => \&cleanup_attr_text,
1062 allowNetworking => \&cleanup_attr_text,
1063 },
1064 );
1065  
1066 14 %safe_style = (
1067 'animation' => \&cleanup_attr_text,
1068 'animation-name' => \&cleanup_attr_text,
1069 'animation-duration' => \&cleanup_attr_text,
1070 'animation-timing-function' => \&cleanup_attr_text,
1071 'animation-delay' => \&cleanup_attr_text,
1072 'animation-iteration-count' => \&cleanup_attr_text,
1073 'animation-direction' => \&cleanup_attr_text,
1074 'animation-play-state' => \&cleanup_attr_text,
1075 'appearance' => \&cleanup_attr_text,
1076 'backface-visibility' => \&cleanup_attr_text,
1077 'background' => \&cleanup_attr_text,
1078 'background-attachment' => \&cleanup_attr_text,
1079 'background-color' => \&cleanup_attr_color,
1080 'background-image' => \&cleanup_attr_text,
1081 'background-position' => \&cleanup_attr_text,
1082 'background-repeat' => \&cleanup_attr_text,
1083 'background-clip' => \&cleanup_attr_text,
1084 'background-origin' => \&cleanup_attr_text,
1085 'background-size' => \&cleanup_attr_text,
1086 'border' => \&cleanup_attr_text,
1087 'border-bottom' => \&cleanup_attr_text,
1088 'border-bottom-color' => \&cleanup_attr_color,
1089 'border-bottom-style' => \&cleanup_attr_text,
1090 'border-bottom-width' => \&cleanup_attr_length,
1091 'border-collapse' => \&cleanup_attr_text,
1092 'border-color' => \&cleanup_attr_color,
1093 'border-left' => \&cleanup_attr_text,
1094 'border-left-color' => \&cleanup_attr_color,
1095 'border-left-style' => \&cleanup_attr_text,
1096 'border-left-width' => \&cleanup_attr_length,
1097 'border-right' => \&cleanup_attr_text,
1098 'border-right-color' => \&cleanup_attr_color,
1099 'border-right-style' => \&cleanup_attr_text,
1100 'border-right-width' => \&cleanup_attr_length,
1101 'border-spacing' => \&cleanup_attr_text,
1102 'border-style' => \&cleanup_attr_text,
1103 'border-top' => \&cleanup_attr_text,
1104 'border-top-color' => \&cleanup_attr_color,
1105 'border-top-style' => \&cleanup_attr_text,
1106 'border-top-width' => \&cleanup_attr_length,
1107 'border-width' => \&cleanup_attr_length,
1108 'border-bottom-left-radius' => \&cleanup_attr_text,
1109 'border-bottom-right-radius' => \&cleanup_attr_text,
1110 'border-image' => \&cleanup_attr_text,
1111 'border-image-outset' => \&cleanup_attr_text,
1112 'border-image-repeat' => \&cleanup_attr_text,
1113 'border-image-slice' => \&cleanup_attr_text,
1114 'border-image-source' => \&cleanup_attr_text,
1115 'border-image-width' => \&cleanup_attr_length,
1116 'border-radius' => \&cleanup_attr_text,
1117 'border-top-left-radius' => \&cleanup_attr_text,
1118 'border-top-right-radius' => \&cleanup_attr_text,
1119 'bottom' => \&cleanup_attr_text,
1120 'box' => \&cleanup_attr_text,
1121 'box-align' => \&cleanup_attr_text,
1122 'box-direction' => \&cleanup_attr_text,
1123 'box-flex' => \&cleanup_attr_text,
1124 'box-flex-group' => \&cleanup_attr_text,
1125 'box-lines' => \&cleanup_attr_text,
1126 'box-ordinal-group' => \&cleanup_attr_text,
1127 'box-orient' => \&cleanup_attr_text,
1128 'box-pack' => \&cleanup_attr_text,
1129 'box-sizing' => \&cleanup_attr_text,
1130 'box-shadow' => \&cleanup_attr_text,
1131 'caption-side' => \&cleanup_attr_text,
1132 'clear' => \&cleanup_attr_text,
1133 'clip' => \&cleanup_attr_text,
1134 'color' => \&cleanup_attr_color,
1135 'column' => \&cleanup_attr_text,
1136 'column-count' => \&cleanup_attr_text,
1137 'column-fill' => \&cleanup_attr_text,
1138 'column-gap' => \&cleanup_attr_text,
1139 'column-rule' => \&cleanup_attr_text,
1140 'column-rule-color' => \&cleanup_attr_text,
1141 'column-rule-style' => \&cleanup_attr_text,
1142 'column-rule-width' => \&cleanup_attr_length,
1143 'column-span' => \&cleanup_attr_text,
1144 'column-width' => \&cleanup_attr_length,
1145 'columns' => \&cleanup_attr_text,
1146 'content' => \&cleanup_attr_text,
1147 'counter-increment' => \&cleanup_attr_text,
1148 'counter-reset' => \&cleanup_attr_text,
1149 'cursor' => \&cleanup_attr_text,
1150 'direction' => \&cleanup_attr_text,
1151 'display' => \&cleanup_attr_text,
1152 'empty-cells' => \&cleanup_attr_text,
1153 'float' => \&cleanup_attr_text,
1154 'font' => \&cleanup_attr_text,
1155 'font-family' => \&cleanup_attr_text,
1156 'font-size' => \&cleanup_attr_text,
1157 'font-style' => \&cleanup_attr_text,
1158 'font-variant' => \&cleanup_attr_text,
1159 'font-weight' => \&cleanup_attr_length,
1160 '@font-face' => \&cleanup_attr_text,
1161 'font-size-adjust' => \&cleanup_attr_text,
1162 'font-stretch' => \&cleanup_attr_text,
1163 'grid-columns' => \&cleanup_attr_text,
1164 'grid-rows' => \&cleanup_attr_text,
1165 'hanging-punctuation' => \&cleanup_attr_text,
1166 'height' => \&cleanup_attr_length,
1167 'icon' => \&cleanup_attr_text,
1168 '@keyframes' => \&cleanup_attr_text,
1169 'left' => \&cleanup_attr_length,
1170 'letter-spacing' => \&cleanup_attr_text,
1171 'line-height' => \&cleanup_attr_text,
1172 'list-style' => \&cleanup_attr_text,
1173 'list-style-image' => \&cleanup_attr_text,
1174 'list-style-position' => \&cleanup_attr_text,
1175 'list-style-type' => \&cleanup_attr_text,
1176 'margin' => \&cleanup_attr_text,
1177 'margin-bottom' => \&cleanup_attr_length,
1178 'margin-left' => \&cleanup_attr_length,
1179 'margin-right' => \&cleanup_attr_length,
1180 'margin-top' => \&cleanup_attr_length,
1181 'max-height' => \&cleanup_attr_length,
1182 'max-width' => \&cleanup_attr_length,
1183 'min-height' => \&cleanup_attr_length,
1184 'min-width' => \&cleanup_attr_length,
1185 'nav' => \&cleanup_attr_text,
1186 'nav-down' => \&cleanup_attr_text,
1187 'nav-index' => \&cleanup_attr_text,
1188 'nav-left' => \&cleanup_attr_text,
1189 'nav-right' => \&cleanup_attr_text,
1190 'nav-up' => \&cleanup_attr_text,
1191 'opacity' => \&cleanup_attr_text,
1192 'outline' => \&cleanup_attr_text,
1193 'outline-color' => \&cleanup_attr_color,
1194 'outline-offset' => \&cleanup_attr_text,
1195 'outline-style' => \&cleanup_attr_text,
1196 'outline-width' => \&cleanup_attr_length,
1197 'overflow' => \&cleanup_attr_text,
1198 'overflow-x' => \&cleanup_attr_text,
1199 'overflow-y' => \&cleanup_attr_text,
1200 'padding' => \&cleanup_attr_text,
1201 'padding-bottom' => \&cleanup_attr_length,
1202 'padding-left' => \&cleanup_attr_length,
1203 'padding-right' => \&cleanup_attr_length,
1204 'padding-top' => \&cleanup_attr_length,
1205 'page-break' => \&cleanup_attr_text,
1206 'page-break-after' => \&cleanup_attr_text,
1207 'page-break-before' => \&cleanup_attr_text,
1208 'page-break-inside' => \&cleanup_attr_text,
1209 'perspective' => \&cleanup_attr_text,
1210 'perspective-origin' => \&cleanup_attr_text,
1211 'position' => \&cleanup_attr_text,
1212 'punctuation-trim' => \&cleanup_attr_text,
1213 'quotes' => \&cleanup_attr_text,
1214 'resize' => \&cleanup_attr_text,
1215 'right' => \&cleanup_attr_length,
1216 'rotation' => \&cleanup_attr_text,
1217 'rotation-point' => \&cleanup_attr_text,
1218 'table-layout' => \&cleanup_attr_text,
1219 'target' => \&cleanup_attr_text,
1220 'target-name' => \&cleanup_attr_text,
1221 'target-new' => \&cleanup_attr_text,
1222 'target-position' => \&cleanup_attr_text,
1223 'text' => \&cleanup_attr_text,
1224 'text-align' => \&cleanup_attr_text,
1225 'text-decoration' => \&cleanup_attr_text,
1226 'text-indent' => \&cleanup_attr_text,
1227 'text-justify' => \&cleanup_attr_text,
1228 'text-outline' => \&cleanup_attr_text,
1229 'text-overflow' => \&cleanup_attr_text,
1230 'text-shadow' => \&cleanup_attr_text,
1231 'text-transform' => \&cleanup_attr_text,
1232 'text-wrap' => \&cleanup_attr_text,
1233 'top' => \&cleanup_attr_length,
1234 'transform' => \&cleanup_attr_text,
1235 'transform-origin' => \&cleanup_attr_text,
1236 'transform-style' => \&cleanup_attr_text,
1237 'transition' => \&cleanup_attr_text,
1238 'transition-property' => \&cleanup_attr_text,
1239 'transition-duration' => \&cleanup_attr_text,
1240 'transition-timing-function' => \&cleanup_attr_text,
1241 'transition-delay' => \&cleanup_attr_text,
1242 'vertical-align' => \&cleanup_attr_text,
1243 'visibility' => \&cleanup_attr_text,
1244 'width' => \&cleanup_attr_length,
1245 'white-space' => \&cleanup_attr_text,
1246 'word-spacing' => \&cleanup_attr_text,
1247 'word-break' => \&cleanup_attr_text,
1248 'word-wrap' => \&cleanup_attr_text,
1249 'z-index' => \&cleanup_attr_text
1250 );
1251 }
1252  
1253  
1254 0 ▶sub cleanup_attr_style {
Subroutine cleanup_attr_style: not called CC=6 SCAR=37.4
Pod documented
1255 0 my @clean = ();
1256 0 foreach my $elt (split /;/, $_) {
1257 0 ▶ next if $elt =~ m#^\s*$#;
Branch: 1 branch
BranchTrueFalseTotal
if $elt =~ /^\s*$/ 0 0 0
1258 0 ▶ if ( $elt =~ m#^\s*([\w\-]+)\s*:\s*(.+?)\s*$#s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt =~ /^\s*([\w\-]+)\s*:\s*(.+?)\s*$/s) 0 0 0
1259 0 my ($key, $val) = (lc $1, $2);
1260 0 local $_ = $val;
1261 0 my $sub = $safe_style{$key};
1262 0 ▶ if (defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub) 0 0 0
1263 0 my $cleanval = &{$sub}();
1264 0 ▶ if (defined $cleanval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cleanval) 0 0 0
1265 0 push @clean, "$key:$val";
1266 }
1267 }
1268 }
1269 }
1270 0 return join '; ', @clean;
1271 }
1272 2 ▶sub cleanup_attr_number {
Subroutine cleanup_attr_number: called CC=2 SCAR=6.9
Pod documented
1273 2 ▶ /^(\d+)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+)$/ ? : 1 1 2
1274 }
1275 2 ▶sub cleanup_attr_method {
Subroutine cleanup_attr_method: called CC=2 SCAR=6.9
Pod documented
1276 2 ▶ /^(get|post)$/i ? lc $1 : 'post';
Branch: 1 branch
BranchTrueFalseTotal
/^(get|post)$/i ? : 1 1 2
1277 }
1278 2 ▶sub cleanup_attr_inputtype {
Subroutine cleanup_attr_inputtype: called CC=2 SCAR=6.9
Pod documented
1279 2 ▶ /^(text|password|checkbox|radio|submit|reset|file|hidden|image|button)$/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(text|password|checkbox|radio|submit|reset|file|hidden|image|button)$/i ? : 1 1 2
1280 }
1281 4 ▶sub cleanup_attr_multilength {
Subroutine cleanup_attr_multilength: called CC=2 SCAR=6.9
Pod documented
1282 4 ▶ /^(\d+(?:\.\d+)?[*%]?)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+(?:\.\d+)?[*%]?)$/ ? : 3 1 4
1283 }
1284 2 ▶sub cleanup_attr_text {
Subroutine cleanup_attr_text: called CC=1 SCAR=0.0
Pod documented
1285 2 tr/-a-zA-Z0-9_()[]{}\/?.,\\|;:&@#~=+*^%$'! \xc0-\xff//dc;
1286 2 $_;
1287 }
1288 3 ▶sub cleanup_attr_length {
Subroutine cleanup_attr_length: called CC=2 SCAR=6.9
Pod documented
1289 3 ▶ /^(\d+(\%|px|em)?)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+(\%|px|em)?)$/ ? : 2 1 3
1290 }
1291 5 ▶sub cleanup_attr_color {
Subroutine cleanup_attr_color: called CC=3 SCAR=11.1
Pod documented
1292 5 ▶ /^(\w{2,20}|#[\da-fA-F]{3}|#[\da-fA-F]{6})$/ or die "color <<$_>> bad";
Branch: 1 branch
BranchTrueFalseTotal
/^(\w{2,20}|#[\da-fA-F]{3}|#[\da-fA-F]{6})$/ or die "color <<$_>> bad" 3 2 5
1293 3 ▶ /^(\w{2,20}|#[\da-fA-F]{3}|#[\da-fA-F]{6})$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w{2,20}|#[\da-fA-F]{3}|#[\da-fA-F]{6})$/ ? : 3 0 3
1294 }
1295 2 ▶sub cleanup_attr_uri {
Subroutine cleanup_attr_uri: called CC=2 SCAR=7.6
Pod documented
1296 2 ▶ check_url_valid($_) ? $_ : undef;
Branch: 1 branch
BranchTrueFalseTotal
check_url_valid($_) ? : 2 0 2
1297 }
1298 2 ▶sub cleanup_attr_tframe {
Subroutine cleanup_attr_tframe: called CC=2 SCAR=6.9
Pod documented
1299 2 ▶ /^(void|above|below|hsides|lhs|rhs|vsides|box|border)$/i
Branch: 1 branch
BranchTrueFalseTotal
/^(void|above|below|hsides|lhs|rhs|vsides|box|border)$/i ? : 1 1 2
1300 ? lc $1 : undef;
1301 }
1302 2 ▶sub cleanup_attr_trules {
Subroutine cleanup_attr_trules: called CC=2 SCAR=6.9
Pod documented
1303 2 ▶ /^(none|groups|rows|cols|all)$/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(none|groups|rows|cols|all)$/i ? : 1 1 2
1304 }
1305  
1306 2 ▶sub cleanup_attr_scriptlang {
Subroutine cleanup_attr_scriptlang: called CC=2 SCAR=6.9
Pod documented
1307 2 ▶ /^(javascript)$/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
/^(javascript)$/i ? : 1 1 2
1308 }
1309 2 ▶sub cleanup_attr_scripttype {
Subroutine cleanup_attr_scripttype: called CC=2 SCAR=6.9
Pod documented
1310 2 ▶ /^(text\/javascript)$/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
m[^(text/javascript)$]i ? : 1 1 2
1311 }
1312  
1313 14 ▶use vars qw(@stack $safe_tags $convert_nl);
Subroutine BEGIN: called CC=1 SCAR=0.0
1314 7 ▶sub cleanup_html {
Subroutine cleanup_html: called CC=11 SCAR=28.4
Pod documented
1315 7 local ($_, $convert_nl, $safe_tags) = @_;
1316 7 local @stack = ();
1317  
1318 7 ▶ return '' unless($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 7 7
1319  
1320 7 my $ignore_comments = 0;
1321 7 ▶ if($ignore_comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore_comments) { } 0 7 7
1322 0 s[
1323 (?: <!--.*?--> ) |
1324 (?: <[?!].*?> ) |
1325 (?: <([a-z0-9]+)\b((?:[^>'"]|"[^"]*"|'[^']*')*)> ) |
1326 (?: </([a-z0-9]+)> ) |
1327 (?: (.[^<]*) )
1328 ][
1329 0 ▶ defined $1 ? cleanup_tag(lc $1, $2) :
Branch: 3 branches
BranchTrueFalseTotal
defined $1 ? : 0 0 0
defined $3 ? : 0 0 0
defined $4 ? : 0 0 0
1330 defined $3 ? cleanup_close(lc $3) :
1331 defined $4 ? cleanup_cdata($4) :
1332 ''
1333 ]igesx;
1334 } else {
1335 7 s[
1336 (?: (<!--.if.*?endif.-->) ) |
1337 (?: <!--.*?--> ) |
1338 (?: <[?!].*?> ) |
1339 (?: <([a-z0-9]+)\b((?:[^>'"]|"[^"]*"|'[^']*')*)> ) |
1340 (?: </([a-z0-9]+)> ) |
1341 (?: (.[^<]*) )
1342 ][
1343 19 ▶ defined $1 ? $1 :
Branch: 4 branches
BranchTrueFalseTotal
defined $1 ? : 0 19 19
defined $2 ? : 5 14 19
defined $4 ? : 5 9 14
defined $5 ? : 9 0 9
1344 defined $2 ? cleanup_tag(lc $2, $3) :
1345 defined $4 ? cleanup_close(lc $4) :
1346 defined $5 ? cleanup_cdata($5) :
1347 ''
1348 ]igesx;
1349 }
1350  
1351 # Close anything that was left open
1352 7 $_ .= join '', map "</$_->{NAME}>", @stack;
1353  
1354 # Where we turned <i><b>foo</i></b> into <i><b>foo</b></i><b></b>,
1355 # take out the pointless <b></b>.
1356 7 ▶ 1 while s#<($auto_deinterleave_pattern)\b[^>]*>(&nbsp;|\s)*</\1>##go;
Branch: 1 branch
BranchTrueFalseTotal
s[<($Labyrinth::MLUtils::auto_deinterleave_pattern)\b[^>]*>(&nbsp;|\s)*</\1>][]go and do { '???' } 0 7 7
1357  
1358 # cleanup p elements
1359 7 s!\s+</p>!</p>!g;
1360 7 s!<p></p>!!g;
1361  
1362 # Element pre is not declared in p list of possible children
1363 7 s!<p>\s*(<pre>.*?</pre>)\s*</p>!$1!g;
1364  
1365 7 return $_;
1366 }
1367  
1368 5 ▶sub cleanup_tag {
Subroutine cleanup_tag: called CC=15 SCAR=42.2
Pod documented
1369 5 my ($tag, $attrs) = @_;
1370 5 ▶ unless (exists $safe_tags->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Labyrinth::MLUtils::safe_tags->{$tag}) 0 5 5
1371 0 return '';
1372 }
1373  
1374 # for XHTML conformity
1375 5 ▶ $tag = $transpose_tag{$tag} if($transpose_tag{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::MLUtils::transpose_tag{$tag} 0 5 5
1376  
1377 5 my $html = '';
1378 5 ▶ if($force_closetag{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::MLUtils::force_closetag{$tag}) 5 0 5
1379 5 ▶ while (scalar @stack and $force_closetag{$tag}{$stack[0]{NAME}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::force_closetag{$tag}{$Labyrinth::MLUtils::stack[0]{"NAME"}}) 0 5 5
Condition33%1/3
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::force_closetag{$tag}{$Labyrinth::MLUtils::stack[0]{"NAME"}}
!ll&&!rl&&r
500
MC/DC0%0/2
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::force_closetag{$tag}{$Labyrinth::MLUtils::stack[0]{"NAME"}}
scalar @Labyrinth::MLUtils::stack $Labyrinth::MLUtils::force_closetag{$tag}{$Labyrinth::MLUtils::stack[0]{"NAME"}}
Truth table33%1/3
A and B
A= scalar @Labyrinth::MLUtils::stackB= $Labyrinth::MLUtils::force_closetag{$tag}{$Labyrinth::MLUtils::stack[0]{"NAME"}}
ABresult
0X0
100
111
1380 0 $html = cleanup_close($stack[0]{NAME});
1381 }
1382 }
1383  
1384 5 my $t = $safe_tags->{$tag};
1385 5 my $safe_attrs = '';
1386 5 ▶ while ($attrs =~ s#^\s*(\w+)(?:\s*=\s*(?:([^"'>\s]+)|"([^"]*)"|'([^']*)'))?##) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs =~ s/^\s*(\w+)(?:\s*=\s*(?:([^"'>\s]+)|"([^"]*)"|'([^']*)'))?//) 0 5 5
1387 0 my $attr = lc $1;
1388 0 ▶ my $val = ( defined $2 ? $2 :
Branch: 3 branches
BranchTrueFalseTotal
defined $2 ? : 0 0 0
defined $3 ? : 0 0 0
defined $4 ? : 0 0 0
1389 defined $3 ? unescape_html($3) :
1390 defined $4 ? unescape_html($4) :
1391 '$attr'
1392 );
1393 0 ▶ unless (exists $t->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $t->{$attr}) 0 0 0
1394 0 next;
1395 }
1396 0 ▶ if (defined $t->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->{$attr}) { } 0 0 0
1397 0 local $_ = $val;
1398 0 my $cleaned = &{ $t->{$attr} }();
1399 0 ▶ if (defined $cleaned) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cleaned) 0 0 0
1400 0 $safe_attrs .= qq| $attr="${\( escape_html($cleaned) )}"|;
1401 }
1402 } else {
1403 0 $safe_attrs .= " $attr";
1404 }
1405 }
1406  
1407 5 my $str;
1408 5 ▶ if (exists $tag_is_empty{$tag}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Labyrinth::MLUtils::tag_is_empty{$tag}) { } 0 5 5
elsif (exists $Labyrinth::MLUtils::closetag_is_optional{$tag}) { } 0 5 5
1409 0 $str = "$html<$tag$safe_attrs />";
1410 } elsif (exists $closetag_is_optional{$tag}) {
1411 0 $str = "$html<$tag$safe_attrs>";
1412 # } elsif (exists $closetag_is_dependent{$tag} && $safe_attrs =~ /$closetag_is_dependent{$tag}=/) {
1413 # return "$html<$tag$safe_attrs />";
1414 } else {
1415 5 my $full = "<$tag$safe_attrs>";
1416 5 unshift @stack, { NAME => $tag, FULL => $full };
1417 5 $str = "$html$full";
1418 }
1419 #LogDebug("cleanup_tag: str=$str");
1420 5 return $str;
1421 }
1422  
1423 5 ▶sub cleanup_close {
Subroutine cleanup_close: called CC=6 SCAR=21.5
Pod documented
1424 5 my $tag = shift;
1425  
1426 # for XHTML conformity
1427 5 ▶ $tag = $transpose_tag{$tag} if($transpose_tag{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::MLUtils::transpose_tag{$tag} 0 5 5
1428  
1429 # Ignore a close without an open
1430 5 ▶ unless (grep {$_->{NAME} eq $tag} @stack) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->{"NAME"} eq $tag;} @Labyrinth::MLUtils::stack) 0 5 5
1431 0 return '';
1432 }
1433  
1434 # Close open tags up to the matching open
1435 5 my @close = ();
1436 5 ▶ while (scalar @stack and $stack[0]{NAME} ne $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} ne $tag) 0 5 5
Condition33%1/3
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} ne $tag
!ll&&!rl&&r
050
MC/DC0%0/2
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} ne $tag
scalar @Labyrinth::MLUtils::stack $Labyrinth::MLUtils::stack[0]{"NAME"} ne $tag
Truth table33%1/3
A and B
A= scalar @Labyrinth::MLUtils::stackB= $Labyrinth::MLUtils::stack[0]{"NAME"} ne $tag
ABresult
0X0
100
111
1437 0 push @close, shift @stack;
1438 }
1439 5 push @close, shift @stack;
1440  
1441 5 my $html = join '', map {"</$_->{NAME}>"} @close;
1442  
1443 # Reopen any we closed early if all that were closed are
1444 # configured to be auto deinterleaved.
1445 5 ▶ unless (grep {! exists $auto_deinterleave{$_->{NAME}} } @close) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not exists $Labyrinth::MLUtils::auto_deinterleave{$_->{"NAME"}};} @close) 0 5 5
1446 0 pop @close;
1447 0 $html .= join '', map {$_->{FULL}} reverse @close;
1448 0 unshift @stack, @close;
1449 }
1450  
1451 5 return $html;
1452 }
1453  
1454 9 ▶sub cleanup_cdata {
Subroutine cleanup_cdata: called CC=6 SCAR=18.7
Pod documented
1455 9 local $_ = shift;
1456  
1457 9 ▶ return $_ if(scalar @stack and $stack[0]{NAME} eq 'script');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} eq "script" 0 9 9
Condition66%2/3
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} eq "script"
!ll&&!rl&&r
450
MC/DC0%0/2
scalar @Labyrinth::MLUtils::stack and $Labyrinth::MLUtils::stack[0]{"NAME"} eq "script"
scalar @Labyrinth::MLUtils::stack $Labyrinth::MLUtils::stack[0]{"NAME"} eq "script"
Truth table66%2/3
A and B
A= scalar @Labyrinth::MLUtils::stackB= $Labyrinth::MLUtils::stack[0]{"NAME"} eq "script"
ABresult
0X0
100
111
1458  
1459 9 s[ (?: & (
1460 [a-zA-Z0-9]{2,15} |
1461 [#][0-9]{2,6} |
1462 [#][xX][a-fA-F0-9]{2,6} | ) \b ;?
1463 ) | ($escape_html_map) | (.)
1464 ][
1465 15 ▶ defined $1 ? "&$1;" : defined $2 ? $2 : $3
Branch: 2 branches
BranchTrueFalseTotal
defined $1 ? : 4 11 15
defined $2 ? : 11 0 11
1466 ]gesx;
1467  
1468 # substitute newlines in the input for html line breaks if required.
1469 9 ▶ s%\cM?\n%<br />\n%g if $convert_nl;
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::MLUtils::convert_nl 0 9 9
1470  
1471 9 return $_;
1472 }
1473  
1474 # subroutine to escape the necessary characters to the appropriate HTML
1475 # entities
1476  
1477 2 ▶sub escape_html {
Subroutine escape_html: called CC=2 SCAR=6.9
Pod documented
1478 2 ▶ my $str = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or return "" 1 1 2
1479 1 $str = encode_entities($str);
1480 1 $str =~ s/&amp;(#x?\d+;)/&$1/g; # avoid double encoding of hex/dec characters
1481 1 return $str;
1482 }
1483  
1484 # subroutine to unescape escaped HTML entities. Note that some entites
1485 # have no 8-bit character equivalent, see
1486 # "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent" for some examples.
1487 # unescape_html() leaves these entities in their encoded form.
1488  
1489 2 ▶sub unescape_html {
Subroutine unescape_html: called CC=2 SCAR=6.9
Pod documented
1490 2 ▶ my $str = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or return "" 1 1 2
1491 1 $str = decode_entities($str);
1492 1 return strip_nonprintable($str);
1493 }
1494  
1495 4 ▶sub check_url_valid {
Subroutine check_url_valid: called CC=7 SCAR=21.5
Pod documented
1496 4 my $url = shift;
1497  
1498 4 ▶ $url = "$tvars{cgipath}/$tvars{script}$url" if($url =~ /^\?/);
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^\?/ 0 4 4
1499  
1500 # allow in page URLs
1501 4 ▶ return 1 if $url =~ m!^\#!;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^\#/ 0 4 4
1502  
1503 # allow relative URLs with sane values
1504 4 ▶ return 1 if $url =~ m!^[a-z0-9_\-\.\,\+\/#]+$!i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[^[a-z0-9_\-\.\,\+\/#]+$]i 0 4 4
1505  
1506 # allow mailto email addresses
1507 4 ▶ return 1 if $url =~ m#mailto:([-+=\w\'.\&\\//]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)#i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m(mailto:([-+=\w\'.\&\\//]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?))i 0 4 4
1508  
1509 # allow javascript calls
1510 4 ▶ return 1 if $url =~ m#^javascript:#i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^javascript:/i 0 4 4
1511  
1512 # $url =~ m< ^ ((?:ftp|http|https):// [\w\-\.]+ (?:\:\d+)?)?
1513 # (?: /? [\w\-.!~*'(|);/\@+\$,%#]* )?
1514 # (?: \? [\w\-.!~*'(|);/\@&=+\$,%#]* )?
1515 # $
1516 # >x ? 1 : 0;
1517 4 ▶ return $url =~ m< ^ $settings{urlregex} $ >x ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ / ^ $Labyrinth::MLUtils::settings{'urlregex'} $ /x ? : 4 0 4
1518 }
1519  
1520 1 ▶sub strip_nonprintable {
Subroutine strip_nonprintable: called CC=2 SCAR=7.0
Pod documented
1521 1 my $text = shift;
1522 1 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
1523  
1524 1 $text=~ tr#\t\n\040-\176\241-\377# #cs;
1525 1 return $text;
1526 }
1527  
1528 #
1529 # End of HTML handling code
1530 #
1531 ##################################################################
1532  
1533 14 1;
1534  
1535 __END__
« Labyrinth/DIUtils.pm ↑ Summary Labyrinth/Plugins.pm »