Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29f7404

Browse files
committed
Switched from "ins" and "del" span classes to "ins" and "del" XHTML elements.
1 parent c7dbcfe commit 29f7404

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

Changes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Revision history for Perl extension Text::Diff::HTML.
22

33
0.04
4+
- Swiched from <span class="ins"> and <span class="del"> to <ins>
5+
and <del> elements. Why didn't I notice those before? Sorry for yet
6+
another change in the XHTML, but this is more semantic, and I there-
7+
fore don't expect to change it again. Suggested by Lucas Carlson.
8+
- Rejiggered sample CSS in "eg/diff.css" for new XHTML elements and
9+
shorter background color specs.
410

511
0.03 2005-09-05T18:40:29
612
- Improved CSS examples in "eg/diff.css" by using more specific

README

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ Description
2121
by "diff()" are wrapped in a "<div>" element, as is each hunk of the
2222
diff. Within each hunk, all content is properly HTML encoded using
2323
HTML::Entities, and the various sections of the diff are marked up with
24-
"<span>" elements. Each "<div>" and "<span>" element has a class,
25-
defined as follows:
24+
the appropriate XHTML elements. The elements used are as follows:
2625

2726
* "<div class="file">"
2827
This element contains the entire contents of the diff "file"
@@ -57,22 +56,22 @@ Description
5756
are contents that have *not* changed between the files being
5857
"diff"ed.
5958

60-
* "<span class="ins">"
61-
An insertion line, starting with "+".
59+
* "<ins>"
60+
Inserted content, each line starting with "+".
6261

63-
* "<span class="del">"
64-
A deletion line, starting with "-".
62+
* "<del>"
63+
Deleted content, each line starting with "-".
6564

6665
* "<span class="hunkfooter">"
6766
The footer section of a hunk; contains no contents.
6867

6968
* "<span class="filefooter">"
7069
The footer section of a file; contains no contents.
7170

72-
You may do whatever you like with these classes; I highly recommend that
73-
you style them using CSS. You'll find an example CSS file in the eg
74-
directory in the Text-Diff-HTML distribution. You will also likely want
75-
to wrap the output of your diff a "<pre>" element.
71+
You may do whatever you like with these elements and classes; I highly
72+
recommend that you style them using CSS. You'll find an example CSS file
73+
in the eg directory in the Text-Diff-HTML distribution. You will also
74+
likely want to wrap the output of your diff a "<pre>" element.
7675

7776
See Also
7877
Text::Diff

eg/diff.css

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.file span { display: block; }
2-
.file .fileheader, .file .hunkheader {color: #888888; }
3-
.file .hunk .ctx { background: #eeeeee; }
4-
.file .hunk .ins { background: #ddffdd; }
5-
.file .hunk .del { background: #ffdddd; }
6-
2+
.file .fileheader, .file .hunkheader {color: #888; }
3+
.file .hunk .ctx { background: #eee;}
4+
.file .hunk ins { background: #dfd; text-decoration: none; display: block; }
5+
.file .hunk del { background: #fdd; text-decoration: none; display: block; }

lib/Text/Diff/HTML.pm

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ use constant SEQ_A_IDX => 0;
5757
use constant SEQ_B_IDX => 1;
5858

5959
my %code_map = (
60-
'+' => 'ins',
61-
'-' => 'del',
62-
' ' => 'ctx',
60+
'+' => [ 'ins' => 'ins' ],
61+
'-' => [ 'del' => 'del' ],
62+
' ' => [ 'span class="ctx"' => 'span' ]
6363
);
6464

6565
sub hunk {
@@ -70,25 +70,25 @@ sub hunk {
7070

7171
# Start the span element for the first opcode.
7272
my $last = $ops->[0][ OPCODE ];
73-
my $hunk = qq{<span class="$code_map{ $last }">};
73+
my $hunk = qq{<$code_map{ $last }->[0]>};
7474

7575
# Output each line of the hunk.
7676
while (my $op = shift @$ops) {
7777
my $opcode = $op->[OPCODE];
78-
my $class = $code_map{ $opcode } or next;
78+
my $elem = $code_map{ $opcode } or next;
7979

8080
# Close the last span and start a new one for a new opcode.
8181
if ($opcode ne $last) {
82+
$hunk .= "</$code_map{ $last }->[1]><$elem->[0]>";
8283
$last = $opcode;
83-
$hunk .= qq{</span><span class="$class">};
8484
}
8585

8686
# Output the appropriate line.
8787
my $idx = $opcode ne '+' ? SEQ_A_IDX : SEQ_B_IDX;
8888
$hunk .= encode_entities("$opcode $seqs->[$idx][$op->[$idx]]");
8989
}
9090

91-
return $hunk . '</span>';
91+
return $hunk . "</$code_map{ $last }->[1]>";
9292
}
9393

9494
1;
@@ -133,8 +133,8 @@ In the XHTML formatted by this module, the contents of the diff returned by
133133
C<diff()> are wrapped in a C<< <div> >> element, as is each hunk of the diff.
134134
Within each hunk, all content is properly HTML encoded using
135135
L<HTML::Entities|HTML::Entities>, and the various sections of the diff are
136-
marked up with C<< <span> >> elements. Each C<< <div> >> and C<< <span> >>
137-
element has a class, defined as follows:
136+
marked up with the appropriate XHTML elements. The elements used are as
137+
follows:
138138
139139
=over
140140
@@ -174,13 +174,13 @@ This element immediately follows the opening "hunk" C<< <div> >> element.
174174
Context around the important part of a C<diff> hunk. These are contents that
175175
have I<not> changed between the files being C<diff>ed.
176176
177-
=item * C<< <span class="ins"> >>
177+
=item * C<< <ins> >>
178178
179-
An insertion line, starting with C<+>.
179+
Inserted content, each line starting with C<+>.
180180
181-
=item * C<< <span class="del"> >>
181+
=item * C<< <del> >>
182182
183-
A deletion line, starting with C<->.
183+
Deleted content, each line starting with C<->.
184184
185185
=item * C<< <span class="hunkfooter"> >>
186186
@@ -196,10 +196,10 @@ The footer section of a file; contains no contents.
196196
197197
=back
198198
199-
You may do whatever you like with these classes; I highly recommend that you
200-
style them using CSS. You'll find an example CSS file in the F<eg> directory
201-
in the Text-Diff-HTML distribution. You will also likely want to wrap the
202-
output of your diff a C<< <pre> >> element.
199+
You may do whatever you like with these elements and classes; I highly
200+
recommend that you style them using CSS. You'll find an example CSS file in
201+
the F<eg> directory in the Text-Diff-HTML distribution. You will also likely
202+
want to wrap the output of your diff a C<< <pre> >> element.
203203
204204
=head1 See Also
205205

t/base.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ my @ops = (
6363
is $html_diff->hunk(\@file_one, \@file_two, \@ops),
6464
qq{<span class="ctx"> This is the first file. We this line will be changed.\n}
6565
. qq{ This one will stay &lt;em&gt;the same.&lt;/em&gt;\n}
66-
. qq{</span><span class="del">- And so will this one.\n}
67-
. qq{</span><span class="ins">+ But only the second file will have this line.\n}
68-
. qq{</span>},
66+
. qq{</span><del>- And so will this one.\n}
67+
. qq{</del><ins>+ But only the second file will have this line.\n}
68+
. qq{</ins>},
6969
'hunk() should give us what we expect.';

0 commit comments

Comments
 (0)