| 1 |
#!perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use English qw( -no_match_vars ); |
| 21 |
use Test::More; |
| 22 |
|
| 23 |
BEGIN { |
| 24 |
use lib 'html/tool/lib'; |
| 25 |
my $eval_result = eval { require Marpa::R2::HTML::Test::Util; 1 }; |
| 26 |
if ( !$eval_result ) { |
| 27 |
Test::More::plan tests => 1; |
| 28 |
Test::More::fail( |
| 29 |
"Could not load Marpa::R2::HTML::Test::Util; $EVAL_ERROR"); |
| 30 |
exit 0; |
| 31 |
} ## end if ( !$eval_result ) |
| 32 |
} ## end BEGIN |
| 33 |
|
| 34 |
BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Parser'); } |
| 35 |
BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Entities'); } |
| 36 |
|
| 37 |
# These tests are based closely on those in the HTML-Tree module, |
| 38 |
# the authors of which I gratefully acknowledge. |
| 39 |
|
| 40 |
Test::More::plan tests => 40; |
| 41 |
my $DEBUG = 2; |
| 42 |
|
| 43 |
use Marpa::R2::HTML; |
| 44 |
|
| 45 |
my $html_args = { |
| 46 |
':CRUFT' => sub { |
| 47 |
my $literal = Marpa::R2::HTML::literal(); |
| 48 |
say {*STDERR} 'Cruft: ', $literal |
| 49 |
or Carp::croak("Cannot print: $ERRNO"); |
| 50 |
return qq{<CRUFT literal="$literal">}; |
| 51 |
}, |
| 52 |
':PCDATA' => sub { |
| 53 |
my $literal = Marpa::R2::HTML::literal(); |
| 54 |
if ( defined &HTML::Entities::decode_entities ) { |
| 55 |
$literal = |
| 56 |
HTML::Entities::encode_entities( |
| 57 |
HTML::Entities::decode_entities($literal) ); |
| 58 |
} |
| 59 |
return $literal; |
| 60 |
}, |
| 61 |
':PROLOG' => sub { |
| 62 |
my $literal = Marpa::R2::HTML::literal(); |
| 63 |
$literal =~ s/\A [\x{20}\t\f\x{200B}]+ //xms; |
| 64 |
$literal =~ s/ [\x{20}\t\f\x{200B}]+ \z//xms; |
| 65 |
return $literal; |
| 66 |
}, |
| 67 |
':COMMENT' => sub { return q{} }, |
| 68 |
q{*} => sub { |
| 69 |
my $tagname = Marpa::R2::HTML::tagname(); |
| 70 |
|
| 71 |
# say STDERR "In handler for $tagname element"; |
| 72 |
|
| 73 |
Carp::croak('Not in an element') if not $tagname; |
| 74 |
my $attributes = Marpa::R2::HTML::attributes(); |
| 75 |
|
| 76 |
# Note this logic suffices to get through |
| 77 |
# the test set but it does not handle |
| 78 |
# the necessary escaping for a production |
| 79 |
# version |
| 80 |
my $start_tag = "<$tagname"; |
| 81 |
for my $attribute ( sort keys %{$attributes} ) { |
| 82 |
$start_tag |
| 83 |
.= qq{ $attribute="} . $attributes->{$attribute} . q{"}; |
| 84 |
} |
| 85 |
$start_tag .= '>'; |
| 86 |
my $end_tag = "</$tagname>"; |
| 87 |
|
| 88 |
my $descendant_data = |
| 89 |
|
| 90 |
# Marpa::R2::Display |
| 91 |
# name: dataspec example |
| 92 |
|
| 93 |
Marpa::R2::HTML::descendants('token_type,literal,element') |
| 94 |
|
| 95 |
# Marpa::R2::Display::End |
| 96 |
|
| 97 |
; # semi to end $descendant_data definition |
| 98 |
|
| 99 |
# For UL element, eliminate all but the LI element children |
| 100 |
if ( $tagname eq 'ul' ) { |
| 101 |
$descendant_data = |
| 102 |
[ grep { defined $_->[2] and $_->[2] eq 'li' } |
| 103 |
@{$descendant_data} ]; |
| 104 |
} |
| 105 |
|
| 106 |
my @contents = (); |
| 107 |
DATUM: for my $descendant_datum (@{$descendant_data}) { |
| 108 |
my $token_type = $descendant_datum->[0]; |
| 109 |
if ( not defined $token_type ) { |
| 110 |
push @contents, $descendant_datum->[1]; |
| 111 |
next DATUM; |
| 112 |
} |
| 113 |
next DATUM if $token_type eq 'S'; |
| 114 |
next DATUM if $token_type eq 'E'; |
| 115 |
push @contents, $descendant_datum->[1]; |
| 116 |
} ## end for my $descendant_datum (@descendant_data) |
| 117 |
my $contents = join q{}, @contents; |
| 118 |
$contents =~ s/\A [\x{20}\t\f\x{200B}]+ //xms; |
| 119 |
$contents =~ s/ [\x{20}\t\f\x{200B}]+ \z//xms; |
| 120 |
return join q{}, $start_tag, $contents, $end_tag; |
| 121 |
}, |
| 122 |
}; |
| 123 |
|
| 124 |
Test::More::ok 1; |
| 125 |
|
| 126 |
Test::More::ok same( |
| 127 |
'<title>foo</title><p>I like pie', |
| 128 |
'<html><head><title>foo</title></head><body><p>I like pie</p></body></html>' |
| 129 |
); |
| 130 |
|
| 131 |
Test::More::ok !same( 'x' => 'y', 1 ); |
| 132 |
Test::More::ok !same( '<p>' => 'y', 1 ); |
| 133 |
|
| 134 |
Test::More::ok same( q{} => q{} ); |
| 135 |
Test::More::ok same( q{} => q{ } ); |
| 136 |
Test::More::ok same( q{} => q{ } ); |
| 137 |
|
| 138 |
Test::More::ok same( q{} => '<!-- tra la la -->' ); |
| 139 |
Test::More::ok same( q{} => '<!-- tra la la --><!-- foo -->' ); |
| 140 |
|
| 141 |
Test::More::ok same( q{} => \'<head></head><body></body>' ); |
| 142 |
|
| 143 |
Test::More::ok same( '<head>' => q{} ); |
| 144 |
|
| 145 |
Test::More::ok same( '<head></head><body>' => \'<head></head><body></body>' ); |
| 146 |
|
| 147 |
Test::More::ok same( |
| 148 |
'<img alt="456" src="123">' => '<img src="123" alt="456">' ); |
| 149 |
Test::More::ok same( |
| 150 |
'<img alt="456" src="123">' => '<img src="123" alt="456">' ); |
| 151 |
Test::More::ok same( |
| 152 |
'<img alt="456" src="123">' => '<img src="123" alt="456" >' ); |
| 153 |
|
| 154 |
Test::More::ok !same( |
| 155 |
'<img alt="456" >' => '<img src="123" alt="456" >', |
| 156 |
1 |
| 157 |
); |
| 158 |
|
| 159 |
SKIP: { |
| 160 |
## no critic (ValuesAndExpressions::ProhibitMagicNumbers) |
| 161 |
defined &HTML::Entities::decode_entities |
| 162 |
or Test::More::skip 'HTML::Entities not installed', 6; |
| 163 |
## use critic |
| 164 |
|
| 165 |
Test::More::ok same( 'abc xyz' => 'abc xyz' ); |
| 166 |
Test::More::ok same( 'abc xyz' => 'abc xyz' ); |
| 167 |
|
| 168 |
Test::More::ok same( 'abc+xyz' => 'abc+xyz' ); |
| 169 |
Test::More::ok same( 'abc+xyz' => 'abc+xyz' ); |
| 170 |
|
| 171 |
Test::More::ok same( 'abc+xyz' => 'abc+xyz' ); |
| 172 |
Test::More::ok same( 'abc+xyz' => 'abc+xyz' ); |
| 173 |
|
| 174 |
} ## end SKIP: |
| 175 |
|
| 176 |
# Now some list tests. |
| 177 |
|
| 178 |
Test::More::ok same( '<ul><li>x</ul>after' => '<ul><li>x</li></ul>after' ); |
| 179 |
Test::More::ok same( |
| 180 |
'<ul><li>x<li>y</ul>after' => '<ul><li>x</li><li>y</li></ul>after' ); |
| 181 |
|
| 182 |
Test::More::ok same( '<ul> <li>x</li> <li>y</li> </ul>after' => |
| 183 |
'<ul><li>x</li><li>y</li></ul>after' ); |
| 184 |
|
| 185 |
Test::More::ok same( '<ul><li>x<li>y</ul>after' => \ |
| 186 |
'<head></head><body><ul><li>x</li><li>y</li></ul>after</body>' ); |
| 187 |
|
| 188 |
# Now some table tests. |
| 189 |
|
| 190 |
Test::More::ok same( '<table>x<td>y<td>z' => |
| 191 |
'<table><tr><td>x</td><td>y</td><td>z</td></table>' ); |
| 192 |
|
| 193 |
Test::More::ok same( '<table>x<td>y<tr>z' => |
| 194 |
'<table><tr><td>x</td><td>y</td></tr><tr><td>z</td></tr></table>' ); |
| 195 |
|
| 196 |
Test::More::ok same( |
| 197 |
'<table><tr><td>x</td><td>y</td></tr><tr><td>z</td></tr></table>' => |
| 198 |
'<table><tr><td>x</td><td>y</td></tr><tr><td>z</td></tr></table>' ); |
| 199 |
Test::More::ok same( |
| 200 |
'<table><tr><td>x</td><td>y</td></tr><tr><td>z</td></tr></table>' => \ |
| 201 |
'<head></head><body><table><tr><td>x</td><td>y</td></tr><tr><td>z</td></tr></table>' |
| 202 |
); |
| 203 |
|
| 204 |
Test::More::ok same( '<table>x' => '<td>x' ); |
| 205 |
Test::More::ok same( '<table>x' => '<table><td>x' ); |
| 206 |
Test::More::ok same( '<table>x' => '<tr>x' ); |
| 207 |
Test::More::ok same( '<table>x' => '<tr><td>x' ); |
| 208 |
Test::More::ok same( '<table>x' => '<table><tr>x' ); |
| 209 |
Test::More::ok same( '<table>x' => '<table><tr><td>x' ); |
| 210 |
|
| 211 |
# Now some p tests. |
| 212 |
|
| 213 |
Test::More::ok same( '<p>x<p>y<p>z' => '<p>x</p><p>y</p><p>z' ); |
| 214 |
Test::More::ok same( '<p>x<p>y<p>z' => '<p>x</p><p>y<p>z</p>' ); |
| 215 |
Test::More::ok same( '<p>x<p>y<p>z' => '<p>x</p><p>y</p><p>z</p>' ); |
| 216 |
Test::More::ok same( |
| 217 |
'<p>x<p>y<p>z' => \'<head></head><body><p>x</p><p>y</p><p>z</p>' ); |
| 218 |
|
| 219 |
sub same { |
| 220 |
my ( $code1, $code2, $flip ) = @_; |
| 221 |
|
| 222 |
if ( ref $code1 ) { $code1 = ${$code1} } |
| 223 |
if ( ref $code2 ) { $code2 = ${$code2} } |
| 224 |
|
| 225 |
my $value1; |
| 226 |
if (not eval { $value1 = Marpa::R2::HTML::html( \$code1, $html_args ); 1 } |
| 227 |
) |
| 228 |
{ |
| 229 |
say "No parse for $code1; $EVAL_ERROR" |
| 230 |
or Carp::croak("Cannot print: $ERRNO"); |
| 231 |
return $flip; |
| 232 |
} ## end if ( not eval { $value1 = Marpa::R2::HTML::html( \$code1...)}) |
| 233 |
|
| 234 |
my $value2; |
| 235 |
if (not eval { $value2 = Marpa::R2::HTML::html( \$code2, $html_args ); 1 } |
| 236 |
) |
| 237 |
{ |
| 238 |
say "No parse for $code2; $EVAL_ERROR" |
| 239 |
or Carp::croak("Cannot print: $ERRNO"); |
| 240 |
return $flip; |
| 241 |
} ## end if ( not eval { $value2 = Marpa::R2::HTML::html( \$code2...)}) |
| 242 |
|
| 243 |
my $out1 = ${$value1}; |
| 244 |
my $out2 = ${$value2}; |
| 245 |
|
| 246 |
my $rv = ( $out1 eq $out2 ); |
| 247 |
|
| 248 |
if ( $flip ? ( !$rv ) : $rv ) { |
| 249 |
if ( $DEBUG > 2 ) { |
| 250 |
print |
| 251 |
"In1 $code1\n", |
| 252 |
"In2 $code2\n", "Out1 $out1\n", "Out2 $out2\n", "\n\n" |
| 253 |
or Carp::croak("Cannot print: $ERRNO"); |
| 254 |
} ## end if ( $DEBUG > 2 ) |
| 255 |
} ## end if ( $flip ? ( !$rv ) : $rv ) |
| 256 |
else { |
| 257 |
print '# The following failure is at ' . join( ' : ', caller ), "\n", |
| 258 |
'# Explanation of failure: ' |
| 259 |
. ( $flip ? 'same' : 'different' ) |
| 260 |
. ' parse trees!', "\n", |
| 261 |
'# Input code 1:', $code1, "\n", |
| 262 |
'# Input code 2:', $code2, "\n", |
| 263 |
'# Output tree (as XML) 1:', $out1, "\n", |
| 264 |
'# Output tree (as XML) 2:', $out2, "\n", |
| 265 |
or Carp::croak("Cannot print: $ERRNO"); |
| 266 |
} ## end else [ if ( $flip ? ( !$rv ) : $rv ) ] |
| 267 |
|
| 268 |
return $rv; |
| 269 |
} ## end sub same |
| 270 |
|
| 271 |
# vim: set expandtab shiftwidth=4: |