will have the new attribute listed at the end.
#
$token->set_attr(class => 'some_class');
print $token->as_is;
#
#
$token->set_attr('bgcolor','red');
print $token->as_is;
#
After this method is called, if successful, the C, C
and C methods will all return updated results.
=item * C
Under the premise that C methods should accept what their corresponding
C methods emit, the following works:
$tag->set_attr($tag->get_attr);
Theoretically that's a no-op and for purposes of rendering HTML, it should be.
However, internally this calls C<$tag-Erewrite_tag>, so see that method to
understand how this may affect you.
Of course, this is useless if you want to actually change the attributes, so you
can do this:
my $attrs = {
class => 'headline',
valign => 'top'
};
$token->set_attr($attrs)
if $token->is_start_tag('td') && $token->get_attr('class') eq 'stories';
=item * C
This method rewrites the tag. The tag name and the name of all attributes will
be lower-cased. Values that are not quoted with double quotes will be. This
may be called on both start or end tags. Note that both C and
C call this method prior to returning.
If called on a token that is not a tag, it simply returns. Regardless of how
it is called, it returns the token.
#
$token->rewrite_tag;
print $token->as_is;
#
A quick cleanup of sloppy HTML is now the following:
my $parser = HTML::TokeParser::Simple->new( string => $ugly_html );
while (my $token = $parser->get_token) {
$token->rewrite_tag;
print $token->as_is;
}
=back
=head1 PARSER VERSUS TOKENS
The parser returns tokens that are blessed into appropriate classes. Some
people get confused and try to call parser methods on tokens and token methods
on the parser. To prevent this, C versions 1.4 and
above now bless all tokens into appropriate token classes. Please keep this in
mind while using this module (and many thanks to PodMaster
L for pointing out this issue
to me.)
=head1 EXAMPLES
=head2 Finding comments
For some strange reason, your Pointy-Haired Boss (PHB) is convinced that the
graphics department is making fun of him by embedding rude things about him in
HTML comments. You need to get all HTML comments from the HTML.
use strict;
use HTML::TokeParser::Simple;
my @html_docs = glob( "*.html" );
open PHB, "> phbreport.txt" or die "Cannot open phbreport for writing: $!";
foreach my $doc ( @html_docs ) {
print "Processing $doc\n";
my $p = HTML::TokeParser::Simple->new( file => $doc );
while ( my $token = $p->get_token ) {
next unless $token->is_comment;
print PHB $token->as_is, "\n";
}
}
close PHB;
=head2 Stripping Comments
Uh oh. Turns out that your PHB was right for a change. Many of the comments
in the HTML weren't very polite. Since your entire graphics department was
just fired, it falls on you need to strip those comments from the HTML.
use strict;
use HTML::TokeParser::Simple;
my $new_folder = 'no_comment/';
my @html_docs = glob( "*.html" );
foreach my $doc ( @html_docs ) {
print "Processing $doc\n";
my $new_file = "$new_folder$doc";
open PHB, "> $new_file" or die "Cannot open $new_file for writing: $!";
my $p = HTML::TokeParser::Simple->new( $file => doc );
while ( my $token = $p->get_token ) {
next if $token->is_comment;
print PHB $token->as_is;
}
close PHB;
}
=head2 Changing form tags
Your company was foo.com and now is bar.com. Unfortunately, whoever wrote your
HTML decided to hardcode "http://www.foo.com/" into the C attribute of
the form tags. You need to change it to "http://www.bar.com/".
use strict;
use HTML::TokeParser::Simple;
my $new_folder = 'new_html/';
my @html_docs = glob( "*.html" );
foreach my $doc ( @html_docs ) {
print "Processing $doc\n";
my $new_file = "$new_folder$doc";
open FILE, "> $new_file" or die "Cannot open $new_file for writing: $!";
my $p = HTML::TokeParser::Simple->new( file => $doc );
while ( my $token = $p->get_token ) {
if ( $token->is_start_tag('form') ) {
my $action = $token->get_attr(action);
$action =~ s/www\.foo\.com/www.bar.com/;
$token->set_attr('action', $action);
}
print FILE $token->as_is;
}
close FILE;
}
=head1 CAVEATS
For compatability reasons with C, methods that return
references are violating encapsulation and altering the references directly
B alter the state of the object. Subsequent calls to C
can thus have unexpected results. Do not alter these references directly
unless you are following behavior described in these docs. In the future,
certain methods such as C, C and others may return a
copy of the reference rather than the original reference. This behavior has
not yet been changed in order to maintain compatability with previous versions
of this module. At the present time, your author is not aware of anyone taking
advantage of this "feature," but it's better to be safe than sorry.
Use of C<$HTML::Parser::VERSION> which is less than 3.25 may result in
incorrect behavior as older versions do not always handle XHTML correctly. It
is the programmer's responsibility to verify that the behavior of this code
matches the programmer's needs.
Note that C processes text in 512 byte chunks. This sometimes
will cause strange behavior and cause text to be broken into more than one
token. You can suppress this behavior with the following command:
$p->unbroken_text( [$bool] );
See the C documentation and
http://www.perlmonks.org/index.pl?node_id=230667 for more information.
=head1 BUGS
There are no known bugs, but that's no guarantee.
Address bug reports and comments to: Eeop_divo_sitruc@yahoo.comE. When
sending bug reports, please provide the version of C,
C, C, the version of Perl, and the
version of the operating system you are using.
Reverse the name to email the author.
=head1 SUBCLASSING
You may wish to change the behavior of this module. You probably do not want
to subclass C. Instead, you'll want to subclass one
of the token classes. C is the base class for
all tokens. Global behavioral changes should go there. Otherwise, see the
appropriate token class for the behavior you wish to alter.
=head1 SEE ALSO
L
L
L
L
L
L
=head1 COPYRIGHT
Copyright (c) 2004 by Curtis "Ovid" Poe. All rights reserved. This program is
free software; you may redistribute it and/or modify it under the same terms as
Perl itself
=head1 AUTHOR
Curtis "Ovid" Poe Eeop_divo_sitruc@yahoo.comE
Reverse the name to email the author.
=cut