Markdown/Perl/HTML.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 86.3% 19 / 22 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 7 totaltotal 87.4% 111 / 127 CC 17 SCAR 13.8
CC
17
Cov
93%
parse_attributes
7.1
convert_entity
6.0
parse_entities
4.1
CRAP 4.0
« Markdown/Perl/Util.pm ↑ Summary App/pmarkdown.pm »
1 package Markdown::Perl::HTML;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 33 ▶use Carp 'cluck';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 our @EXPORT_OK = qw(decode_entities html_escape http_escape remove_disallowed_tags
13 parse_attributes);
14 33 our %EXPORT_TAGS = (all => \@EXPORT_OK);
15  
16 33 our $VERSION = 0.01;
17  
18 33 our %html_entities;
19  
20 # Parse the __DATA__ section into the %html_entities hash.
21 # This is done lazily to reduce the loading time of the program if the table is
22 # not needed.
23 #
24 # TODO: add a :full_init import option to the main package, to force the early
25 # execution of this method.
26 16953 ▶sub parse_entities {
Subroutine parse_entities: called CC=4 SCAR=14.0
Pod undocumented
27 16953 ▶ return if %html_entities;
Branch: 1 branch
BranchTrueFalseTotal
if %Markdown::Perl::HTML::html_entities 16948 5 16953
28 5 local $INPUT_RECORD_SEPARATOR = undef;
29 5 %html_entities = eval <DATA>; ## no critic (ProhibitStringyEval);
30 5 ▶ confess $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $Markdown::Perl::HTML::EVAL_ERROR 0 5 5
31 5 ▶ close DATA or cluck 'Can’t close DATA file handle in lazy parsing of HTML entities mapping';
Branch: 1 branch
BranchTrueFalseTotal
close DATA or cluck("Can\x{2019}t close DATA file handle in lazy parsing of HTML entities mapping") 5 0 5
32 5 return;
33 }
34  
35 # Decode HTML entities like &amp; in the given string. Do it in-place if called
36 # in void-context otherwise returns a modified string.
37 # This is mostly equivalent to HTML::Entities::decode_entities but with our more
38 # complete list of entities, probably slower, and handling the replacement of
39 # 0x0 characters by 0xfffd.
40  
41 # Currently, CounterClockwiseContourIntegral is the longest entity name, at 31
42 # char. We limit our search to 40 char to limit possible backtracking in case of
43 # failures (even if the spec does not say anything about it).
44 33 my $numeric_entity_re = qr/(?:\#(?: (?<dec> [0-9]{1,7} ) | [xX] (?<hex> [a-fA-F0-9]{1,6} ) ))/x;
45 33 my $entity_re = qr/& (?: ${numeric_entity_re} | (?<named> [a-zA-Z0-9]{2,40} ) ) ; /x;
46  
47 16983 ▶sub convert_entity {
Subroutine convert_entity: called CC=6 SCAR=18.0
Pod undocumented
48 # the `|| 0xfffd` part is so that &#0; is correctly replaced by the 0xfffd
49 # "replacement" character (as is being done on the whole string at the
50 # beginning of its processing).
51 return
52 exists $+{dec} ? chr($+{dec} || 0xfffd)
53 : exists $+{hex} ? chr(hex($+{hex}) || 0xfffd)
54 16983 ▶ : (parse_entities(), $html_entities{$+{named}}) // "&$+{named};";
Branch: 2 branches
BranchTrueFalseTotal
exists $+{'dec'} ? : 22 16961 16983
exists $+{'hex'} ? : 8 16953 16961
Condition83%5/6
$+{'dec'} || 65533
l!l
193
hex $+{'hex'} || 65533
l!l
80
(parse_entities(), $Markdown::Perl::HTML::html_entities{$+{"named"}}) // qq[&$+{"named"};]
l!l
84088545
MC/DC100%1/1
$+{'dec'} || 65533
$+{'dec'}
MC/DC0%0/1
hex $+{'hex'} || 65533
hex $+{'hex'}
MC/DC100%1/1
(parse_entities(), $Markdown::Perl::HTML::html_entities{$+{"named"}}) // qq[&$+{"named"};]
(parse_entities(), $Markdown::Perl::HTML::html_entities{$+{"named"}})
Truth table100%2/2
A
A= $+{'dec'}
Aresult
11
00
Truth table50%1/2
A
A= hex $+{'hex'}
Aresult
11
00
Truth table100%2/2
A
A= (parse_entities(), $Markdown::Perl::HTML::html_entities{$+{"named"}})
Aresult
11
00
55 }
56  
57 112344 ▶sub decode_entities {
Subroutine decode_entities: called CC=2 SCAR=6.9
Pod undocumented
58 112344 ▶ return $_[0] =~ s/${entity_re}/&convert_entity/egr if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 13 112331 112344
59 112331 $_[0] =~ s/${entity_re}/&convert_entity/eg;
60 112331 return;
61 }
62  
63 # There are four characters that are escaped in the html output (although the
64 # spec never really says so because they claim that they care only about parsing).
65 33 my %char_to_html_entity = (
66 '"' => '&quot;',
67 "'" => '&apos;',
68 '&' => '&amp;',
69 '<' => '&lt;',
70 '>' => '&gt;'
71 );
72  
73 # html_escape($str_to_escape, $character_class_to_use_for_escaping)
74 160340 ▶sub html_escape {
Subroutine html_escape: called CC=1 SCAR=0.0
Pod undocumented
75 # TODO: I believe that this is re-compiled at each call because of the local
76 # variable. The regex should be built once in the Markdown::Perl object.
77 160340 $_[0] =~ s/([$_[1]])/$char_to_html_entity{$1}/eg;
78 160340 return;
79 }
80  
81 11459 ▶sub http_escape {
Subroutine http_escape: called CC=1 SCAR=0.0
Pod undocumented
82 11459 utf8::encode($_[0]);
83 # TODO: there are more characters that need to be escaped here, but it’s not
84 # extremely clear which ones (the spec does a show of pretending to care only
85 # only about parsing, but the conformance test suite expect something quite
86 # specific. See: https://spec.commonmark.org/0.31.2/#example-502
87 11459 $_[0] =~ s/([ \\\[\]\x80-\xff`""])/sprintf('%%%02X', ord($1))/ge;
88 11459 return;
89 }
90  
91 # remove_disallowed_tags($html, \@tag_list)
92 # modify $html in place.
93 17141 ▶sub remove_disallowed_tags {
Subroutine remove_disallowed_tags: called CC=2 SCAR=6.9
Pod undocumented
94 # TODO: check if GitHub also deactivates closing tags or only opening ones.
95 # Note: this is a dummy approach, based on the fact that this method is called
96 # only in the context of HTML blocks or inline HTML and so, something looking
97 # like an HTML tag is probably an HTML tag.
98 17141 for my $t (@{$_[1]}) {
99 33219 $_[0] =~ s/<(\Q$t\E)/&lt;$1/gi;
100 }
101 17141 return;
102 }
103  
104 # This parses the attributes of the directive syntax. E.g. it receives the
105 # attribute part in the :name[content]{attributes} syntax, where each part can
106 # be missing.
107 11 ▶sub parse_attributes {
Subroutine parse_attributes: called CC=7 SCAR=19.6
Pod undocumented
108 11 my ($attributes) = @_;
109 11 my %out;
110 11 my $id_re = qr/(?: \# (?<id> \w+ ))/x;
111 11 my $class_re = qr/(?: \. (?<class> \w+ ))/x;
112 11 my $key_value_re = qr/(?: (?<key> \w+ ) \s* = \s* (?<value> \w+ ))/x;
113 11 pos($attributes) = 0; # so that it’s set even if we don’t match once to avoid a warning later.
114 11 ▶ while ($attributes =~ m/\G\s* (?: ${id_re} | ${class_re} | ${key_value_re} ) /gcx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes =~ /\G\s* (?: $id_re | $class_re | $key_value_re ) /cgux) 14 11 25
115 14 ▶ if (defined ($+{id}) && !exists($out{id})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $+{'id'} and not exists $out{'id'}) { } 4 10 14
elsif (defined $+{'class'}) { } 8 2 10
elsif (defined $+{'key'}) { } 2 0 2
Condition66%2/3
defined $+{"id"} and not exists $out{"id"}
!ll&&!rl&&r
1004
MC/DC50%1/2
defined $+{"id"} and not exists $out{"id"}
defined $+{"id"} not exists $out{"id"}
Truth table66%2/3
A and not(B)
A= defined $+{"id"}B= not exists $out{"id"}
ABresult
0X0
100
111
116 4 $out{id} = $+{id};
117 } elsif (defined ($+{class})) {
118 8 push(@{$out{class}}, $+{class});
119 } elsif (defined ($+{key})) {
120 2 push(@{$out{keys}}, [$+{key}, $+{value}]);
121 } else {
122 0 pos($attributes) = $LAST_MATCH_START[0];
123 0 last;
124 }
125 }
126 11 ▶ if ($attributes !~ m/\G \s* $/x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attributes =~ /\G \s* $/ux) 3 8 11
127 3 $out{junk} = substr($attributes, pos($attributes));
128 }
129  
130 11 return %out;
131 }
132  
133 33 1;
134  
135 __DATA__
« Markdown/Perl/Util.pm ↑ Summary App/pmarkdown.pm »