Skip to content

Commit

Permalink
Eliminate use vars, and add test that all vars are populated.
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Mar 10, 2024
1 parent aa53885 commit c1606be
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 36 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Makefile.PL
MANIFEST
README.md
t/00-load.t
t/01-populated.t
t/pod.t
63 changes: 27 additions & 36 deletions lib/HTML/Tagset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Version 3.21
=cut

use vars qw( $VERSION );

$VERSION = '3.21_01';
our $VERSION = '3.22';

=head1 SYNOPSIS
Expand All @@ -34,20 +32,6 @@ set -- the hash conveys that its keys are there, and the actual values
associated with the keys are not significant. (But what values are
there, are always true.)
=cut

use vars qw(
$VERSION
%emptyElement %optionalEndTag %linkElements %boolean_attr
%isHeadElement %isBodyElement %isPhraseMarkup
%is_Possible_Strict_P_Content
%isHeadOrBodyElement
%isList %isTableElement %isFormElement
%isKnown %canTighten
@p_closure_barriers
%isCDATA_Parent
);

=head1 VARIABLES
Note that none of these variables are exported.
Expand All @@ -61,7 +45,8 @@ C<$HTML::Tagset::emptyElement{'dl'}> does not exist, and so is not true.
=cut

%emptyElement = map {; $_ => 1 } qw(base link meta isindex
our %emptyElement = map { $_ => 1 } qw(
base link meta isindex
img br hr wbr
input area param
embed bgsound spacer
Expand All @@ -80,7 +65,9 @@ C<$HTML::Tagset::emptyElement{'li'}> exists and is true.
=cut

%optionalEndTag = map {; $_ => 1 } qw(p li dt dd); # option th tr td);
our %optionalEndTag = map { $_ => 1 } qw(
p li dt dd
); # option th tr td);

=head2 hash %HTML::Tagset::linkElements
Expand All @@ -90,7 +77,7 @@ of attributes whose values can be links.
=cut

%linkElements =
our %linkElements =
(
'a' => ['href'],
'applet' => ['archive', 'codebase', 'code'],
Expand Down Expand Up @@ -132,7 +119,7 @@ the value is a reference to a hashset containing all such attributes.
=cut

%boolean_attr = (
our %boolean_attr = (
# TODO: make these all hashes
'area' => 'nohref',
'dir' => 'compact',
Expand Down Expand Up @@ -177,7 +164,7 @@ This hashset contains all phrasal-level elements.
=cut

%isPhraseMarkup = map {; $_ => 1 } qw(
our %isPhraseMarkup = map { $_ => 1 } qw(
span abbr acronym q sub sup
cite code em kbd samp strong var dfn strike
b i u s tt small big
Expand All @@ -196,7 +183,8 @@ P element, for a strict model of HTML.
=cut

%is_Possible_Strict_P_Content = (
our %isFormElement; # Forward declaration
our %is_Possible_Strict_P_Content = (
%isPhraseMarkup,
%isFormElement,
map {; $_ => 1} qw( object script map )
Expand Down Expand Up @@ -225,7 +213,7 @@ present only in the 'head' element of an HTML document.
=cut

%isHeadElement = map {; $_ => 1 }
our %isHeadElement = map { $_ => 1 }
qw(title base link meta isindex script style object bgsound);

=head2 hashset %HTML::Tagset::isList
Expand All @@ -234,7 +222,9 @@ This hashset contains all elements that can contain "li" elements.
=cut

%isList = map {; $_ => 1 } qw(ul ol dir menu);
our %isList = map { $_ => 1 } qw(
ul ol dir menu
);

=head2 hashset %HTML::Tagset::isTableElement
Expand All @@ -243,7 +233,7 @@ a "table" element.
=cut

%isTableElement = map {; $_ => 1 }
our %isTableElement = map { $_ => 1 }
qw(tr td th thead tbody tfoot caption col colgroup);

=head2 hashset %HTML::Tagset::isFormElement
Expand All @@ -253,7 +243,8 @@ a "form" element.
=cut

%isFormElement = map {; $_ => 1 }
# Declared earlier in the file
%isFormElement = map { $_ => 1 }
qw(input select option optgroup textarea button label);

=head2 hashset %HTML::Tagset::isBodyElement
Expand All @@ -263,7 +254,7 @@ the "body" element of an HTML document.
=cut

%isBodyElement = map {; $_ => 1 } qw(
our %isBodyElement = map { $_ => 1 } qw(
h1 h2 h3 h4 h5 h6
p div pre plaintext address blockquote
xmp listing
Expand All @@ -277,9 +268,9 @@ the "body" element of an HTML document.
ol ul dir menu li
dl dt dd
ins del
fieldset legend
map area
applet param object
isindex script noscript
Expand All @@ -300,7 +291,7 @@ the head or in the body.
=cut

%isHeadOrBodyElement = map {; $_ => 1 }
our %isHeadOrBodyElement = map { $_ => 1 }
qw(script isindex style object map area param noscript bgsound);
# i.e., if we find 'script' in the 'body' or the 'head', don't freak out.

Expand All @@ -311,8 +302,8 @@ This hashset lists all known HTML elements.
=cut

%isKnown = (%isHeadElement, %isBodyElement,
map{; $_=>1 }
our %isKnown = (%isHeadElement, %isBodyElement,
map{ $_ => 1 }
qw( head body html
frame frameset noframes
~comment ~pi ~directive ~literal
Expand All @@ -327,7 +318,7 @@ children or siblings.
=cut

%canTighten = %isKnown;
our %canTighten = %isKnown;
delete @canTighten{
keys(%isPhraseMarkup), 'input', 'select',
'xmp', 'listing', 'plaintext', 'pre',
Expand Down Expand Up @@ -387,7 +378,7 @@ barrier-tags.
=cut

@p_closure_barriers = qw(
our @p_closure_barriers = qw(
li blockquote
ul ol menu dir
dl dt dd
Expand All @@ -404,7 +395,7 @@ This hashset includes all elements whose content is CDATA.
=cut

%isCDATA_Parent = map {; $_ => 1 }
our %isCDATA_Parent = map { $_ => 1 }
qw(script style xmp listing plaintext);

# TODO: there's nothing else that takes CDATA children, right?
Expand Down
59 changes: 59 additions & 0 deletions t/01-populated.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!perl

# Verify that all the hashes and arrays are populated.

use strict;
use warnings;

use Test::More tests => 32;

use HTML::Tagset;

my @vars = qw(
%emptyElement
%optionalEndTag
%linkElements
%boolean_attr
%isPhraseMarkup
%is_Possible_Strict_P_Content
%isHeadElement
%isList
%isTableElement
%isFormElement
%isBodyElement
%isHeadOrBodyElement
%isKnown
%canTighten
%isCDATA_Parent
@p_closure_barriers
);


HASHES: {
for my $var ( grep { /%/ } @vars ) {
$var =~ s/^%(.+)/%HTML::Tagset::$1/ or die;

my %h = eval "$var";
cmp_ok( scalar keys %h, '>', 0, "$var is not an empty hash" );

my @undefs = grep { !defined } values %h;
is( scalar @undefs, 0, "$var has no undef values" );
}
}


ARRAYS: {
for my $var ( grep { /@/ } @vars ) {
$var =~ s/^\@(.+)/\@HTML::Tagset::$1/ or die;

my @a = eval "$var";
cmp_ok( scalar @a, '>', 0, "$var is not an empty array" );

my @undefs = grep { !defined } @a;
is( scalar @undefs, 0, "$var has no undef values" );
}
}


exit;

0 comments on commit c1606be

Please sign in to comment.