Permalink
Browse files

add it

  • Loading branch information...
1 parent 0a9db31 commit 5c6ae4cdd25eddf308564e4c0d6f532c62fd79d0 @jrockway committed Jul 7, 2008
Showing with 132 additions and 5 deletions.
  1. +4 −0 Makefile.PL
  2. +47 −5 lib/HTML/Document.pm
  3. +29 −0 lib/HTML/Document/Header.pm
  4. +52 −0 t/document-basic.t
View
@@ -3,6 +3,10 @@ use inc::Module::Install;
name 'HTML-Document';
all_from 'lib/HTML/Document.pm';
+requires 'Moose';
+requires 'XML::LibXML';
+build_requires 'Test::Exception';
+build_requires 'Test::LongString';
build_requires 'Test::More';
build_requires 'ok';
View
@@ -1,11 +1,53 @@
package HTML::Document;
-use strict;
-use warnings;
+use Moose;
+use XML::LibXML;
+use HTML::Document::Header;
-=head1 NAME
+has header => (
+ is => 'ro',
+ isa => 'HTML::Document::Header',
+ required => 1,
+);
-HTML::Document -
+has document => (
+ init_arg => undef,
+ is => 'ro',
+ isa => 'XML::LibXML::Document',
+ lazy => 1,
+ builder => '_initial_doc',
+);
-=cut
+sub _initial_doc {
+ my $self = shift;
+ my $head_str = $self->header->toString;
+ return XML::LibXML->new->parse_html_string(
+ qq{<html><head>$head_str</head><body /></html>}
+ );
+}
+
+sub add_fragment {
+ my ($self, @fragments) = @_;
+ my $doc = $self->document;
+ my ($body) = $doc->getElementsByTagName('body');
+
+ for my $frag (@fragments){
+ if(!eval{ $frag->isa('XML::LibXML::Node') }){
+ $body->appendChild($_) for (
+ [ XML::LibXML->new->parse_html_string($frag)->
+ getElementsByTagName('body')
+ ]->[0]->childNodes
+ );
+ }
+ else {
+ $body->appendChild($frag);
+ }
+ }
+ return;
+}
+
+sub render {
+ my $self = shift;
+ return $self->document->toStringHTML;
+}
1;
@@ -0,0 +1,29 @@
+package HTML::Document::Header;
+use Moose;
+
+has 'css' => (
+ is => 'ro',
+ isa => 'ArrayRef[Str]',
+ default => sub { [] },
+);
+
+has 'javascript' => (
+ is => 'ro',
+ isa => 'ArrayRef[Str]',
+ default => sub { [] },
+);
+
+has 'title' => (
+ is => 'ro',
+ isa => 'Str',
+ required => 1,
+);
+
+sub toString {
+ my ($self) = @_;
+ my $title = $self->title;
+ # XXX: hack
+ return "<title>$title</title>";
+}
+
+1;
View
@@ -0,0 +1,52 @@
+use strict;
+use warnings;
+use Test::More tests => 9;
+use Test::LongString;
+use Test::Exception;
+
+use XML::LibXML;
+
+use ok 'HTML::Document';
+use ok 'HTML::Document::Header';
+
+my $h = HTML::Document::Header->new(
+ css => [],
+ javascript => [],
+ title => 'Hello, world!',
+);
+isa_ok $h, 'HTML::Document::Header';
+
+my $t = HTML::Document->new( header => $h );
+isa_ok $t, 'HTML::Document';
+
+is_string $t->render, '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
+<html>
+<head><title>Hello, world!</title></head>
+<body></body>
+</html>
+';
+
+my @frags = map { XML::LibXML->new->parse_balanced_chunk($_) } (
+ '<p>This is a test</p>',
+ '<p>of the document!</p>',
+);
+
+isa_ok $frags[0], 'XML::LibXML::DocumentFragment';
+isa_ok $frags[1], 'XML::LibXML::DocumentFragment';
+
+lives_ok {
+ $t->add_fragment(@frags, '<p>And another paragraph</p><p class="foo">Or two</p>');
+} 'adding fragments lives';
+
+is_string $t->render, <<EOHTML;
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
+<html>
+<head><title>Hello, world!</title></head>
+<body>
+<p>This is a test</p>
+<p>of the document!</p>
+<p>And another paragraph</p>
+<p class="foo">Or two</p>
+</body>
+</html>
+EOHTML

0 comments on commit 5c6ae4c

Please sign in to comment.