Navigation Menu

Skip to content

Commit

Permalink
made tag helper faster again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 25, 2015
1 parent a03f544 commit ee38a39
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -5,6 +5,9 @@ use Mojo::ByteStream;
use Mojo::DOM::HTML;
use Scalar::Util 'blessed';

# XML mode
our $_XML;

sub register {
my ($self, $app) = @_;

Expand All @@ -18,15 +21,15 @@ sub register {
sub { _input(shift, shift, value => shift, @_, type => 'checkbox') });
$app->helper(csrf_field => \&_csrf_field);
$app->helper(file_field =>
sub { _tag(shift, 'input', name => shift, @_, type => 'file') });
sub { shift; _tag('input', name => shift, @_, type => 'file') });

$app->helper(form_for => \&_form_for);
$app->helper(hidden_field => \&_hidden_field);
$app->helper(image => \&_image);
$app->helper(input_tag => sub { _input(@_) });
$app->helper(javascript => \&_javascript);
$app->helper(label_for => \&_label_for);
$app->helper(link_to => \&_link_to);
$app->helper(image => sub { _tag('img', src => shift->url_for(shift), @_) });
$app->helper(input_tag => sub { _input(@_) });
$app->helper(javascript => \&_javascript);
$app->helper(label_for => \&_label_for);
$app->helper(link_to => \&_link_to);

$app->helper(password_field => \&_password_field);
$app->helper(radio_button =>
Expand All @@ -37,11 +40,11 @@ sub register {
$app->helper(submit_button => \&_submit_button);

# "t" is just a shortcut for the "tag" helper
$app->helper($_ => \&_tag) for qw(t tag);
$app->helper($_ => sub { shift; _tag(@_) }) for qw(t tag);

$app->helper(tag_with_error => \&_tag_with_error);
$app->helper(text_area => \&_text_area);
$app->helper(xml => sub { local shift->stash->{'mojo.xml'} = 1; shift->() });
$app->helper(xml => sub { local $_XML = 1; pop->() });
}

sub _csrf_field {
Expand All @@ -64,16 +67,12 @@ sub _form_for {
@post = (method => 'POST') if $methods{POST} && !$methods{GET};
}

return _tag($c, 'form', action => $c->url_for(@url), @post, @_);
return _tag('form', action => $c->url_for(@url), @post, @_);
}

sub _hidden_field {
_tag(shift, 'input', name => shift, value => shift, @_, type => 'hidden');
}

sub _image {
my $c = shift;
return _tag($c, 'img', src => $c->url_for(shift), @_);
return _tag('input', name => shift, value => shift, @_, type => 'hidden');
}

sub _input {
Expand Down Expand Up @@ -103,7 +102,7 @@ sub _javascript {
my $content
= ref $_[-1] eq 'CODE' ? "//<![CDATA[\n" . pop->() . "\n//]]>" : '';
my @src = @_ % 2 ? (src => $c->url_for(shift)) : ();
return _tag($c, 'script', @src, @_, sub {$content});
return _tag('script', @src, @_, sub {$content});
}

sub _label_for {
Expand All @@ -125,15 +124,15 @@ sub _link_to {
# Captures
push @url, shift if ref $_[0] eq 'HASH';

return _tag($c, 'a', href => $c->url_for(@url), @_);
return _tag('a', href => $c->url_for(@url), @_);
}

sub _option {
my ($c, $values, $pair) = @_;
my ($values, $pair) = @_;
$pair = [$pair => $pair] unless ref $pair eq 'ARRAY';
my %attrs = (value => $pair->[1]);
$attrs{selected} = undef if exists $values->{$pair->[1]};
return _tag($c, 'option', %attrs, @$pair[2 .. $#$pair], $pair->[0]);
return _tag('option', %attrs, @$pair[2 .. $#$pair], $pair->[0]);
}

sub _password_field {
Expand All @@ -153,12 +152,12 @@ sub _select_field {
# "optgroup" tag
if (blessed $group && $group->isa('Mojo::Collection')) {
my ($label, $values, %attrs) = @$group;
my $content = join '', map { _option($c, \%values, $_) } @$values;
$groups .= _tag($c, 'optgroup', label => $label, %attrs, sub {$content});
my $content = join '', map { _option(\%values, $_) } @$values;
$groups .= _tag('optgroup', label => $label, %attrs, sub {$content});
}

# "option" tag
else { $groups .= _option($c, \%values, $group) }
else { $groups .= _option(\%values, $group) }
}

return _validation($c, $name, 'select', name => $name, %attrs,
Expand All @@ -169,16 +168,17 @@ sub _stylesheet {
my $c = shift;
my $content
= ref $_[-1] eq 'CODE' ? "/*<![CDATA[*/\n" . pop->() . "\n/*]]>*/" : '';
return _tag($c, 'style', @_, sub {$content}) unless @_ % 2;
return _tag($c, 'link', rel => 'stylesheet', href => $c->url_for(shift), @_);
return _tag('style', @_, sub {$content}) unless @_ % 2;
return _tag('link', rel => 'stylesheet', href => $c->url_for(shift), @_);
}

sub _submit_button {
_tag(shift, 'input', value => shift // 'Ok', @_, type => 'submit');
my $c = shift;
return _tag('input', value => shift // 'Ok', @_, type => 'submit');
}

sub _tag {
my ($c, $tree) = (shift, ['tag', shift, undef, undef]);
my $tree = ['tag', shift, undef, undef];

# Content
if (ref $_[-1] eq 'CODE') { push @$tree, ['raw', pop->()] }
Expand All @@ -194,15 +194,14 @@ sub _tag {
delete $attrs->{data};
}

return Mojo::ByteStream->new(
Mojo::DOM::HTML::_render($tree, $c->stash->{'mojo.xml'}));
return Mojo::ByteStream->new(Mojo::DOM::HTML::_render($tree, $_XML));
}

sub _tag_with_error {
my ($c, $tag) = (shift, shift);
my ($content, %attrs) = (@_ % 2 ? pop : undef, @_);
$attrs{class} .= $attrs{class} ? ' field-with-error' : 'field-with-error';
return _tag($c, $tag, %attrs, defined $content ? $content : ());
return _tag($tag, %attrs, defined $content ? $content : ());
}

sub _text_area {
Expand All @@ -217,7 +216,7 @@ sub _text_area {

sub _validation {
my ($c, $name) = (shift, shift);
return _tag($c, @_) unless $c->validation->has_error($name);
return _tag(@_) unless $c->validation->has_error($name);
return $c->helpers->tag_with_error(@_);
}

Expand Down

0 comments on commit ee38a39

Please sign in to comment.