lib/CSS/Inliner/TreeBuilder.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 52 / 52 SCAR 16.1
CC
5
Cov
100%
as_HTML
3.0
parse_content
3.0
BEGIN
1.0
CRAP 5.0
« blib/lib/CSS/Inliner/TreeBuilder.pm ↑ Summary
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
BranchTrueFalseTotal
if ($self->implicit_tags == 0) { } 1 24 25
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
BranchTrueFalseTotal
if ($count < scalar @lines) 9 1 10
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
BranchTrueFalseTotal
if ($self->implicit_tags == 0) { } 28 24 52
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;
« blib/lib/CSS/Inliner/TreeBuilder.pm ↑ Summary