| 1 | package CSS::Inliner::TreeBuilder; | ||||||||||
| 2 | 26 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 26 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 26 | ▶ | use Storable qw(dclone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | ▶ | BEGIN { | |||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | # $HTML::TreeBuilder::DEBUG = 1; | ||||||||||
| 9 | } | ||||||||||
| 10 | |||||||||||
| 11 | 26 | ▶ | use base qw(HTML::TreeBuilder); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =pod | ||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | CSS::Inliner::TreeBuilder - Parser that builds a HTML syntax tree | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use CSS::Inliner::TreeBuilder; | ||||||||||
| 22 | |||||||||||
| 23 | foreach my $file_name (@ARGV) { | ||||||||||
| 24 | my $tree = CSS::Inliner::TreeBuilder->new(); | ||||||||||
| 25 | $tree->parse_file($file_name); | ||||||||||
| 26 | |||||||||||
| 27 | print "Hey, here's a dump of the parse tree of $file_name:\n"; | ||||||||||
| 28 | $tree->dump(); # a method we inherit from HTML::Element | ||||||||||
| 29 | print "And here it is, bizarrely rerendered as HTML:\n", $tree->as_HTML, "\n"; | ||||||||||
| 30 | |||||||||||
| 31 | $tree = $tree->delete(); | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||
| 35 | |||||||||||
| 36 | Class to handling parsing of generic HTML | ||||||||||
| 37 | |||||||||||
| 38 | This sub-module is derived from HTML::TreeBuilder. The aforementioned module is almost completely incapable | ||||||||||
| 39 | of handling non-standard HTML4 documents commonly seen in the wild, let alone HTML5 documents. This module | ||||||||||
| 40 | basically performs some minor adjustments to the way parsing and printing occur such that an acceptable result | ||||||||||
| 41 | can be reached when handling real world documents. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | sub as_HTML { | ||||||||||
| 46 | 25 | ▶ | my $self = shift; | ||||||||
|
Subroutine
as_HTML: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 47 | |||||||||||
| 48 | 25 | my $html; | |||||||||
| 49 | 25 | ▶ | if ($self->implicit_tags() == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 1 | $html = $self->SUPER::as_HTML(@_); | |||||||||
| 51 | |||||||||||
| 52 | #strip trailing and leading whitespace which our relaxed mode may have | ||||||||||
| 53 | #inadvertently adds | ||||||||||
| 54 | 1 | $html =~ s/^\s+|\s+$//g; | |||||||||
| 55 | |||||||||||
| 56 | # our indentation is messed up by 1 space, try to clean it up | ||||||||||
| 57 | 1 | my @lines = split /\n/, $html; | |||||||||
| 58 | 1 | ▶ | for (my $count = 0; $count < scalar @lines; $count++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 9 | $lines[$count] =~ s/^ //; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | # put html back together after whitespace processing, probably still indentation | ||||||||||
| 63 | # problems, but this is the best we can do without some sort of indentation library | ||||||||||
| 64 | 1 | $html = join("\n",@lines); | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 24 | $html = $self->SUPER::as_HTML(@_); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 25 | return $html; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub parse_content { | ||||||||||
| 74 | 52 | ▶ | my $self = shift; | ||||||||
|
Subroutine
parse_content: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | 52 | ▶ | if ($self->implicit_tags() == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | # protect doctype declarations... parser is too strict here | ||||||||||
| 78 | 28 | $_[0] =~ s/\<!(doctype) ([^>]+)\>/\<decl ~pi="1" \>$1 $2<\/decl\>/gi; | |||||||||
| 79 | |||||||||||
| 80 | 28 | $self->SUPER::parse_content(@_); | |||||||||
| 81 | |||||||||||
| 82 | 28 | $self->{_tag} = '~literal'; | |||||||||
| 83 | 28 | $self->{text} = ''; | |||||||||
| 84 | |||||||||||
| 85 | 28 | my @decls = $self->look_down('_tag','decl','~pi','1'); | |||||||||
| 86 | 28 | foreach my $decl (@decls) { | |||||||||
| 87 | 6 | my $text = '<!' . $decl->as_text() . '>'; | |||||||||
| 88 | 6 | my $literal = HTML::Element->new('~literal', 'text' => $text ); | |||||||||
| 89 | |||||||||||
| 90 | 6 | $decl->replace_with($literal); | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | else { | ||||||||||
| 94 | 24 | $self->SUPER::parse_content(@_); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 52 | return(); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 26 | 1; | |||||||||