1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#!perl
# Copyright 2022 Jeffrey Kegler
# This file is part of Marpa::R2. Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2. If not, see
# http://www.gnu.org/licenses/.
use 5.010001;
use strict;
use warnings;
use English qw( -no_match_vars );
use List::Util;
use Test::More;
use lib 'tool/lib';
BEGIN {
use lib 'html/tool/lib';
my $eval_result = eval { require Marpa::R2::HTML::Test::Util; 1 };
if ( !$eval_result ) {
Test::More::plan tests => 1;
Test::More::fail(
"Could not load Marpa::R2::HTML::Test::Util; $EVAL_ERROR");
exit 0;
} ## end if ( !$eval_result )
} ## end BEGIN
BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Parser'); }
BEGIN { use Test::More tests => 9; }
use Marpa::R2::Test;
# Marpa::R2::Display
# name: 'HTML Synopsis: Delete Tables'
use Marpa::R2::HTML qw(html);
my $with_table = 'Text<table><tr><td>I am a cell</table> More Text';
my $no_table = html( \$with_table,
{ table => sub { return q{} } });
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Delete Everything But Tables'
my %handlers_to_keep_only_tables = (
table => sub { return Marpa::R2::HTML::original() },
':TOP' => sub { return \( join q{}, @{ Marpa::R2::HTML::values() } ) }
);
my $only_table = html( \$with_table, \%handlers_to_keep_only_tables );
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Defective Tables'
my $with_bad_table = 'Text<tr>I am a cell</table> More Text';
my $only_bad_table = html( \$with_bad_table, \%handlers_to_keep_only_tables );
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Delete Comments'
my $with_comment = 'Text <!-- I am a comment --> I am not a comment';
my $no_comment = html( \$with_comment,
{ ':COMMENT' => sub { return q{} } }
);
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Change Title'
my $old_title = '<title>Old Title</title>A little html text';
my $new_title = html(
\$old_title,
{ 'title' => sub { return '<title>New Title</title>' }
}
);
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Delete by Class'
my $stuff_to_be_edited = '<p>A<p class="delete_me">B<p>C';
my $edited_stuff = html( \$stuff_to_be_edited,
{ '.delete_me' => sub { return q{} } });
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Supply Missing Tags'
sub supply_missing_tags {
my $tagname = Marpa::R2::HTML::tagname();
return if Marpa::R2::HTML::is_empty_element($tagname);
return
( Marpa::R2::HTML::start_tag() // "<$tagname>\n" )
. Marpa::R2::HTML::contents()
. ( Marpa::R2::HTML::end_tag() // "</$tagname>\n" );
} ## end sub supply_missing_tags
my $html_with_just_a_title = '<title>I am a title and That is IT!';
my $valid_html_with_all_tags =
html( \$html_with_just_a_title, { q{*} => \&supply_missing_tags } );
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: 'HTML Synopsis: Maximum Element Depth'
sub depth_below_me {
return List::Util::max( 0, @{ Marpa::R2::HTML::values() } );
}
my %handlers_to_calculate_maximum_element_depth = (
q{*} => sub { return 1 + depth_below_me() },
':TOP' => sub { return depth_below_me() },
);
my $maximum_depth_with_just_a_title = html( \$html_with_just_a_title,
\%handlers_to_calculate_maximum_element_depth );
# Marpa::R2::Display::End
my $maximum_depth_with_all_tags_supplied = html( $valid_html_with_all_tags,
\%handlers_to_calculate_maximum_element_depth );
Marpa::R2::Test::is( $maximum_depth_with_just_a_title,
3, 'compute maximum depth' );
Marpa::R2::Test::is(
$maximum_depth_with_just_a_title,
$maximum_depth_with_all_tags_supplied,
'compare maximum depths'
);
my $expected_valid_html_with_all_tags = <<'END_OF_EXPECTED';
<html>
<head>
<title>I am a title and That is IT!</title>
</head>
<body>
</body>
</html>
END_OF_EXPECTED
Marpa::R2::Test::is( ${$no_table}, 'Text More Text', 'delete tables' );
Marpa::R2::Test::is(
${$only_table},
'<table><tr><td>I am a cell</table>',
'keep only tables'
);
Marpa::R2::Test::is(
${$only_bad_table},
'<tr>I am a cell</table>',
'keep only tables -- bad table'
);
Marpa::R2::Test::is(
${$no_comment},
'Text I am not a comment',
'delete comments'
);
Marpa::R2::Test::is(
${$new_title},
'<title>New Title</title>A little html text',
'replace title'
);
Marpa::R2::Test::is( ${$edited_stuff}, '<p>A<p>C', 'delete by class name' );
Marpa::R2::Test::is(
${$valid_html_with_all_tags},
$expected_valid_html_with_all_tags,
'supply tags'
);
|