| 1 | # | ||||||||||||
| 2 | # $Id: libXML.pm,v 0.2 2009/02/21 11:47:58 dankogai Exp dankogai $ | ||||||||||||
| 3 | # | ||||||||||||
| 4 | package HTML::Tidy::libXML; | ||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use XML::LibXML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 13 | 0 | my $class = shift; | |||||||||||
| 14 | 0 | my $lx = XML::LibXML->new; | |||||||||||
| 15 | 0 | $lx->validation(0); | |||||||||||
| 16 | 0 | $lx->recover_silently(1); | |||||||||||
| 17 | 0 | bless { lx => $lx }, $class; | |||||||||||
| 18 | } | ||||||||||||
| 19 | |||||||||||||
| 20 | 0 | ▶ | sub html2dom { | ||||||||||
|
Subroutine
html2dom: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 21 | 0 | my ( $self, $html, $encoding ) = @_; | |||||||||||
| 22 | 0 | ▶ | $encoding ||= 'iso-8859-1'; | ||||||||||
|
Condition0%0/2
$encoding ||= "iso-8859-1"
MC/DC0%0/1
$encoding ||= "iso-8859-1"
$encoding
Truth table0%0/2
A
A= $encoding
| |||||||||||||
| 23 | 0 | $html =~ s/\r\n?/\n/msg; # normalize CRLF to LF | |||||||||||
| 24 | 0 | $html = decode( $encoding, $html ); # leave the utf8 flag | |||||||||||
| 25 | 0 | $self->{lx}->parse_html_string($html); | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub dom2xml { | ||||||||||
|
Subroutine
dom2xml: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 29 | 0 | my ($self, $dom, $level) = @_; | |||||||||||
| 30 | 0 | my $root = $dom->findnodes('/html')->shift; | |||||||||||
| 31 | 0 | $root->setAttribute( xmlns => 'http://www.w3.org/1999/xhtml' ); | |||||||||||
| 32 | 0 | for my $meta ( $dom->findnodes('//meta[@http-equiv!=""]') ) { | |||||||||||
| 33 | 0 | $meta->setAttribute( content => 'text/html; charset=utf-8' ); | |||||||||||
| 34 | } | ||||||||||||
| 35 | 0 | ▶ | _tidy_dom($dom) if $level > 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | my $xhtml = $root->toString( 0, 'utf-8' ); # utf8 flag off | |||||||||||
| 37 | 0 | return <<EOT; | |||||||||||
| 38 | <?xml version="1.0" encoding="UTF-8"?> | ||||||||||||
| 39 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||||||||||||
| 40 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||||||||||||
| 41 | $xhtml | ||||||||||||
| 42 | EOT | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | sub html2xml { | ||||||||||
|
Subroutine
html2xml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 46 | 0 | my ( $self, $html, $encoding, $level ) = @_; | |||||||||||
| 47 | 0 | my $dom = $self->html2dom( $html, $encoding ); | |||||||||||
| 48 | 0 | $self->dom2xml($dom, $level); | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 0 | ▶ | sub _tidy_dom { | ||||||||||
|
Subroutine
_tidy_dom: not called CC=14 SCAR=53.5
| |||||||||||||
| 52 | 0 | my $dom = shift; | |||||||||||
| 53 | # remove empty attributes (like <br clear="">) | ||||||||||||
| 54 | 0 | for my $node ( $dom->findnodes('//*[attribute::*=""]') ) { | |||||||||||
| 55 | 0 | for my $attr ( $node->attributes ) { | |||||||||||
| 56 | 0 | ▶ | next if $attr->getValue; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | $node->removeAttribute( $attr->getName ); | |||||||||||
| 58 | } | ||||||||||||
| 59 | } | ||||||||||||
| 60 | # handle <script> | ||||||||||||
| 61 | 0 | for my $script ( $dom->findnodes('//script') ) { | |||||||||||
| 62 | 0 | ▶ | $script->getAttribute('type') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | or $script->setAttribute( type => "text/javascript" ); | ||||||||||||
| 64 | 0 | ▶ | if ( $script->hasChildNodes ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | $script->insertBefore( $dom->createTextNode("//"), | |||||||||||
| 66 | $script->firstChild ); | ||||||||||||
| 67 | 0 | $script->lastChild->appendData("\n//"); | |||||||||||
| 68 | } | ||||||||||||
| 69 | else { # <script src="..."/> => <script src=""></script> | ||||||||||||
| 70 | 0 | $script->appendChild( $dom->createTextNode("") ); | |||||||||||
| 71 | } | ||||||||||||
| 72 | } | ||||||||||||
| 73 | # handle <style> | ||||||||||||
| 74 | 0 | for my $style ( $dom->findnodes('//style') ) { | |||||||||||
| 75 | 0 | ▶ | $style->getAttribute('type') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | or $style->setAttribute( type => "text/css" ); | ||||||||||||
| 77 | 0 | ▶ | if ( $style->hasChildNodes ) { # this one is trickier | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | $style->insertBefore( $dom->createTextNode("/*"), | |||||||||||
| 79 | $style->firstChild ); | ||||||||||||
| 80 | 0 | $style->lastChild->insertData( 0, "*/" ); | |||||||||||
| 81 | 0 | $style->lastChild->appendData("/*"); | |||||||||||
| 82 | 0 | $style->appendChild( $dom->createTextNode("*/") ); | |||||||||||
| 83 | }else{ | ||||||||||||
| 84 | 0 | $style->appendChild( $dom->createTextNode("") ); | |||||||||||
| 85 | } | ||||||||||||
| 86 | } | ||||||||||||
| 87 | # fix <img> | ||||||||||||
| 88 | 0 | for my $img ( $dom->findnodes('//img') ) { | |||||||||||
| 89 | 0 | ▶ | next if $img->getAttribute('type'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | my $alt = $img->getAttribute('src'); | |||||||||||
| 91 | 0 | $alt =~ s{.*/}{}o; # basename only | |||||||||||
| 92 | 0 | ▶ | $img->setAttribute( alt => $alt || 'img' ); | ||||||||||
|
Condition0%0/2
$alt || 'img'
MC/DC0%0/1
$alt || 'img'
$alt
Truth table0%0/2
A
A= $alt
| |||||||||||||
| 93 | } | ||||||||||||
| 94 | # <a name="foo"/> => <a name="foo"></a> | ||||||||||||
| 95 | 0 | for my $a ( $dom->findnodes('//a[@name!=""]') ) { | |||||||||||
| 96 | 0 | my $empty = $dom->createTextNode(""); | |||||||||||
| 97 | 0 | $a->appendChild($empty); | |||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | { | ||||||||||||
| 102 | 2 | ▶ | no warnings 'once'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 103 | 2 | *clean = \&html2xml; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 2 | ▶ | if ($0 eq __FILE__){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | require LWP::UserAgent; | |||||||||||
| 108 | 0 | require HTTP::Response::Encoding; | |||||||||||
| 109 | 0 | ▶ | my $uri = shift || die; | ||||||||||
|
Condition0%0/2
shift @ARGV || die()
MC/DC0%0/1
shift @ARGV || die()
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
| |||||||||||||
| 110 | 0 | my $res = LWP::UserAgent->new->get($uri); | |||||||||||
| 111 | 0 | ▶ | die $res->status_line unless $res->is_success; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | my $html = __PACKAGE__->new->clean($res->content, $res->encoding, 1); | |||||||||||
| 113 | 0 | print $html; | |||||||||||
| 114 | #my $xl = XML::LibXML->new; | ||||||||||||
| 115 | #$xl->validation(0); | ||||||||||||
| 116 | #print $xl->parse_string($html)->toString; | ||||||||||||
| 117 | #require XML::Simple; | ||||||||||||
| 118 | #require Data::Dumper; | ||||||||||||
| 119 | #print Data::Dumper::Dumper(XML::Simple::XMLin($html)) | ||||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 2 | 1; # End of HTML::Tidy::libXML | |||||||||||
| 123 | |||||||||||||
| 124 | __END__ | ||||||||||||