HTML-Parser

 view release on metacpan or  search on metacpan

t/argspec.t  view on Meta::CPAN

use strict;
use warnings;

use HTML::Parser ();
use Test::More;

my $decl  = '<!ENTITY nbsp   CDATA "&#160;" -- no-break space -->';
my $com1  = '<!-- Comment -->';
my $com2  = '<!-- Comment -- -- Comment -->';
my $start = '<a href="foo">';
my $end   = '</a>';
my $empty = "<IMG SRC='foo'/>";
my $proc  = '<? something completely different ?>';

my @argspec = qw(
    self offset length event tagname tag token0 text
    is_cdata dtext tokens tokenpos attr attrseq
);

my @result;
my $p = HTML::Parser->new(
    default_h      => [\@result, join(',', @argspec)],
    strict_comment => 1,
    xml_mode       => 1
);

my @tests = (    # string, expected results
    $decl => [
        [
            $p, 0, 52, 'declaration', 'ENTITY', '!ENTITY', 'ENTITY',
            '<!ENTITY nbsp   CDATA "&#160;" -- no-break space -->', undef,
            undef,
            ['ENTITY', 'nbsp', 'CDATA', '"&#160;"', '-- no-break space --'],
            [2, 6, 9, 4, 16, 5, 22, 8, 31, 20], undef, undef
        ]
    ],
    $com1 => [
        [
            $p, 0, 16, 'comment', ' Comment ', '# Comment ', ' Comment ',
            '<!-- Comment -->',
            undef, undef, [' Comment '], [4, 9], undef, undef
        ]
    ],
    $com2 => [
        [
            $p,          0,
            30,          'comment',
            ' Comment ', '# Comment ',
            ' Comment ', '<!-- Comment -- -- Comment -->',
            undef,       undef,
            [' Comment ', ' Comment '], [4, 9, 18, 9],
            undef, undef
        ]
    ],
    $start => [
        [
            $p,    0,
            14,    'start',
            'a',   'a',
            'a',   '<a href="foo">',
            undef, undef,
            ['a', 'href', '"foo"'], [1, 1, 3, 4, 8, 5],
            {'href', 'foo'}, ['href']
        ]
    ],
    $end => [
        [
            $p,    0,     4,     'end',  'a',   '/a', 'a', '</a>',
            undef, undef, ['a'], [2, 1], undef, undef
        ]
    ],
    $empty => [
        [
            $p,    0,
            16,    'start',
            'IMG', 'IMG',
            'IMG', "<IMG SRC='foo'/>",



( run in 0.604 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )