Skip to content

Commit

Permalink
Merge pull request #15 from gregapompe/master
Browse files Browse the repository at this point in the history
parse_html used only for root templates and only parse for inner snippet...
  • Loading branch information
racke committed Nov 26, 2013
2 parents af5eca0 + e32eefb commit 0f4a728
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 33 deletions.
17 changes: 9 additions & 8 deletions lib/Template/Flute.pm
Expand Up @@ -267,7 +267,7 @@ sub new {
}

sub _bootstrap {
my ($self) = @_;
my ($self, $snippet) = @_;
my ($parser_name, $parser_spec, $spec_file, $spec, $template_file, $template_object);

unless ($self->{specification}) {
Expand All @@ -283,7 +283,7 @@ sub _bootstrap {
$self->_bootstrap_specification(file => $self->{specification_file});
}

$self->_bootstrap_template(file => $self->{template_file});
$self->_bootstrap_template(file => $self->{template_file}, $snippet);
}

sub _bootstrap_specification {
Expand Down Expand Up @@ -339,17 +339,17 @@ sub _bootstrap_specification {
}

sub _bootstrap_template {
my ($self, $source, $template) = @_;
my ($self, $source, $template, $snippet) = @_;
my ($template_object);

$template_object = new Template::Flute::HTML;

if ($source eq 'file') {
$template_object->parse_file($template, $self->{specification});
$template_object->parse_file($template, $self->{specification}, $snippet);
$self->{template} = $template_object;
}
elsif ($source eq 'string') {
$template_object->parse($template, $self->{specification});
$template_object->parse($template, $self->{specification}, $snippet);
$self->{template} = $template_object;
}

Expand All @@ -376,7 +376,7 @@ sub process {


unless ($self->{template}) {
$self->_bootstrap();
$self->_bootstrap($params->{snippet});
}

if ($self->{i18n}) {
Expand All @@ -390,6 +390,7 @@ sub process {
$self->{'values'},
$self->{specification},
$self->{template},

);
my $shtml = $html->sprint;
return $shtml;
Expand All @@ -399,14 +400,14 @@ sub _sub_process {
my ($self, $html, $spec_xml, $values, $spec, $root_template, $count) = @_;
my ($template);
# Use root spec or sub-spec
my $specification = $spec || $self->_bootstrap_specification(string => "<specification>".$spec_xml->sprint."</specification>");
my $specification = $spec || $self->_bootstrap_specification(string => "<specification>".$spec_xml->sprint."</specification>", 1);

if($root_template){
$template = $root_template;
}
else {
$template = new Template::Flute::HTML;
$template->parse("<flutexml>".$html->sprint."</flutexml>", $specification);
$template->parse("<flutexml>".$html->sprint."</flutexml>", $specification, 1);
}

my $classes = $specification->{classes};
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/Flute/Filter/Eol.pm
Expand Up @@ -37,7 +37,7 @@ sub twig {
}

# add HTML linebreak
push (@elts, XML::Twig::Elt->new('br '));
push (@elts, XML::Twig::Elt->new(br => '#EMPTY'));
}

# pop last HTML linebreak
Expand Down
15 changes: 7 additions & 8 deletions lib/Template/Flute/HTML.pm
Expand Up @@ -261,14 +261,14 @@ of a L<Template::Flute::Specification> object SPECOBJECT.
=cut

sub parse {
my ($self, $template, $spec_object) = @_;
my ($self, $template, $spec_object, $snippet) = @_;
my ($object);

if (ref($template) eq 'SCALAR') {
$object = $self->_parse_template($template, $spec_object);
$object = $self->_parse_template($template, $spec_object, $snippet);
}
else {
$object = $self->_parse_template(\$template, $spec_object);
$object = $self->_parse_template(\$template, $spec_object, $snippet);
}

return $object;
Expand All @@ -282,13 +282,13 @@ of a L<Template::Flute::Specification> object SPECOBJECT.
=cut

sub parse_file {
my ($self, $template_file, $spec_object) = @_;
my ($self, $template_file, $spec_object, $snippet) = @_;

return $self->_parse_template($template_file, $spec_object);
return $self->_parse_template($template_file, $spec_object, $snippet);
}

sub _parse_template {
my ($self, $template, $spec_object) = @_;
my ($self, $template, $spec_object, $snippet) = @_;
my ($twig, %twig_args, $xml, $object, $list, $html_content, $encoding);

$object = {specs => {}, lists => {}, forms => {}, params => {}};
Expand All @@ -304,7 +304,6 @@ sub _parse_template {
if (ref($template) eq 'SCALAR') {
$self->{file} = '';
$html_content = decode_entities($$template);
$xml = $twig->parse_html($html_content);
}
else {
$self->{file} = $template;
Expand All @@ -313,8 +312,8 @@ sub _parse_template {
unless ($encoding eq 'utf8') {
$html_content = encode('utf8', $html_content);
}
$xml = $twig->parse_html($html_content);
}
$xml = $snippet ? $twig->parse($html_content) : $twig->parse_html($html_content);

unless ($xml) {
die "Invalid HTML template: $template: $@\n";
Expand Down
2 changes: 1 addition & 1 deletion t/15-entities.t
Expand Up @@ -50,7 +50,7 @@ my $flute = Template::Flute->new(specification => $template_spec,

my $out = $flute->process();

my $expected = q{<html><div id="body">v&amp;r</div><div id="test">  v&amp;r</div><span id="spanning" style="display:none">hello</span></html>};
my $expected = q{<div id="body">v&amp;r</div><div id="test">  v&amp;r</div><span id="spanning" style="display:none">hello</span>};
ok((index($out, $expected) >= 0),
"body rendered");

Expand Down
2 changes: 1 addition & 1 deletion t/filters/eol.t
Expand Up @@ -59,7 +59,7 @@ $flute = Template::Flute->new(specification => $xml,

$ret = $flute->process();

ok($ret =~ m%div class="text"/>%, "Output: $ret");
ok($ret =~ m%div class="text"></div>%, "Output: $ret");

# linebreak filter (leading linebreak)
$xml = <<EOF;
Expand Down
5 changes: 1 addition & 4 deletions t/forms/checkbox.t
Expand Up @@ -2,7 +2,6 @@

use strict;
use warnings;

use Test::More tests => 2;
use Template::Flute;

Expand All @@ -15,14 +14,12 @@ my $spec = <<EOF;
EOF

my $html = <<EOF;
<html>
<form name="colors">
<input type="checkbox" name="color" value="red" />
<input type="checkbox" name="color" value="blue" />
<input type="checkbox" name="color" value="green" />
<input type="submit" value="OK" />
</form>
</html>
EOF

process_form({red => 0, blue => 1, green => 0});
Expand Down Expand Up @@ -56,7 +53,7 @@ sub process_form {
$match = $out;

# match input HTML tags
while ($match =~ s%<input( checked="checked")? name="color" type="checkbox" value="(.*?)"/>%%) {
while ($match =~ s%<input( checked="checked")? name="color" type="checkbox" value="(.*?)" />%%) {
if ($1) {
$colors_found{$2} = 1;
}
Expand Down
9 changes: 4 additions & 5 deletions t/lists/separator.t
@@ -1,6 +1,5 @@
#
# Tests for separators

use strict;
use warnings;

Expand All @@ -20,7 +19,7 @@ $spec = q{<specification>
$iter = [{key => 'FOO'}, {key => 'BAR'}];

# first test: separator outside the list
$html_outside = q{<html><div class="list"><span class="key">KEY</span></div><span class="sep"> | </span></html>};
$html_outside = q{<div class="list"><span class="key">KEY</span></div><span class="sep"> | </span>};

$tf = Template::Flute->new(template => $html_outside,
specification => $spec,
Expand All @@ -29,10 +28,10 @@ $tf = Template::Flute->new(template => $html_outside,

$out = $tf->process();

ok ($out =~ m%<html><div class="list"><span class="key">FOO</span></div><span class="sep"> | </span><div class="list"><span class="key">BAR</span></div></html>%, "Out: $out.");
ok ($out =~ m%<div class="list"><span class="key">FOO</span></div><span class="sep"> | </span><div class="list"><span class="key">BAR</span></div>%, "Out: $out.");

# second test: separator inside the list
$html_inside = q{<html><div class="list"><span class="key">KEY</span><span class="sep"> | </span></div></html>};
$html_inside = q{<div class="list"><span class="key">KEY</span><span class="sep"> | </span></div>};

$tf = Template::Flute->new(template => $html_inside,
specification => $spec,
Expand All @@ -41,4 +40,4 @@ $tf = Template::Flute->new(template => $html_inside,

$out = $tf->process();

ok ($out =~ m%<html><div class="list"><span class="key">FOO</span><span class="sep"> | </span></div><div class="list"><span class="key">BAR</span></div></html>%, "Out: $out.");
ok ($out =~ m%<div class="list"><span class="key">FOO</span><span class="sep"> | </span></div><div class="list"><span class="key">BAR</span></div>%, "Out: $out.");
9 changes: 4 additions & 5 deletions t/values/basic.t
@@ -1,6 +1,5 @@
#
# Basic tests for values

use strict;
use warnings;

Expand Down Expand Up @@ -35,7 +34,7 @@ $spec = q{<specification>
</specification>
};

$html = q{<iframe class="test" src="test"/>};
$html = q{<iframe class="test" src="test"></iframe>};

$flute = Template::Flute->new(template => $html,
specification => $spec,
Expand All @@ -44,23 +43,23 @@ $flute = Template::Flute->new(template => $html,

$out = $flute->process();

ok($out =~ m%<iframe class="test" src="/test.html"/>%, 'basic value target test by class')
ok($out =~ m%<iframe class="test" src="/test.html"></iframe>%, 'basic value target test by class')
|| diag $out;

$spec = q{<specification>
<value name="test" id="test" target="src"/>
</specification>
};

$html = q{<iframe id="test" src="test"/>};
$html = q{<iframe id="test" src="test"></iframe>};

$flute = Template::Flute->new(template => $html,
specification => $spec,
values => {test => '/test.html'});

$out = $flute->process();

ok($out =~ m%<iframe id="test" src="/test.html"/>%, 'basic value target test by id')
ok($out =~ m%<iframe id="test" src="/test.html"></iframe>%, 'basic value target test by id')
|| diag $out;

# test "field"
Expand Down

0 comments on commit 0f4a728

Please sign in to comment.