| 1 | package HTML::Doctype; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package HTML::Doctype::Detector; | ||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub new | ||||||||||||||||||||||||||||
| 13 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 14 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 0 | my $p = shift; | |||||||||||||||||||||||||||
| 16 | 0 | bless { p => $p }, $class; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub _type | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_type: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 23 | 0 | my $doct = shift; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $p = $self->{p}; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | $self->{type} = $type; | |||||||||||||||||||||||||||
| 28 | 0 | $self->{location} = $p->get_location; | |||||||||||||||||||||||||||
| 29 | 0 | $self->{doctype} = $doct; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | $p->halt; | |||||||||||||||||||||||||||
| 32 | 0 | return; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub public_id { shift->{doctype}{ExternalId}{PublicId} } | ||||||||||||||||||||||||||
|
Subroutine
public_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub system_id { shift->{doctype}{ExternalId}{SystemId} } | ||||||||||||||||||||||||||
|
Subroutine
system_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub has_doctype { defined $_[0]->{doctype} } | ||||||||||||||||||||||||||
|
Subroutine
has_doctype: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub is_xhtml { defined $_[0]->{type} and $_[0]->{type} eq "XHTML" } | ||||||||||||||||||||||||||
|
Subroutine
is_xhtml: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # fails if | ||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||
| 43 | # * the first [ in the decl does not open the internal subset | ||||||||||||||||||||||||||||
| 44 | # * the internal subset contains ]> | ||||||||||||||||||||||||||||
| 45 | # * comments outside the internal subset contain > | ||||||||||||||||||||||||||||
| 46 | # * the internal subset is not closed with ]> | ||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||
| 48 | sub doctype_length | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
doctype_length: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 0 | my $document = shift; | |||||||||||||||||||||||||||
| 52 | 0 | my $doctyped = $self->{doctype}; | |||||||||||||||||||||||||||
| 53 | 0 | my $location = $self->{location}; | |||||||||||||||||||||||||||
| 54 | 0 | my $gtpos = 0; | |||||||||||||||||||||||||||
| 55 | 0 | my $gtskip = 0; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # no document type declaration | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return $gtpos unless defined $doctyped; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # < of <!DOCTYPE ... | ||||||||||||||||||||||||||||
| 61 | 0 | my $ltpos = $location->{EntityOffset} - 9; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $gtpos = index $document, ">", $ltpos; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # malformed doctype, missing > | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | return if $gtpos < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | $gtskip += $doctyped->{ExternalId}{PublicId} =~ />/g | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if defined $doctyped and exists $doctyped->{ExternalId}{PublicId}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $doctyped and exists $doctyped->{"ExternalId"}{"PublicId"}
MC/DC0%0/2
defined $doctyped and exists $doctyped->{"ExternalId"}{"PublicId"}
defined $doctyped
exists $doctyped->{"ExternalId"}{"PublicId"}
Truth table0%0/3
A and B
A= defined $doctypedB= exists $doctyped->{"ExternalId"}{"PublicId"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $gtskip += $doctyped->{ExternalId}{SystemId} =~ />/g | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if defined $doctyped and exists $doctyped->{ExternalId}{SystemId}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $doctyped and exists $doctyped->{"ExternalId"}{"SystemId"}
MC/DC0%0/2
defined $doctyped and exists $doctyped->{"ExternalId"}{"SystemId"}
defined $doctyped
exists $doctyped->{"ExternalId"}{"SystemId"}
Truth table0%0/3
A and B
A= defined $doctypedB= exists $doctyped->{"ExternalId"}{"SystemId"}
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $gtpos = index $document, ">", $gtpos # +1 ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | while $gtskip--; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # malformed doctype, missing proper > | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return if $gtpos < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # extract possible doctype | ||||||||||||||||||||||||||||
| 81 | 0 | my $text = substr $document, $ltpos, $gtpos - $ltpos + 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # look for ]> if suspected internal subset | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (index($text, "[") >= 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 0 | my $gtpos2 = index $document, "]>", $ltpos; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $gtpos = $gtpos2 + 1 if $gtpos2 >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $gtpos - $ltpos + 1 | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub start_dtd | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
start_dtd: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 0 | my $doct = shift; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # ignore specified document type declarations without | ||||||||||||||||||||||||||||
| 99 | # public or system identifier and implied document type | ||||||||||||||||||||||||||||
| 100 | # declarations (which have just a GeneratedSystemId key) | ||||||||||||||||||||||||||||
| 101 | return unless exists $doct->{ExternalId}{PublicId} or | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | exists $doct->{ExternalId}{SystemId}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $doct->{"ExternalId"}{"PublicId"} or exists $doct->{"ExternalId"}{"SystemId"}
MC/DC0%0/2
exists $doct->{"ExternalId"}{"PublicId"} or exists $doct->{"ExternalId"}{"SystemId"}
exists $doct->{"ExternalId"}{"PublicId"}
exists $doct->{"ExternalId"}{"SystemId"}
Truth table0%0/3
A or B
A= exists $doct->{"ExternalId"}{"PublicId"}B= exists $doct->{"ExternalId"}{"SystemId"}
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $puid = $doct->{ExternalId}{PublicId}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # no public identifier means HTML | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return $self->_type("HTML", $doct) unless defined $puid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # split public identifier at // | ||||||||||||||||||||||||||||
| 110 | 0 | my @comp = split(/\/\//, $puid); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # malformed public identifiers mean HTML | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return $self->_type("HTML", $doct) unless @comp > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # we might want something different than \s and \S here | ||||||||||||||||||||||||||||
| 116 | # but it is not clear to me what exactly we should expect | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return $self->_type("HTML", $doct) unless $comp[2] =~ /^DTD\s+(\S+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # the first token of the public text description must include | ||||||||||||||||||||||||||||
| 120 | # the string "XHTML", see XHTML M12N section 3.1, and see also | ||||||||||||||||||||||||||||
| 121 | # http://w3.org/mid/41584c61.156809450@smtp.bjoern.hoehrmann.de | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return $self->_type("HTML", $doct) unless $1 =~ /XHTML/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # otherwise considers this document XHTML | ||||||||||||||||||||||||||||
| 125 | 0 | return $self->_type("XHTML", $doct) | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub start_element | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
start_element: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 130 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 0 | my $elem = shift; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # no xmlns attribute means HTML | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return $self->_type("HTML") unless exists $elem->{Attributes}{XMLNS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $xmlns = $elem->{Attributes}{XMLNS}; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # this should use the corresponding helper function to deal | ||||||||||||||||||||||||||||
| 139 | # with some potential edge cases but it is not in CVS yet | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return $self->_type("HTML") unless $xmlns->{Defaulted} eq "specified"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # see above | ||||||||||||||||||||||||||||
| 143 | # return $self->_type("HTML") unless "http://www.w3.org/1999/xhtml" eq | ||||||||||||||||||||||||||||
| 144 | # join '', map { $_->{Data} } @{$xmlns->{CdataChunks}}; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | return $self->_type("XHTML") | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||