Debian Codegraph @

html/t/synopsis.t

raw
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 List::Util;
22 use Test::More;
23 use lib 'tool/lib';
24
25 BEGIN {
26 use lib 'html/tool/lib';
27 my $eval_result = eval { require Marpa::R2::HTML::Test::Util; 1 };
28 if ( !$eval_result ) {
29 Test::More::plan tests => 1;
30 Test::More::fail(
31 "Could not load Marpa::R2::HTML::Test::Util; $EVAL_ERROR");
32 exit 0;
33 } ## end if ( !$eval_result )
34 } ## end BEGIN
35
36 BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Parser'); }
37
38 BEGIN { use Test::More tests => 9; }
39
40 use Marpa::R2::Test;
41
42 # Marpa::R2::Display
43 # name: 'HTML Synopsis: Delete Tables'
44
45 use Marpa::R2::HTML qw(html);
46
47 my $with_table = 'Text<table><tr><td>I am a cell</table> More Text';
48 my $no_table = html( \$with_table,
49 { table => sub { return q{} } });
50
51 # Marpa::R2::Display::End
52
53 # Marpa::R2::Display
54 # name: 'HTML Synopsis: Delete Everything But Tables'
55
56 my %handlers_to_keep_only_tables = (
57 table => sub { return Marpa::R2::HTML::original() },
58 ':TOP' => sub { return \( join q{}, @{ Marpa::R2::HTML::values() } ) }
59 );
60 my $only_table = html( \$with_table, \%handlers_to_keep_only_tables );
61
62 # Marpa::R2::Display::End
63
64 # Marpa::R2::Display
65 # name: 'HTML Synopsis: Defective Tables'
66
67 my $with_bad_table = 'Text<tr>I am a cell</table> More Text';
68 my $only_bad_table = html( \$with_bad_table, \%handlers_to_keep_only_tables );
69
70 # Marpa::R2::Display::End
71
72 # Marpa::R2::Display
73 # name: 'HTML Synopsis: Delete Comments'
74
75 my $with_comment = 'Text <!-- I am a comment --> I am not a comment';
76 my $no_comment = html( \$with_comment,
77 { ':COMMENT' => sub { return q{} } }
78 );
79
80 # Marpa::R2::Display::End
81
82 # Marpa::R2::Display
83 # name: 'HTML Synopsis: Change Title'
84
85 my $old_title = '<title>Old Title</title>A little html text';
86 my $new_title = html(
87 \$old_title,
88 { 'title' => sub { return '<title>New Title</title>' }
89 }
90 );
91
92 # Marpa::R2::Display::End
93
94 # Marpa::R2::Display
95 # name: 'HTML Synopsis: Delete by Class'
96
97 my $stuff_to_be_edited = '<p>A<p class="delete_me">B<p>C';
98 my $edited_stuff = html( \$stuff_to_be_edited,
99 { '.delete_me' => sub { return q{} } });
100
101 # Marpa::R2::Display::End
102
103 # Marpa::R2::Display
104 # name: 'HTML Synopsis: Supply Missing Tags'
105
106 sub supply_missing_tags {
107 my $tagname = Marpa::R2::HTML::tagname();
108 return if Marpa::R2::HTML::is_empty_element($tagname);
109 return
110 ( Marpa::R2::HTML::start_tag() // "<$tagname>\n" )
111 . Marpa::R2::HTML::contents()
112 . ( Marpa::R2::HTML::end_tag() // "</$tagname>\n" );
113 } ## end sub supply_missing_tags
114 my $html_with_just_a_title = '<title>I am a title and That is IT!';
115 my $valid_html_with_all_tags =
116 html( \$html_with_just_a_title, { q{*} => \&supply_missing_tags } );
117
118 # Marpa::R2::Display::End
119
120 # Marpa::R2::Display
121 # name: 'HTML Synopsis: Maximum Element Depth'
122
123 sub depth_below_me {
124 return List::Util::max( 0, @{ Marpa::R2::HTML::values() } );
125 }
126 my %handlers_to_calculate_maximum_element_depth = (
127 q{*} => sub { return 1 + depth_below_me() },
128 ':TOP' => sub { return depth_below_me() },
129 );
130 my $maximum_depth_with_just_a_title = html( \$html_with_just_a_title,
131 \%handlers_to_calculate_maximum_element_depth );
132
133 # Marpa::R2::Display::End
134
135 my $maximum_depth_with_all_tags_supplied = html( $valid_html_with_all_tags,
136 \%handlers_to_calculate_maximum_element_depth );
137 Marpa::R2::Test::is( $maximum_depth_with_just_a_title,
138 3, 'compute maximum depth' );
139 Marpa::R2::Test::is(
140 $maximum_depth_with_just_a_title,
141 $maximum_depth_with_all_tags_supplied,
142 'compare maximum depths'
143 );
144
145 my $expected_valid_html_with_all_tags = <<'END_OF_EXPECTED';
146 <html>
147 <head>
148 <title>I am a title and That is IT!</title>
149 </head>
150 <body>
151 </body>
152 </html>
153 END_OF_EXPECTED
154
155 Marpa::R2::Test::is( ${$no_table}, 'Text More Text', 'delete tables' );
156 Marpa::R2::Test::is(
157 ${$only_table},
158 '<table><tr><td>I am a cell</table>',
159 'keep only tables'
160 );
161 Marpa::R2::Test::is(
162 ${$only_bad_table},
163 '<tr>I am a cell</table>',
164 'keep only tables -- bad table'
165 );
166 Marpa::R2::Test::is(
167 ${$no_comment},
168 'Text I am not a comment',
169 'delete comments'
170 );
171 Marpa::R2::Test::is(
172 ${$new_title},
173 '<title>New Title</title>A little html text',
174 'replace title'
175 );
176 Marpa::R2::Test::is( ${$edited_stuff}, '<p>A<p>C', 'delete by class name' );
177 Marpa::R2::Test::is(
178 ${$valid_html_with_all_tags},
179 $expected_valid_html_with_all_tags,
180 'supply tags'
181 );