<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package HTML::Parser;

use strict;

our $VERSION = '3.81';

require HTML::Entities;

require XSLoader;
XSLoader::load('HTML::Parser', $VERSION);

sub new
{
    my $class = shift;
    my $self = bless {}, $class;
    return $self-&gt;init(@_);
}


sub init
{
    my $self = shift;
    $self-&gt;_alloc_pstate;

    my %arg = @_;
    my $api_version = delete $arg{api_version} || (@_ ? 3 : 2);
    if ($api_version &gt;= 4) {
	require Carp;
	Carp::croak("API version $api_version not supported " .
		    "by HTML::Parser $VERSION");
    }

    if ($api_version &lt; 3) {
	# Set up method callbacks compatible with HTML-Parser-2.xx
	$self-&gt;handler(text    =&gt; "text",    "self,text,is_cdata");
	$self-&gt;handler(end     =&gt; "end",     "self,tagname,text");
	$self-&gt;handler(process =&gt; "process", "self,token0,text");
	$self-&gt;handler(start   =&gt; "start",
		                  "self,tagname,attr,attrseq,text");

	$self-&gt;handler(comment =&gt;
		       sub {
			   my($self, $tokens) = @_;
			   for (@$tokens) {
			       $self-&gt;comment($_);
			   }
		       }, "self,tokens");

	$self-&gt;handler(declaration =&gt;
		       sub {
			   my $self = shift;
			   $self-&gt;declaration(substr($_[0], 2, -1));
		       }, "self,text");
    }

    if (my $h = delete $arg{handlers}) {
	$h = {@$h} if ref($h) eq "ARRAY";
	while (my($event, $cb) = each %$h) {
	    $self-&gt;handler($event =&gt; @$cb);
	}
    }

    # In the end we try to assume plain attribute or handler
    while (my($option, $val) = each %arg) {
	if ($option =~ /^(\w+)_h$/) {
	    $self-&gt;handler($1 =&gt; @$val);
	}
        elsif ($option =~ /^(text|start|end|process|declaration|comment)$/) {
	    require Carp;
	    Carp::croak("Bad constructor option '$option'");
        }
	else {
	    $self-&gt;$option($val);
	}
    }

    return $self;
}


sub parse_file
{
    my($self, $file) = @_;
    my $opened;
    if (!ref($file) &amp;&amp; ref(\$file) ne "GLOB") {
        # Assume $file is a filename
        local(*F);
        open(F, "&lt;", $file) || return undef;
        binmode(F);  # should we? good for byte counts
        $opened++;
        $file = *F;
    }
    my $chunk = '';
    while (read($file, $chunk, 512)) {
        $self-&gt;parse($chunk) || last;
    }
    close($file) if $opened;
    $self-&gt;eof;
}


sub netscape_buggy_comment  # legacy
{
    my $self = shift;
    require Carp;
    Carp::carp("netscape_buggy_comment() is deprecated.  " .
        "Please use the strict_comment() method instead");
    my $old = !$self-&gt;strict_comment;
    $self-&gt;strict_comment(!shift) if @_;
    return $old;
}

# set up method stubs
sub text { }
*start       = \&amp;text;
*end         = \&amp;text;
*comment     = \&amp;text;
*declaration = \&amp;text;
*process     = \&amp;text;

1;

__END__


=head1 NAME

HTML::Parser - HTML parser class

=head1 SYNOPSIS

    use strict;
    use warnings;
    use HTML::Parser ();

    # Create parser object
    my $p = HTML::Parser-&gt;new(
        api_version     =&gt; 3,
        start_h         =&gt; [\&amp;start, "tagname, attr"],
        end_h           =&gt; [\&amp;end,   "tagname"],
        marked_sections =&gt; 1,
    );

    # Parse document text chunk by chunk
    $p-&gt;parse($chunk1);
    $p-&gt;parse($chunk2);

    # ...
    # signal end of document
    $p-&gt;eof;

    # Parse directly from file
    $p-&gt;parse_file("foo.html");

    # or
    open(my $fh, "&lt;:utf8", "foo.html") || die;
    $p-&gt;parse_file($fh);

=head1 DESCRIPTION

Objects of the C&lt;HTML::Parser&gt; class will recognize markup and
separate it from plain text (alias data content) in HTML
documents.  As different kinds of markup and text are recognized, the
corresponding event handlers are invoked.

C&lt;HTML::Parser&gt; is not a generic SGML parser.  We have tried to
make it able to deal with the HTML that is actually "out there", and
it normally parses as closely as possible to the way the popular web
browsers do it instead of strictly following one of the many HTML
specifications from W3C.  Where there is disagreement, there is often
an option that you can enable to get the official behaviour.

The document to be parsed may be supplied in arbitrary chunks.  This
makes on-the-fly parsing as documents are received from the network
possible.

If event driven parsing does not feel right for your application, you
might want to use C&lt;HTML::PullParser&gt;.  This is an C&lt;HTML::Parser&gt;
subclass that allows a more conventional program structure.


=head1 METHODS

The following method is used to construct a new C&lt;HTML::Parser&gt; object:

=over

=item $p = HTML::Parser-&gt;new( %options_and_handlers )

This class method creates a new C&lt;HTML::Parser&gt; object and
returns it.  Key/value argument pairs may be provided to assign event
handlers or initialize parser options.  The handlers and parser
options can also be set or modified later by the method calls described below.

If a top level key is in the form "&lt;event&gt;_h" (e.g., "text_h") then it
assigns a handler to that event, otherwise it initializes a parser
option. The event handler specification value must be an array
reference.  Multiple handlers may also be assigned with the 'handlers
=&gt; [%handlers]' option.  See examples below.

If new() is called without any arguments, it will create a parser that
uses callback methods compatible with version 2 of C&lt;HTML::Parser&gt;.
See the section on "version 2 compatibility" below for details.

The special constructor option 'api_version =&gt; 2' can be used to
initialize version 2 callbacks while still setting other options and
handlers.  The 'api_version =&gt; 3' option can be used if you don't want
to set any options and don't want to fall back to v2 compatible
mode.

Examples:

 $p = HTML::Parser-&gt;new(
   api_version =&gt; 3,
   text_h =&gt; [ sub {...}, "dtext" ]
 );

This creates a new parser object with a text event handler subroutine
that receives the original text with general entities decoded.

 $p = HTML::Parser-&gt;new(
   api_version =&gt; 3,
   start_h =&gt; [ 'my_start', "self,tokens" ]
 );

This creates a new parser object with a start event handler method
that receives the $p and the tokens array.

 $p = HTML::Parser-&gt;new(
   api_version =&gt; 3,
   handlers =&gt; {
     text =&gt; [\@array, "event,text"],
     comment =&gt; [\@array, "event,text"],
   }
 );

This creates a new parser object that stores the event type and the
original text in @array for text and comment events.

=back

The following methods feed the HTML document
to the C&lt;HTML::Parser&gt; object:

=over

=item $p-&gt;parse( $string )

Parse $string as the next chunk of the HTML document.  Handlers invoked should
not attempt to modify the $string in-place until $p-&gt;parse returns.

If an invoked event handler aborts parsing by calling $p-&gt;eof, then $p-&gt;parse()
will return a FALSE value.  Otherwise the return value is a reference to the
parser object ($p).

=item $p-&gt;parse( $code_ref )

If a code reference is passed as the argument to be parsed, then the
chunks to be parsed are obtained by invoking this function repeatedly.
Parsing continues until the function returns an empty (or undefined)
result.  When this happens $p-&gt;eof is automatically signaled.

Parsing will also abort if one of the event handlers calls $p-&gt;eof.

The effect of this is the same as:

    while (1) {
        my $chunk = &amp;$code_ref();
        if (!defined($chunk) || !length($chunk)) {
            $p-&gt;eof;
            return $p;
        }
        $p-&gt;parse($chunk) || return undef;
    }

But it is more efficient as this loop runs internally in XS code.

=item $p-&gt;parse_file( $file )

Parse text directly from a file.  The $file argument can be a
filename, an open file handle, or a reference to an open file
handle.

If $file contains a filename and the file can't be opened, then the
method returns an undefined value and $! tells why it failed.
Otherwise the return value is a reference to the parser object.

If a file handle is passed as the $file argument, then the file will
normally be read until EOF, but not closed.

If an invoked event handler aborts parsing by calling $p-&gt;eof,
then $p-&gt;parse_file() may not have read the entire file.

On systems with multi-byte line terminators, the values passed for the
offset and length argspecs may be too low if parse_file() is called on
a file handle that is not in binary mode.

If a filename is passed in, then parse_file() will open the file in
binary mode.

=item $p-&gt;eof

Signals the end of the HTML document.  Calling the $p-&gt;eof method
outside a handler callback will flush any remaining buffered text
(which triggers the C&lt;text&gt; event if there is any remaining text).

Calling $p-&gt;eof inside a handler will terminate parsing at that point
and cause $p-&gt;parse to return a FALSE value.  This also terminates
parsing by $p-&gt;parse_file().

After $p-&gt;eof has been called, the parse() and parse_file() methods
can be invoked to feed new documents with the parser object.

The return value from eof() is a reference to the parser object.

=back


Most parser options are controlled by boolean attributes.
Each boolean attribute is enabled by calling the corresponding method
with a TRUE argument and disabled with a FALSE argument.  The
attribute value is left unchanged if no argument is given.  The return
value from each method is the old attribute value.

Methods that can be used to get and/or set parser options are:

=over

=item $p-&gt;attr_encoded

=item $p-&gt;attr_encoded( $bool )

By default, the C&lt;attr&gt; and C&lt;@attr&gt; argspecs will have general
entities for attribute values decoded.  Enabling this attribute leaves
entities alone.

=item $p-&gt;backquote

=item $p-&gt;backquote( $bool )

By default, only ' and " are recognized as quote characters around
attribute values.  MSIE also recognizes backquotes for some reason.
Enabling this attribute provides compatibility with this behaviour.

=item $p-&gt;boolean_attribute_value( $val )

This method sets the value reported for boolean attributes inside HTML
start tags.  By default, the name of the attribute is also used as its
value.  This affects the values reported for C&lt;tokens&gt; and C&lt;attr&gt;
argspecs.

=item $p-&gt;case_sensitive

=item $p-&gt;case_sensitive( $bool )

By default, tag names and attribute names are down-cased.  Enabling this
attribute leaves them as found in the HTML source document.

=item $p-&gt;closing_plaintext

=item $p-&gt;closing_plaintext( $bool )

By default, C&lt;plaintext&gt; element can never be closed. Everything up to
the end of the document is parsed in CDATA mode.  This historical
behaviour is what at least MSIE does.  Enabling this attribute makes
closing C&lt; &lt;/plaintext&gt; &gt; tag effective and the parsing process will resume
after seeing this tag.  This emulates early gecko-based browsers.

=item $p-&gt;empty_element_tags

=item $p-&gt;empty_element_tags( $bool )

By default, empty element tags are not recognized as such and the "/"
before "&gt;" is just treated like a normal name character (unless
C&lt;strict_names&gt; is enabled).  Enabling this attribute make
C&lt;HTML::Parser&gt; recognize these tags.

Empty element tags look like start tags, but end with the character
sequence "/&gt;" instead of "&gt;".  When recognized by C&lt;HTML::Parser&gt; they
cause an artificial end event in addition to the start event.  The
C&lt;text&gt; for the artificial end event will be empty and the C&lt;tokenpos&gt;
array will be undefined even though the token array will have one
element containing the tag name.

=item $p-&gt;marked_sections

=item $p-&gt;marked_sections( $bool )

By default, section markings like &lt;![CDATA[...]]&gt; are treated like
ordinary text.  When this attribute is enabled section markings are
honoured.

There are currently no events associated with the marked section
markup, but the text can be returned as C&lt;skipped_text&gt;.

=item $p-&gt;strict_comment

=item $p-&gt;strict_comment( $bool )

By default, comments are terminated by the first occurrence of "--&gt;".
This is the behaviour of most popular browsers (like Mozilla, Opera and
MSIE), but it is not correct according to the official HTML
standard.  Officially, you need an even number of "--" tokens before
the closing "&gt;" is recognized and there may not be anything but
whitespace between an even and an odd "--".

The official behaviour is enabled by enabling this attribute.

Enabling of 'strict_comment' also disables recognizing these forms as
comments:

  &lt;/ comment&gt;
  &lt;! comment&gt;


=item $p-&gt;strict_end

=item $p-&gt;strict_end( $bool )

By default, attributes and other junk are allowed to be present on end tags in a
manner that emulates MSIE's behaviour.

The official behaviour is enabled with this attribute.  If enabled,
only whitespace is allowed between the tagname and the final "&gt;".

=item $p-&gt;strict_names

=item $p-&gt;strict_names( $bool )

By default, almost anything is allowed in tag and attribute names.
This is the behaviour of most popular browsers and allows us to parse
some broken tags with invalid attribute values like:

   &lt;IMG SRC=newprevlstGr.gif ALT=[PREV LIST] BORDER=0&gt;

By default, "LIST]" is parsed as a boolean attribute, not as
part of the ALT value as was clearly intended.  This is also what
Mozilla sees.

The official behaviour is enabled by enabling this attribute.  If
enabled, it will cause the tag above to be reported as text
since "LIST]" is not a legal attribute name.

=item $p-&gt;unbroken_text

=item $p-&gt;unbroken_text( $bool )

By default, blocks of text are given to the text handler as soon as
possible (but the parser takes care always to break text at a
boundary between whitespace and non-whitespace so single words and
entities can always be decoded safely).  This might create breaks that
make it hard to do transformations on the text. When this attribute is
enabled, blocks of text are always reported in one piece.  This will
delay the text event until the following (non-text) event has been
recognized by the parser.

Note that the C&lt;offset&gt; argspec will give you the offset of the first
segment of text and C&lt;length&gt; is the combined length of the segments.
Since there might be ignored tags in between, these numbers can't be
used to directly index in the original document file.

=item $p-&gt;utf8_mode

=item $p-&gt;utf8_mode( $bool )

Enable this option when parsing raw undecoded UTF-8.  This tells the
parser that the entities expanded for strings reported by C&lt;attr&gt;,
C&lt;@attr&gt; and C&lt;dtext&gt; should be expanded as decoded UTF-8 so they end
up compatible with the surrounding text.

If C&lt;utf8_mode&gt; is enabled then it is an error to pass strings
containing characters with code above 255 to the parse() method, and
the parse() method will croak if you try.

Example: The Unicode character "\x{2665}" is "\xE2\x99\xA5" when UTF-8
encoded.  The character can also be represented by the entity
"&amp;hearts;" or "&amp;#x2665".  If we feed the parser:

  $p-&gt;parse("\xE2\x99\xA5&amp;hearts;");

then C&lt;dtext&gt; will be reported as "\xE2\x99\xA5\x{2665}" without
C&lt;utf8_mode&gt; enabled, but as "\xE2\x99\xA5\xE2\x99\xA5" when enabled.
The later string is what you want.

This option is only available with perl-5.8 or better.

=item $p-&gt;xml_mode

=item $p-&gt;xml_mode( $bool )

Enabling this attribute changes the parser to allow some XML
constructs.  This enables the behaviour controlled by individually by
the C&lt;case_sensitive&gt;, C&lt;empty_element_tags&gt;, C&lt;strict_names&gt; and
C&lt;xml_pic&gt; attributes and also suppresses special treatment of
elements that are parsed as CDATA for HTML.

=item $p-&gt;xml_pic

=item $p-&gt;xml_pic( $bool )

By default, I&lt;processing instructions&gt; are terminated by "&gt;". When
this attribute is enabled, processing instructions are terminated by
"?&gt;" instead.

=back

As markup and text is recognized, handlers are invoked.  The following
method is used to set up handlers for different events:

=over

=item $p-&gt;handler( event =&gt; \&amp;subroutine, $argspec )

=item $p-&gt;handler( event =&gt; $method_name, $argspec )

=item $p-&gt;handler( event =&gt; \@accum, $argspec )

=item $p-&gt;handler( event =&gt; "" );

=item $p-&gt;handler( event =&gt; undef );

=item $p-&gt;handler( event );

This method assigns a subroutine, method, or array to handle an event.

Event is one of C&lt;text&gt;, C&lt;start&gt;, C&lt;end&gt;, C&lt;declaration&gt;, C&lt;comment&gt;,
C&lt;process&gt;, C&lt;start_document&gt;, C&lt;end_document&gt; or C&lt;default&gt;.

The C&lt;\&amp;subroutine&gt; is a reference to a subroutine which is called to handle
the event.

The C&lt;$method_name&gt; is the name of a method of $p which is called to handle
the event.

The C&lt;@accum&gt; is an array that will hold the event information as
sub-arrays.

If the second argument is "", the event is ignored.
If it is undef, the default handler is invoked for the event.

The C&lt;$argspec&gt; is a string that describes the information to be reported
for the event.  Any requested information that does not apply to a
specific event is passed as C&lt;undef&gt;.  If argspec is omitted, then it
is left unchanged.

The return value from $p-&gt;handler is the old callback routine or a
reference to the accumulator array.

Any return values from handler callback routines/methods are always
ignored.  A handler callback can request parsing to be aborted by
invoking the $p-&gt;eof method.  A handler callback is not allowed to
invoke the $p-&gt;parse() or $p-&gt;parse_file() method.  An exception will
be raised if it tries.

Examples:

    $p-&gt;handler(start =&gt;  "start", 'self, attr, attrseq, text' );

This causes the "start" method of object C&lt;$p&gt; to be called for 'start' events.
The callback signature is C&lt;&lt; $p-&gt;start(\%attr, \@attr_seq, $text) &gt;&gt;.

    $p-&gt;handler(start =&gt;  \&amp;start, 'attr, attrseq, text' );

This causes subroutine start() to be called for 'start' events.
The callback signature is start(\%attr, \@attr_seq, $text).

    $p-&gt;handler(start =&gt;  \@accum, '"S", attr, attrseq, text' );

This causes 'start' event information to be saved in @accum.
The array elements will be ['S', \%attr, \@attr_seq, $text].

   $p-&gt;handler(start =&gt; "");

This causes 'start' events to be ignored.  It also suppresses
invocations of any default handler for start events.  It is in most
cases equivalent to $p-&gt;handler(start =&gt; sub {}), but is more
efficient.  It is different from the empty-sub-handler in that
C&lt;skipped_text&gt; is not reset by it.

   $p-&gt;handler(start =&gt; undef);

This causes no handler to be associated with start events.
If there is a default handler it will be invoked.

=back

Filters based on tags can be set up to limit the number of events
reported.  The main bottleneck during parsing is often the huge number
of callbacks made from the parser.  Applying filters can improve
performance significantly.

The following methods control filters:

=over

=item $p-&gt;ignore_elements( @tags )

Both the C&lt;start&gt; event and the C&lt;end&gt; event as well as any events that
would be reported in between are suppressed.  The ignored elements can
contain nested occurrences of itself.  Example:

   $p-&gt;ignore_elements(qw(script style));

The C&lt;script&gt; and C&lt;style&gt; tags will always nest properly since their
content is parsed in CDATA mode.  For most other tags
C&lt;ignore_elements&gt; must be used with caution since HTML is often not
I&lt;well formed&gt;.

=item $p-&gt;ignore_tags( @tags )

Any C&lt;start&gt; and C&lt;end&gt; events involving any of the tags given are
suppressed.  To reset the filter (i.e. don't suppress any C&lt;start&gt; and
C&lt;end&gt; events), call C&lt;ignore_tags&gt; without an argument.

=item $p-&gt;report_tags( @tags )

Any C&lt;start&gt; and C&lt;end&gt; events involving any of the tags I&lt;not&gt; given
are suppressed.  To reset the filter (i.e. report all C&lt;start&gt; and
C&lt;end&gt; events), call C&lt;report_tags&gt; without an argument.

=back

Internally, the system has two filter lists, one for C&lt;report_tags&gt;
and one for C&lt;ignore_tags&gt;, and both filters are applied.  This
effectively gives C&lt;ignore_tags&gt; precedence over C&lt;report_tags&gt;.

Examples:

   $p-&gt;ignore_tags(qw(style));
   $p-&gt;report_tags(qw(script style));

results in only C&lt;script&gt; events being reported.

=head2 Argspec

Argspec is a string containing a comma-separated list that describes
the information reported by the event.  The following argspec
identifier names can be used:

=over

=item C&lt;attr&gt;

Attr causes a reference to a hash of attribute name/value pairs to be
passed.

Boolean attributes' values are either the value set by
$p-&gt;boolean_attribute_value, or the attribute name if no value has been
set by $p-&gt;boolean_attribute_value.

This passes undef except for C&lt;start&gt; events.

Unless C&lt;xml_mode&gt; or C&lt;case_sensitive&gt; is enabled, the attribute
names are forced to lower case.

General entities are decoded in the attribute values and
one layer of matching quotes enclosing the attribute values is removed.

The Unicode character set is assumed for entity decoding.

=item C&lt;@attr&gt;

Basically the same as C&lt;attr&gt;, but keys and values are passed as
individual arguments and the original sequence of the attributes is
kept.  The parameters passed will be the same as the @attr calculated
here:

   @attr = map { $_ =&gt; $attr-&gt;{$_} } @$attrseq;

assuming $attr and $attrseq here are the hash and array passed as the
result of C&lt;attr&gt; and C&lt;attrseq&gt; argspecs.

This passes no values for events besides C&lt;start&gt;.

=item C&lt;attrseq&gt;

Attrseq causes a reference to an array of attribute names to be
passed.  This can be useful if you want to walk the C&lt;attr&gt; hash in
the original sequence.

This passes undef except for C&lt;start&gt; events.

Unless C&lt;xml_mode&gt; or C&lt;case_sensitive&gt; is enabled, the attribute
names are forced to lower case.

=item C&lt;column&gt;

Column causes the column number of the start of the event to be passed.
The first column on a line is 0.

=item C&lt;dtext&gt;

Dtext causes the decoded text to be passed.  General entities are
automatically decoded unless the event was inside a CDATA section or
was between literal start and end tags (C&lt;script&gt;, C&lt;style&gt;,
C&lt;xmp&gt;, C&lt;iframe&gt;, C&lt;title&gt;, C&lt;textarea&gt; and C&lt;plaintext&gt;).

The Unicode character set is assumed for entity decoding.  With Perl
version 5.6 or earlier only the Latin-1 range is supported, and
entities for characters outside the range 0..255 are left unchanged.

This passes undef except for C&lt;text&gt; events.

=item C&lt;event&gt;

Event causes the event name to be passed.

The event name is one of C&lt;text&gt;, C&lt;start&gt;, C&lt;end&gt;, C&lt;declaration&gt;,
C&lt;comment&gt;, C&lt;process&gt;, C&lt;start_document&gt; or C&lt;end_document&gt;.

=item C&lt;is_cdata&gt;

Is_cdata causes a TRUE value to be passed if the event is inside a CDATA
section or between literal start and end tags (C&lt;script&gt;,
C&lt;style&gt;, C&lt;xmp&gt;, C&lt;iframe&gt;, C&lt;title&gt;, C&lt;textarea&gt; and C&lt;plaintext&gt;).

if the flag is FALSE for a text event, then you should normally
either use C&lt;dtext&gt; or decode the entities yourself before the text is
processed further.

=item C&lt;length&gt;

Length causes the number of bytes of the source text of the event to
be passed.

=item C&lt;line&gt;

Line causes the line number of the start of the event to be passed.
The first line in the document is 1.  Line counting doesn't start
until at least one handler requests this value to be reported.

=item C&lt;offset&gt;

Offset causes the byte position in the HTML document of the start of
the event to be passed.  The first byte in the document has offset 0.

=item C&lt;offset_end&gt;

Offset_end causes the byte position in the HTML document of the end of
the event to be passed.  This is the same as C&lt;offset&gt; + C&lt;length&gt;.

=item C&lt;self&gt;

Self causes the current object to be passed to the handler.  If the
handler is a method, this must be the first element in the argspec.

An alternative to passing self as an argspec is to register closures
that capture $self by themselves as handlers.  Unfortunately this
creates circular references which prevent the HTML::Parser object
from being garbage collected.  Using the C&lt;self&gt; argspec avoids this
problem.

=item C&lt;skipped_text&gt;

Skipped_text returns the concatenated text of all the events that have
been skipped since the last time an event was reported.  Events might
be skipped because no handler is registered for them or because some
filter applies.  Skipped text also includes marked section markup,
since there are no events that can catch it.

If an C&lt;""&gt;-handler is registered for an event, then the text for this
event is not included in C&lt;skipped_text&gt;.  Skipped text both before
and after the C&lt;""&gt;-event is included in the next reported
C&lt;skipped_text&gt;.

=item C&lt;tag&gt;

Same as C&lt;tagname&gt;, but prefixed with "/" if it belongs to an C&lt;end&gt;
event and "!" for a declaration.  The C&lt;tag&gt; does not have any prefix
for C&lt;start&gt; events, and is in this case identical to C&lt;tagname&gt;.

=item C&lt;tagname&gt;

This is the element name (or I&lt;generic identifier&gt; in SGML jargon) for
start and end tags.  Since HTML is case insensitive, this name is
forced to lower case to ease string matching.

Since XML is case sensitive, the tagname case is not changed when
C&lt;xml_mode&gt; is enabled.  The same happens if the C&lt;case_sensitive&gt; attribute
is set.

The declaration type of declaration elements is also passed as a tagname,
even if that is a bit strange.
In fact, in the current implementation tagname is
identical to C&lt;token0&gt; except that the name may be forced to lower case.

=item C&lt;token0&gt;

Token0 causes the original text of the first token string to be
passed.  This should always be the same as $tokens-&gt;[0].

For C&lt;declaration&gt; events, this is the declaration type.

For C&lt;start&gt; and C&lt;end&gt; events, this is the tag name.

For C&lt;process&gt; and non-strict C&lt;comment&gt; events, this is everything
inside the tag.

This passes undef if there are no tokens in the event.

=item C&lt;tokenpos&gt;

Tokenpos causes a reference to an array of token positions to be
passed.  For each string that appears in C&lt;tokens&gt;, this array
contains two numbers.  The first number is the offset of the start of
the token in the original C&lt;text&gt; and the second number is the length
of the token.

Boolean attributes in a C&lt;start&gt; event will have (0,0) for the
attribute value offset and length.

This passes undef if there are no tokens in the event (e.g., C&lt;text&gt;)
and for artificial C&lt;end&gt; events triggered by empty element tags.

If you are using these offsets and lengths to modify C&lt;text&gt;, you
should either work from right to left, or be very careful to calculate
the changes to the offsets.

=item C&lt;tokens&gt;

Tokens causes a reference to an array of token strings to be passed.
The strings are exactly as they were found in the original text,
no decoding or case changes are applied.

For C&lt;declaration&gt; events, the array contains each word, comment, and
delimited string starting with the declaration type.

For C&lt;comment&gt; events, this contains each sub-comment.  If
$p-&gt;strict_comments is disabled, there will be only one sub-comment.

For C&lt;start&gt; events, this contains the original tag name followed by
the attribute name/value pairs.  The values of boolean attributes will
be either the value set by $p-&gt;boolean_attribute_value, or the
attribute name if no value has been set by
$p-&gt;boolean_attribute_value.

For C&lt;end&gt; events, this contains the original tag name (always one token).

For C&lt;process&gt; events, this contains the process instructions (always one
token).

This passes C&lt;undef&gt; for C&lt;text&gt; events.

=item C&lt;text&gt;

Text causes the source text (including markup element delimiters) to be
passed.

=item C&lt;undef&gt;

Pass an undefined value.  Useful as padding where the same handler
routine is registered for multiple events.

=item C&lt;'...'&gt;

A literal string of 0 to 255 characters enclosed
in single (') or double (") quotes is passed as entered.

=back

The whole argspec string can be wrapped up in C&lt;'@{...}'&gt; to signal
that the resulting event array should be flattened.  This only makes a
difference if an array reference is used as the handler target.
Consider this example:

   $p-&gt;handler(text =&gt; [], 'text');
   $p-&gt;handler(text =&gt; [], '@{text}']);

With two text events; C&lt;"foo"&gt;, C&lt;"bar"&gt;; then the first example will end
up with [["foo"], ["bar"]] and the second with ["foo", "bar"] in
the handler target array.


=head2 Events

Handlers for the following events can be registered:

=over

=item C&lt;comment&gt;

This event is triggered when a markup comment is recognized.

Example:

  &lt;!-- This is a comment -- -- So is this --&gt;

=item C&lt;declaration&gt;

This event is triggered when a I&lt;markup declaration&gt; is recognized.

For typical HTML documents, the only declaration you are
likely to find is &lt;!DOCTYPE ...&gt;.

Example:

  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd"&gt;

DTDs inside &lt;!DOCTYPE ...&gt; will confuse HTML::Parser.

=item C&lt;default&gt;

This event is triggered for events that do not have a specific
handler.  You can set up a handler for this event to catch stuff you
did not want to catch explicitly.

=item C&lt;end&gt;

This event is triggered when an end tag is recognized.

Example:

  &lt;/A&gt;

=item C&lt;end_document&gt;

This event is triggered when $p-&gt;eof is called and after any remaining
text is flushed.  There is no document text associated with this event.

=item C&lt;process&gt;

This event is triggered when a processing instructions markup is
recognized.

The format and content of processing instructions are system and
application dependent.

Examples:

  &lt;? HTML processing instructions &gt;
  &lt;? XML processing instructions ?&gt;

=item C&lt;start&gt;

This event is triggered when a start tag is recognized.

Example:

  &lt;A HREF="http://www.perl.com/"&gt;

=item C&lt;start_document&gt;

This event is triggered before any other events for a new document.  A
handler for it can be used to initialize stuff.  There is no document
text associated with this event.

=item C&lt;text&gt;

This event is triggered when plain text (characters) is recognized.
The text may contain multiple lines.  A sequence of text may be broken
between several text events unless $p-&gt;unbroken_text is enabled.

The parser will make sure that it does not break a word or a sequence
of whitespace between two text events.

=back

=head2 Unicode

C&lt;HTML::Parser&gt; can parse Unicode strings when running under
perl-5.8 or better.  If Unicode is passed to $p-&gt;parse() then chunks
of Unicode will be reported to the handlers.  The offset and length
argspecs will also report their position in terms of characters.

It is safe to parse raw undecoded UTF-8 if you either avoid decoding
entities and make sure to not use I&lt;argspecs&gt; that do, or enable the
C&lt;utf8_mode&gt; for the parser.  Parsing of undecoded UTF-8 might be
useful when parsing from a file where you need the reported offsets
and lengths to match the byte offsets in the file.

If a filename is passed to $p-&gt;parse_file() then the file will be read
in binary mode.  This will be fine if the file contains only ASCII or
Latin-1 characters.  If the file contains UTF-8 encoded text then care
must be taken when decoding entities as described in the previous
paragraph, but better is to open the file with the UTF-8 layer so that
it is decoded properly:

   open(my $fh, "&lt;:utf8", "index.html") || die "...: $!";
   $p-&gt;parse_file($fh);

If the file contains text encoded in a charset besides ASCII, Latin-1
or UTF-8 then decoding will always be needed.

=head1 VERSION 2 COMPATIBILITY

When an C&lt;HTML::Parser&gt; object is constructed with no arguments, a set
of handlers is automatically provided that is compatible with the old
HTML::Parser version 2 callback methods.

This is equivalent to the following method calls:

    $p-&gt;handler(start   =&gt; "start",   "self, tagname, attr, attrseq, text");
    $p-&gt;handler(end     =&gt; "end",     "self, tagname, text");
    $p-&gt;handler(text    =&gt; "text",    "self, text, is_cdata");
    $p-&gt;handler(process =&gt; "process", "self, token0, text");
    $p-&gt;handler(
        comment =&gt; sub {
            my ($self, $tokens) = @_;
            for (@$tokens) { $self-&gt;comment($_); }
        },
        "self, tokens"
    );
    $p-&gt;handler(
        declaration =&gt; sub {
            my $self = shift;
            $self-&gt;declaration(substr($_[0], 2, -1));
        },
        "self, text"
    );

Setting up these handlers can also be requested with the "api_version =&gt;
2" constructor option.

=head1 SUBCLASSING

The C&lt;HTML::Parser&gt; class is able to be subclassed.  Parser objects are plain
hashes and C&lt;HTML::Parser&gt; reserves only hash keys that start with
"_hparser".  The parser state can be set up by invoking the init()
method, which takes the same arguments as new().

=head1 EXAMPLES

The first simple example shows how you might strip out comments from
an HTML document.  We achieve this by setting up a comment handler that
does nothing and a default handler that will print out anything else:

    use HTML::Parser ();
    HTML::Parser-&gt;new(
        default_h =&gt; [sub { print shift }, 'text'],
        comment_h =&gt; [""],
    )-&gt;parse_file(shift || die)
        || die $!;

An alternative implementation is:

    use HTML::Parser ();
    HTML::Parser-&gt;new(
        end_document_h =&gt; [sub { print shift }, 'skipped_text'],
        comment_h      =&gt; [""],
    )-&gt;parse_file(shift || die)
        || die $!;

This will in most cases be much more efficient since only a single
callback will be made.

The next example prints out the text that is inside the &lt;title&gt;
element of an HTML document.  Here we start by setting up a start
handler.  When it sees the title start tag it enables a text handler
that prints any text found and an end handler that will terminate
parsing as soon as the title end tag is seen:

    use HTML::Parser ();

    sub start_handler {
        return if shift ne "title";
        my $self = shift;
        $self-&gt;handler(text =&gt; sub { print shift }, "dtext");
        $self-&gt;handler(
            end =&gt; sub {
                shift-&gt;eof if shift eq "title";
            },
            "tagname,self"
        );
    }

    my $p = HTML::Parser-&gt;new(api_version =&gt; 3);
    $p-&gt;handler(start =&gt; \&amp;start_handler, "tagname,self");
    $p-&gt;parse_file(shift || die) || die $!;
    print "\n";

On a Debian box, more examples can be found in the
/usr/share/doc/libhtml-parser-perl/examples directory.
The program C&lt;hrefsub&gt; shows how you can edit all links
found in a document and C&lt;htextsub&gt; how to edit the text only; the
program C&lt;hstrip&gt; shows how you can strip out certain tags/elements
and/or attributes; and the program C&lt;htext&gt; show how to obtain the
plain text, but not any script/style content.

You can browse the F&lt;eg/&gt; directory online from the I&lt;[Browse]&gt; link on
the http://search.cpan.org/~gaas/HTML-Parser/ page.

=head1 BUGS

The &lt;style&gt; and &lt;script&gt; sections do not end with the first "&lt;/", but
need the complete corresponding end tag.  The standard behaviour is
not really practical.

When the I&lt;strict_comment&gt; option is enabled, we still recognize
comments where there is something other than whitespace between even
and odd "--" markers.

Once $p-&gt;boolean_attribute_value has been set, there is no way to
restore the default behaviour.

There is currently no way to get both quote characters
into the same literal argspec.

Empty tags, e.g. "&lt;&gt;" and "&lt;/&gt;", are not recognized.  SGML allows them
to repeat the previous start tag or close the previous start tag
respectively.

NET tags, e.g. "code/.../" are not recognized.  This is SGML
shorthand for "&lt;code&gt;...&lt;/code&gt;".

Incomplete start or end tags, e.g. "&lt;tt&lt;b&gt;...&lt;/b&lt;/tt&gt;" are not
recognized.

=head1 DIAGNOSTICS

The following messages may be produced by HTML::Parser.  The notation
in this listing is the same as used in L&lt;perldiag&gt;:

=over

=item Not a reference to a hash

(F) The object blessed into or subclassed from HTML::Parser is not a
hash as required by the HTML::Parser methods.

=item Bad signature in parser state object at %p

(F) The _hparser_xs_state element does not refer to a valid state structure.
Something must have changed the internal value
stored in this hash element, or the memory has been overwritten.

=item _hparser_xs_state element is not a reference

(F) The _hparser_xs_state element has been destroyed.

=item Can't find '_hparser_xs_state' element in HTML::Parser hash

(F) The _hparser_xs_state element is missing from the parser hash.
It was either deleted, or not created when the object was created.

=item API version %s not supported by HTML::Parser %s

(F) The constructor option 'api_version' with an argument greater than
or equal to 4 is reserved for future extensions.

=item Bad constructor option '%s'

(F) An unknown constructor option key was passed to the new() or
init() methods.

=item Parse loop not allowed

(F) A handler invoked the parse() or parse_file() method.
This is not permitted.

=item marked sections not supported

(F) The $p-&gt;marked_sections() method was invoked in a HTML::Parser
module that was compiled without support for marked sections.

=item Unknown boolean attribute (%d)

(F) Something is wrong with the internal logic that set up aliases for
boolean attributes.

=item Only code or array references allowed as handler

(F) The second argument for $p-&gt;handler must be either a subroutine
reference, then name of a subroutine or method, or a reference to an
array.

=item No handler for %s events

(F) The first argument to $p-&gt;handler must be a valid event name; i.e. one
of "start", "end", "text", "process", "declaration" or "comment".

=item Unrecognized identifier %s in argspec

(F) The identifier is not a known argspec name.
Use one of the names mentioned in the argspec section above.

=item Literal string is longer than 255 chars in argspec

(F) The current implementation limits the length of literals in
an argspec to 255 characters.  Make the literal shorter.

=item Backslash reserved for literal string in argspec

(F) The backslash character "\" is not allowed in argspec literals.
It is reserved to permit quoting inside a literal in a later version.

=item Unterminated literal string in argspec

(F) The terminating quote character for a literal was not found.

=item Bad argspec (%s)

(F) Only identifier names, literals, spaces and commas
are allowed in argspecs.

=item Missing comma separator in argspec

(F) Identifiers in an argspec must be separated with ",".

=item Parsing of undecoded UTF-8 will give garbage when decoding entities

(W) The first chunk parsed appears to contain undecoded UTF-8 and one
or more argspecs that decode entities are used for the callback
handlers.

The result of decoding will be a mix of encoded and decoded characters
for any entities that expand to characters with code above 127.  This
is not a good thing.

The recommended solution is to apply Encode::decode_utf8() on the data before
feeding it to the $p-&gt;parse().  For $p-&gt;parse_file() pass a file that has been
opened in ":utf8" mode.

The alternative solution is to enable the C&lt;utf8_mode&gt; and not decode before
passing strings to $p-&gt;parse().  The parser can process raw undecoded UTF-8
sanely if the C&lt;utf8_mode&gt; is enabled, or if the C&lt;attr&gt;, C&lt;@attr&gt; or C&lt;dtext&gt;
argspecs are avoided.

=item Parsing string decoded with wrong endian selection

(W) The first character in the document is U+FFFE.  This is not a
legal Unicode character but a byte swapped C&lt;BOM&gt;.  The result of parsing
will likely be garbage.

=item Parsing of undecoded UTF-32

(W) The parser found the Unicode UTF-32 C&lt;BOM&gt; signature at the start
of the document.  The result of parsing will likely be garbage.

=item Parsing of undecoded UTF-16

(W) The parser found the Unicode UTF-16 C&lt;BOM&gt; signature at the start of
the document.  The result of parsing will likely be garbage.

=back

=head1 SEE ALSO

L&lt;HTML::Entities&gt;, L&lt;HTML::PullParser&gt;, L&lt;HTML::TokeParser&gt;, L&lt;HTML::HeadParser&gt;,
L&lt;HTML::LinkExtor&gt;, L&lt;HTML::Form&gt;

L&lt;HTML::TreeBuilder&gt; (part of the I&lt;HTML-Tree&gt; distribution)

L&lt;http://www.w3.org/TR/html4/&gt;

More information about marked sections and processing instructions may
be found at L&lt;http://www.is-thought.co.uk/book/sgml-8.htm&gt;.

=head1 COPYRIGHT

 Copyright 1996-2016 Gisle Aas. All rights reserved.
 Copyright 1999-2000 Michael A. Chase.  All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut
</pre></body></html>