Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

HTML::TreeBuilder and HTML4.01 empty elements

by wfsp (Abbot)
on Jul 09, 2008 at 10:38 UTC ( [id://696419]=perlquestion: print w/replies, xml ) Need Help??

wfsp has asked for the wisdom of the Perl Monks concerning the following question:

w3c advise using <br> in HTML4.01 and not <br /> (while it validates as strict it generates a warning).

Does HTML::TreeBuilder have an option/attribute (via HTML::Element, HTML::Parser, etc.) that disables the XHTML style empty tags?

The only references I can find are about parsing rather than output.

#!/usr/local/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $h = HTML::Element->new_from_lol( ['html', ['hr'], ['br'], ['img', {src => 'pic.jpg'}], ], ); print $h->as_HTML __DATA__ output: <html><hr /><br /><img src="pic.jpg" /></html>

Replies are listed 'Best First'.
Re: HTML::TreeBuilder and HTML4.01 empty elements
by rhesa (Vicar) on Jul 09, 2008 at 11:54 UTC
    The relevant code in HTML::Element->starttag is this:
    if ( scalar $self->content_list == 0 && $self->_empty_element_map- +>{ $self->tag } ) { return $tag . " />"; } else { return $tag . ">"; }
    Since hr, br, and img never have children, the first condition is always true. So we'll have to tweak the second one to get the output you want.

    Here's a quick hack that works:

    #!/usr/local/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $h = HTML::Element->new_from_lol( ['html', ['hr'], ['br'], ['img', {src => 'pic.jpg'}], ], ); { # suppress xhtml /> on these tags: my @noslash = qw/ hr br img /; # by temporarily pretending the can have content: local @HTML::Tagset::emptyElement{ @noslash } = 0; # and have optional end tags: local @HTML::Tagset::optionalEndTag{ @noslash } = (1) x @noslash; print $h->as_HTML; } __END__ <html><hr><br><img src="pic.jpg"></html>
Re: HTML::TreeBuilder and HTML4.01 empty elements
by Anonymous Monk on Jul 10, 2008 at 03:30 UTC
    http://search.cpan.org/grep?cpanid=PETEK&release=HTML-Tree-3.23&string=XHTML+&i=1&n=1&C=0
    http://search.cpan.org/grep?cpanid=PETEK&release=HTML-Tree-3.23&string=%2F%3E&i=1&n=1&C=0
    http://search.cpan.org/grep?cpanid=PETEK&release=HTML-Tree-3.23&string=empty&i=1&n=1&C=0
    http://search.cpan.org/src/PETEK/HTML-Tree-3.23/lib/HTML/Element.pm
    sub starttag { my($self, $entities) = @_; my $name = $self->{'_tag'}; return $self->{'text'} if $name eq '~literal'; return "<!" . $self->{'text'} . ">" if $name eq '~declaration'; return "<?" . $self->{'text'} . ">" if $name eq '~pi'; if($name eq '~comment') { if(ref($self->{'text'} || '') eq 'ARRAY') { # Does this ever get used? And is this right? return "<!" . join(' ', map("--$_--", @{$self->{'text'}})) . ">" ; } else { return "<!--" . $self->{'text'} . "-->" } } my $tag = $html_uc ? "<\U$name" : "<\L$name"; my $val; for (sort keys %$self) { # predictable ordering next if !length $_ or m/^_/s or $_ eq '/'; $val = $self->{$_}; next if !defined $val; # or ref $val; if ($_ eq $val && # if attribute is boolean, for this elemen +t exists($HTML::Element::boolean_attr{$name}) && (ref($HTML::Element::boolean_attr{$name}) ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_) ) { $tag .= $html_uc ? " \U$_" : " \L$_"; } else { # non-boolean attribute if (ref $val eq 'HTML::Element' and $val->{_tag} eq '~literal') { $val = $val->{text}; } else { HTML::Entities::encode_entities($val, $entities) unless (d +efined($entities) && !length($entities)); } $val = qq{"$val"}; $tag .= $html_uc ? qq{ \U$_\E=$val} : qq{ \L$_\E=$val}; } } # for keys if ( scalar $self->content_list == 0 && $self->_empty_element_map- +>{ $self->tag } ) { return $tag . " />"; } else { return $tag . ">"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://696419]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found