| 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 |
|
| 24 |
BEGIN { |
| 25 |
use lib 'html/tool/lib'; |
| 26 |
my $eval_result = eval { require Marpa::R2::HTML::Test::Util; 1 }; |
| 27 |
if ( !$eval_result ) { |
| 28 |
Test::More::plan tests => 1; |
| 29 |
Test::More::fail( |
| 30 |
"Could not load Marpa::R2::HTML::Test::Util; $EVAL_ERROR"); |
| 31 |
exit 0; |
| 32 |
} ## end if ( !$eval_result ) |
| 33 |
} ## end BEGIN |
| 34 |
|
| 35 |
BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Parser'); } |
| 36 |
|
| 37 |
BEGIN { Test::More::plan tests => 2; } |
| 38 |
|
| 39 |
use Marpa::R2::HTML; |
| 40 |
use lib 'tool/lib'; |
| 41 |
use Marpa::R2::Test; |
| 42 |
|
| 43 |
# Non-synopsis example in HTML.pod |
| 44 |
|
| 45 |
# Marpa::R2::Display |
| 46 |
# name: 'HTML Pod: Handler Precedence' |
| 47 |
|
| 48 |
my $html = <<'END_OF_HTML'; |
| 49 |
<span class="high">High Span</span> |
| 50 |
<span class="low">Low Span</span> |
| 51 |
<div class="high">High Div</div> |
| 52 |
<div class="low">Low Div</div> |
| 53 |
<div class="oddball">Oddball Div</div> |
| 54 |
END_OF_HTML |
| 55 |
|
| 56 |
our @RESULTS = (); |
| 57 |
Marpa::R2::HTML::html( |
| 58 |
\$html, |
| 59 |
{ q{*} => sub { |
| 60 |
push @RESULTS, 'wildcard handler: ' . Marpa::R2::HTML::contents(); |
| 61 |
}, |
| 62 |
'div' => sub { |
| 63 |
push @RESULTS, '"div" handler: ' . Marpa::R2::HTML::contents(); |
| 64 |
}, |
| 65 |
'.high' => sub { |
| 66 |
push @RESULTS, '".high" handler: ' . Marpa::R2::HTML::contents(); |
| 67 |
}, |
| 68 |
'div.high' => sub { |
| 69 |
push @RESULTS, |
| 70 |
'"div.high" handler: ' . Marpa::R2::HTML::contents(); |
| 71 |
}, |
| 72 |
'.oddball' => sub { |
| 73 |
push @RESULTS, |
| 74 |
'".oddball" handler: ' . Marpa::R2::HTML::contents(); |
| 75 |
}, |
| 76 |
'body' => sub {undef}, |
| 77 |
'head' => sub {undef}, |
| 78 |
'html' => sub {undef}, |
| 79 |
'p' => sub {undef}, |
| 80 |
} |
| 81 |
); |
| 82 |
|
| 83 |
# Marpa::R2::Display::End |
| 84 |
|
| 85 |
# Marpa::R2::Display |
| 86 |
# name: 'HTML Pod: Handler Precedence Result' |
| 87 |
# start-after-line: EXPECTED_RESULT |
| 88 |
# end-before-line: '^EXPECTED_RESULT$' |
| 89 |
|
| 90 |
my $expected_result = <<'EXPECTED_RESULT'; |
| 91 |
".high" handler: High Span |
| 92 |
wildcard handler: Low Span |
| 93 |
"div.high" handler: High Div |
| 94 |
"div" handler: Low Div |
| 95 |
".oddball" handler: Oddball Div |
| 96 |
EXPECTED_RESULT |
| 97 |
|
| 98 |
# Marpa::R2::Display::End |
| 99 |
|
| 100 |
my $result = join "\n", @RESULTS; |
| 101 |
Marpa::R2::Test::is( "$result\n", $expected_result, |
| 102 |
'handler precedence example' ); |
| 103 |
|
| 104 |
# Marpa::R2::Display |
| 105 |
# name: 'HTML Pod: Structure vs. Element Example' |
| 106 |
# start-after-line: END_OF_EXAMPLE |
| 107 |
# end-before-line: '^END_OF_EXAMPLE$' |
| 108 |
|
| 109 |
my $tagged_html_example = <<'END_OF_EXAMPLE'; |
| 110 |
<title>Short</title><p>Text</head><head> |
| 111 |
END_OF_EXAMPLE |
| 112 |
|
| 113 |
# Marpa::R2::Display::End |
| 114 |
|
| 115 |
my $expected_structured_result = <<'END_OF_EXPECTED'; |
| 116 |
<html> |
| 117 |
<head> |
| 118 |
<title>Short</title></head> |
| 119 |
<body> |
| 120 |
<p>Text</p> |
| 121 |
</head><head> |
| 122 |
</body> |
| 123 |
</html> |
| 124 |
END_OF_EXPECTED |
| 125 |
|
| 126 |
sub supply_missing_tags { |
| 127 |
my $tagname = Marpa::R2::HTML::tagname(); |
| 128 |
return |
| 129 |
( Marpa::R2::HTML::start_tag() // "<$tagname>\n" ) |
| 130 |
. Marpa::R2::HTML::contents() |
| 131 |
. ( Marpa::R2::HTML::end_tag() // "</$tagname>\n" ); |
| 132 |
} ## end sub supply_missing_tags |
| 133 |
my $structured_html_ref = |
| 134 |
Marpa::R2::HTML::html( \$tagged_html_example, |
| 135 |
{ q{*} => \&supply_missing_tags } ); |
| 136 |
|
| 137 |
Marpa::R2::Test::is( ${$structured_html_ref}, $expected_structured_result, |
| 138 |
'structure vs. tags example' ); |
| 139 |
|