blib/lib/HTML/Diff.pm

filter: Statementstmt 92.9% 92 / 99 Branchbran 95.8% 23 / 24 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 6 totaltotal 88.2% 143 / 162 CC 18 SCAR 17.2
CC
18
Cov
93%
html_word_diff
10.0
__ANON__
7.0
elem_cmprsn
2.0
CRAP 5.6
↑ Summary
1 package HTML::Diff;
2  
3 1 our $VERSION = '0.60';
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our @ISA = qw(Exporter);
11 1 our @EXPORT = qw(line_diff word_diff html_word_diff);
12  
13 # This list of tags is taken from the XHTML spec and includes
14 # all those for which no closing tag is expected. In addition
15 # the pattern below matches any tag which ends with a slash /
16  
17 1 our @UNBALANCED_TAGS = qw(br hr p li base basefont meta link
18 col colgroup frame input isindex area
19 embed img bgsound marquee);
20  
21 1 ▶use Algorithm::Diff 'sdiff';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 122 ▶sub member {
Subroutine member: called CC=1 SCAR=0.0
Pod undocumented
24 122 my ($item, @list) = @_;
25  
26 122 return scalar(grep {$_ eq $item} @list);
27 }
28  
29 10 ▶sub html_word_diff {
Subroutine html_word_diff: called CC=10 SCAR=23.0
Pod undocumented
30 10 my ($left, $right) = @_;
31  
32 # Split the two texts into words and tags.
33 10 my (@leftchks) = $left =~ m/(<[^>]*>\s*|[^<]+)/gm;
34 10 my (@rightchks) = $right =~ m/(<[^>]*>\s*|[^<]+)/gm;
35
36 10 ▶ @leftchks = map { $_ =~ /^<[^>]*>$/ ? $_ : ($_ =~ m/(\S+\s*)/gm) }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^<[^>]*>$/ ? : 62 87 149
37 @leftchks;
38 10 ▶ @rightchks = map { $_ =~ /^<[^>]*>$/ ? $_ : ($_ =~ m/(\S+\s*)/gm) }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^<[^>]*>$/ ? : 56 76 132
39 @rightchks;
40  
41 # Remove blanks; maybe the above regexes could handle this?
42 10 @leftchks = grep { $_ ne '' } @leftchks;
43 10 @rightchks = grep { $_ ne '' } @rightchks;
44  
45 # Now we process each segment by turning it into a pair. The first element
46 # is the text as we want it to read in the result. The second element is
47 # the value we will to use in comparisons. It contains an identifier
48 # for each of the balanced tags that it lies within.
49  
50 # This subroutine holds state in the tagstack variable
51 10 my $tagstack = [];
52 1119 ▶ my $smear_tags = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
53 1119 ▶ if ($_ =~ /^<.*>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^<.*>/) { } 160 959 1119
54 160 ▶ if ($_ =~ m|^</|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ m[^</]) { } 38 122 160
55 38 my ($tag) = m|^</\s*([^ \t\n\r>]*)|;
56 38 $tag = lc $tag;
57 # print STDERR "Found closer of $tag with " . (scalar @$tagstack) . " stack items\n";
58 # If we found the closer for the tag on top
59 # of the stack, pop it off.
60 38 ▶ if ((scalar @$tagstack) > 0 && $$tagstack[-1] eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$tagstack > 0 and $tagstack->[-1] eq $tag) 36 2 38
Condition66%2/3
scalar @$tagstack > 0 and $tagstack->[-1] eq $tag
!ll&&!rl&&r
2036
MC/DC50%1/2
scalar @$tagstack > 0 and $tagstack->[-1] eq $tag
scalar @$tagstack > 0 $tagstack->[-1] eq $tag
Truth table66%2/3
A and B
A= scalar @$tagstack > 0B= $tagstack->[-1] eq $tag
ABresult
0X0
100
111
61 36 my $stacktag = pop @$tagstack;
62 }
63 38 return [$_, $tag];
64 } else {
65 122 my ($tag) = m|^<\s*([^\s>]*)|;
66 122 $tag = lc $tag;
67 # print STDERR "Found opener of $tag with " . (scalar @$tagstack) . " stack items\n";
68 122 ▶ if (member($tag, @UNBALANCED_TAGS) || $tag =~ m#/\s*>$#)
Branch: 1 branch
BranchTrueFalseTotal
if (member($tag, @HTML::Diff::UNBALANCED_TAGS) or $tag =~ m[/\s*>$]) { } 86 36 122
Condition66%2/3
member($tag, @HTML::Diff::UNBALANCED_TAGS) or $tag =~ m[/\s*>$]
l!l&&r!l&&!r
86036
MC/DC50%1/2
member($tag, @HTML::Diff::UNBALANCED_TAGS) or $tag =~ m[/\s*>$]
member($tag, @HTML::Diff::UNBALANCED_TAGS) $tag =~ m[/\s*>$]
Truth table66%2/3
A or B
A= member($tag, @HTML::Diff::UNBALANCED_TAGS)B= $tag =~ m[/\s*>$]
ABresult
1X1
011
000
69 { # (tags without correspond closer tags)
70 86 return [$_, $tag];
71 } else {
72 36 push @$tagstack, $tag;
73 }
74 36 return [$_, $_];
75 }
76 } else {
77 959 my $result = [$_, (join "!!!", (@$tagstack, $_)) ];
78 959 return $result;
79 }
80 10 };
81  
82 # Now do the "smear tags" operation across each of the chunk-lists
83 10 $tagstack = [];
84 10 @leftchks = map { &$smear_tags } @leftchks;
85 # TBD: better modularity would preclude having to reset the stack
86 10 $tagstack = [];
87 10 @rightchks = map { &$smear_tags } @rightchks;
88  
89 # print STDERR Data::Dumper::Dumper(\@leftchks);
90 # print STDERR Data::Dumper::Dumper(\@rightchks);
91
92 # Now do the diff, using the "comparison" half of the pair to
93 # compare two chuncks.
94 my $chunks = sdiff(\@leftchks, \@rightchks,
95 10 ▶ sub { $_ = elem_cmprsn(shift); $_ =~ s/\s+$/ /g; $_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 # print STDERR Data::Dumper::Dumper($chunks);
98
99 # Finally, process the output of sdiff by concatenating
100 # consecutive chunks that were "unchanged."
101 10 my $lastsignal = '';
102 10 my $lbuf = "";
103 10 my $rbuf = "";
104 10 my @result;
105 my $ch;
106 10 foreach $ch (@$chunks) {
107 594 my ($signal, $left, $right) = @$ch;
108 594 ▶ if ($signal ne $lastsignal && $lastsignal ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal ne $lastsignal and $lastsignal ne "") 55 539 594
Condition100%3/3
$signal ne $lastsignal and $lastsignal ne ""
!ll&&!rl&&r
530955
MC/DC100%2/2
$signal ne $lastsignal and $lastsignal ne ""
$signal ne $lastsignal $lastsignal ne ""
Truth table100%3/3
A and B
A= $signal ne $lastsignalB= $lastsignal ne ""
ABresult
0X0
100
111
109 55 ▶ if ($signal ne 'u' && $lastsignal ne 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal ne 'u' and $lastsignal ne 'u') { } 7 48 55
Condition100%3/3
$signal ne "u" and $lastsignal ne "u"
!ll&&!rl&&r
25237
MC/DC100%2/2
$signal ne "u" and $lastsignal ne "u"
$signal ne "u" $lastsignal ne "u"
Truth table100%3/3
A and B
A= $signal ne "u"B= $lastsignal ne "u"
ABresult
0X0
100
111
110 7 $signal = 'c';
111 } else {
112 48 push @result, [$lastsignal, $lbuf, $rbuf];
113 48 $lbuf = "";
114 48 $rbuf = "";
115 }
116 }
117 # if ($signal eq 'u' && $lastsignal ne 'u') {
118 # push @result, [$lastsignal, $lbuf, $rbuf]
119 # unless $lastsignal eq '';
120 # $lbuf = "";
121 # $rbuf = "";
122 # } elsif ($signal ne 'u' && $lastsignal eq 'u') {
123 # push @result, [$lastsignal, $lbuf, $rbuf];
124 # $lbuf = "";
125 # $rbuf = "";
126 # }
127 594 my $lelem = elem_mkp($left);
128 594 my $relem = elem_mkp($right);
129 594 ▶ $lbuf .= (defined $lelem ? $lelem : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $lelem ? : 588 6 594
130 594 ▶ $rbuf .= (defined $relem ? $relem : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $relem ? : 531 63 594
131 594 $lastsignal = $signal;
132 }
133 10 push @result, [$lastsignal, $lbuf, $rbuf];
134 10 return \@result;
135 }
136  
137 # these are like "accessors" for the two halves of the diff-chunk pairs
138 1188 ▶sub elem_mkp {
Subroutine elem_mkp: called CC=2 SCAR=6.9
Pod undocumented
139 1188 my ($e) = @_;
140 1188 ▶ return undef unless ref $e eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "ARRAY" 69 1119 1188
141 1119 my ($mkp, $cmp) = @$e;
142 1119 return $mkp;
143 }
144  
145 1141 ▶sub elem_cmprsn {
Subroutine elem_cmprsn: called CC=2 SCAR=7.0
Pod undocumented
146 1141 my ($e) = @_;
147 1141 ▶ return undef unless ref $e eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "ARRAY" 0 1141 1141
148 1141 my ($mkp, $cmp) = @$e;
149 1141 return $cmp;
150 }
151  
152 # Finally a couple of non-HTML diff routines
153  
154 1 ▶sub line_diff {
Subroutine line_diff: called CC=1 SCAR=0.0
Pod undocumented
155 1 my ($left, $right) = @_;
156 1 my (@leftchks) = $left =~ m/(.*\n?)/gm;
157 1 my (@rightchks) = $right =~ m/(.*\n?)/gm;
158 1 my $result = sdiff(\@leftchks, \@rightchks);
159 # my @result = map { [ $_->[1], $_->[2] ] } @$result;
160 1 return $result;
161 }
162  
163 0 ▶sub word_diff {
Subroutine word_diff: not called CC=1 SCAR=6.9
Pod undocumented
164 0 my ($left, $right) = @_;
165 0 my (@leftchks) = $left =~ m/([^\s]*\s?)/gm;
166 0 my (@rightchks) = $right =~ m/([^\s]*\s?)/gm;
167  
168 0 my $result = sdiff(\@leftchks, \@rightchks);
169 0 my @result = (map { [ $_->[1], $_->[2] ] } @$result);
170 0 return $result;
171 }
172  
173 1 1;
174  
175 =head1 NAME
176  
177 HTML::Diff - compare two HTML strings and return a list of differences
178  
179 =head1 SYNOPSIS
180  
181 use HTML::Diff;
182  
183 $result = html_word_diff($left_text, $right_text);
184  
185 =head1 DESCRIPTION
186  
187 This module compares two strings of HTML and returns a list of a
188 chunks which indicate the diff between the two input strings, where
189 changes in formatting are considered changes.
190  
191 HTML::Diff does not strictly parse the HTML. Instead, it uses regular
192 expressions to make a decent effort at understanding the given HTML.
193 As a result, there are many valid HTML documents for which it will not
194 produce the correct answer. But there may be some invalid HTML
195 documents for which it gives you the answer you're looking for. Your
196 mileage may vary; test it on lots of inputs from your domain before
197 relying on it.
198  
199 Returns a reference to a list of triples [<flag>, <left>, <right>].
200 Each triple represents a check of the input texts. The flag tells you
201 whether it represents a deletion, insertion, a modification, or an
202 unchanged chunk.
203  
204 Every character of each input text is accounted for by some triple in
205 the output. Specifically, Concatenating all the <left> members from
206 the return value should produce C<$left_text>, and likewise the
207 <right> members concatenate together to produce C<$right_text>.
208  
209 The <flag> is either C<'u'>, C<'+'>, C<'-'>, or C<'c'>, indicating
210 whether the two chunks are the same, the $right_text contained this
211 chunk and the left chunk didn't, or vice versa, or the two chunks are
212 simply different. This follows the usage of Algorithm::Diff.
213  
214 The difference is computed on a word-by-word basis, "breaking" on
215 visible words in the HTML text. If a tag only is changed, it will not
216 be returned as an independent chunk but will be shown as a change to
217 one of the neighboring words. For balanced tags, such as <b> </b>, it
218 is intended that a change to the tag will be treated as a change to
219 all words in between.
220  
221 =head1 SEE ALSO
222  
223 L<Algorithm::Diff> provides the diff algorithm used in this module.
224  
225 L<XML::Diff> can generate a diff between two XML files, and also
226 patch an XML file, given a diff.
227  
228 =head1 REPOSITORY
229  
230 L<https://github.com/neilb/html-diff>
231  
232 =head1 AUTHOR
233  
234 Whipped up by Ezra elias kilty Cooper, E<lt>ezra@ezrakilty.netE<gt>.
235  
236 Patch contributed by Adam E<lt>asjo@koldfront.dkE<gt>.
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 Copyright 2003-2014 by Ezra elias kilty Cooper, E<lt>ezra@ezrakilty.netE<gt>
241  
242 This program is free software; you can redistribute it and/or
243 modify it under the same terms as Perl itself.
244  
245 =cut
↑ Summary