Skip to content

Instantly share code, notes, and snippets.

/xml_tag.diff Secret

Created January 20, 2015 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0fa6569643dc358a4474 to your computer and use it in GitHub Desktop.
Save anonymous/0fa6569643dc358a4474 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious/Plugin/TagHelpers.pm b/lib/Mojolicious/Plugin/TagHelpers.pm
index f4522e6..5a7afa5 100644
--- a/lib/Mojolicious/Plugin/TagHelpers.pm
+++ b/lib/Mojolicious/Plugin/TagHelpers.pm
@@ -18,11 +18,12 @@ sub register {
sub { _input(shift, shift, value => shift, @_, type => 'checkbox') });
$app->helper(csrf_field => \&_csrf_field);
$app->helper(file_field =>
- sub { shift; _tag('input', name => shift, @_, type => 'file') });
+ sub { shift; _tag(0, 'input', name => shift, @_, type => 'file') });
$app->helper(form_for => \&_form_for);
$app->helper(hidden_field => \&_hidden_field);
- $app->helper(image => sub { _tag('img', src => shift->url_for(shift), @_) });
+ $app->helper(
+ image => sub { _tag(0, 'img', src => shift->url_for(shift), @_) });
$app->helper(input_tag => sub { _input(@_) });
$app->helper(javascript => \&_javascript);
$app->helper(label_for => \&_label_for);
@@ -37,10 +38,11 @@ sub register {
$app->helper(submit_button => \&_submit_button);
# "t" is just a shortcut for the "tag" helper
- $app->helper($_ => sub { shift; _tag(@_) }) for qw(t tag);
+ $app->helper($_ => sub { shift; _tag(0, @_) }) for qw(t tag);
$app->helper(tag_with_error => \&_tag_with_error);
$app->helper(text_area => \&_text_area);
+ $app->helper(xml => sub { shift; _tag(1, @_) });
}
sub _csrf_field {
@@ -63,12 +65,12 @@ sub _form_for {
@post = (method => 'POST') if $methods{POST} && !$methods{GET};
}
- return _tag('form', action => $c->url_for(@url), @post, @_);
+ return _tag(0, 'form', action => $c->url_for(@url), @post, @_);
}
sub _hidden_field {
my $c = shift;
- return _tag('input', name => shift, value => shift, @_, type => 'hidden');
+ return _tag(0, 'input', name => shift, value => shift, @_, type => 'hidden');
}
sub _input {
@@ -106,7 +108,7 @@ sub _javascript {
# URL
my $src = @_ % 2 ? $c->url_for(shift) : undef;
- return _tag('script', @_, $src ? (src => $src) : (), $cb);
+ return _tag(0, 'script', @_, $src ? (src => $src) : (), $cb);
}
sub _label_for {
@@ -128,7 +130,7 @@ sub _link_to {
# Captures
push @url, shift if ref $_[0] eq 'HASH';
- return _tag('a', href => $c->url_for(@url), @_);
+ return _tag(0, 'a', href => $c->url_for(@url), @_);
}
sub _option {
@@ -140,7 +142,7 @@ sub _option {
$attrs{selected} = undef if exists $values->{$pair->[1]};
%attrs = (%attrs, @$pair[2 .. $#$pair]);
- return _tag('option', %attrs, $pair->[0]);
+ return _tag(0, 'option', %attrs, $pair->[0]);
}
sub _password_field {
@@ -161,7 +163,7 @@ sub _select_field {
if (blessed $group && $group->isa('Mojo::Collection')) {
my ($label, $values, %attrs) = @$group;
my $content = join '', map { _option(\%values, $_) } @$values;
- $groups .= _tag('optgroup', label => $label, %attrs, sub {$content});
+ $groups .= _tag(0, 'optgroup', label => $label, %attrs, sub {$content});
}
# "option" tag
@@ -184,17 +186,17 @@ sub _stylesheet {
# "link" or "style" tag
my $href = @_ % 2 ? $c->url_for(shift) : undef;
return $href
- ? _tag('link', rel => 'stylesheet', href => $href, @_)
- : _tag('style', @_, $cb);
+ ? _tag(0, 'link', rel => 'stylesheet', href => $href, @_)
+ : _tag(0, 'style', @_, $cb);
}
sub _submit_button {
my $c = shift;
- return _tag('input', value => shift // 'Ok', @_, type => 'submit');
+ return _tag(0, 'input', value => shift // 'Ok', @_, type => 'submit');
}
sub _tag {
- my $tree = ['tag', shift, undef, undef];
+ my ($xml, $tree) = (shift, ['tag', shift, undef, undef]);
# Content
if (ref $_[-1] eq 'CODE') { push @$tree, ['raw', pop->()] }
@@ -210,14 +212,14 @@ sub _tag {
delete $attrs->{data};
}
- return Mojo::ByteStream->new(Mojo::DOM::HTML::_render($tree));
+ 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($tag, %attrs, defined $content ? $content : ());
+ return _tag(0, $tag, %attrs, defined $content ? $content : ());
}
sub _text_area {
@@ -232,7 +234,7 @@ sub _text_area {
sub _validation {
my ($c, $name) = (shift, shift);
- return _tag(@_) unless $c->validation->has_error($name);
+ return _tag(0, @_) unless $c->validation->has_error($name);
return $c->helpers->tag_with_error(@_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment