X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FTemplate%2FPlugin%2FJavaScript.pm;h=16989e9a2d84f9e3396d72c1f86862c5663be2b5;hb=8be0f8a7ff52242c1b38345541796f66891735fa;hp=467c59111a1cae3f6635594e54e8943ba2075a83;hpb=518ed6b5bd287b66ea4d8495d319640541878dea;p=kivitendo-erp.git diff --git a/SL/Template/Plugin/JavaScript.pm b/SL/Template/Plugin/JavaScript.pm index 467c59111..16989e9a2 100644 --- a/SL/Template/Plugin/JavaScript.pm +++ b/SL/Template/Plugin/JavaScript.pm @@ -3,22 +3,152 @@ package SL::Template::Plugin::JavaScript; use base qw( Template::Plugin ); use Template::Plugin; +use strict; + sub new { - my $class = shift; - my $context = shift; + my ($class, $context, @args) = @_; - bless { }, $class; + return bless { + CONTEXT => $context, + }, $class; } +# +# public interface +# + +# see ecmascript spec section 7.8.4 +my @escape_chars = ('\\', '\'', '"'); +my %control_chars = ( + "\n" => 'n', + "\t" => 't', + "\r" => 'r', + "\f" => 'f', + "\x08" => 'b', + "\x0B" => 'v', # noone uses vertical tab anyway... +); +my $re = join '', map { qr/($_)/s } join '|', keys(%control_chars), map { "\Q$_\E" } @escape_chars; + sub escape { my $self = shift; my $text = shift; - $text =~ s|\"|\\\"|g; + $text =~ s/$re/'\\' . ($control_chars{$1} || $1)/egs; return $text; } +sub replace_with { + return _replace_helper('replaceWith', @_); +} + +sub replace_html_with { + return _replace_helper('html', @_); +} + +# +# private methods +# + +sub _context { + die 'not an accessor' if @_ > 1; + return $_[0]->{CONTEXT}; +} + +sub _replace_helper { + my ($method, $self, $selector, $template, $locals) = @_; + + $template .= '.html' unless $template =~ m/\.html$/; + my $html = $self->escape($self->_context->process($template, %{ $locals || { } })); + my $code = < + +Returns C<$value> escaped for inclusion in a JavaScript string. The +value is not wrapped in quotes. Example: + + + +=item C + +Returns code replacing the DOM elements matched by C<$selector> with +the content rendered by Template's I directive applied to +C<$template>. C<%locals> are passed as local parameters to I. + +Uses jQuery's C function. Requires jQuery to be loaded. + +Example: + +
TODO:
+
    +
  • First item
  • +
  • Second item
  • +
  • Another item
  • +
+ + + + + +=item C + +Returns code replacing the inner HTML of the DOM elements matched by +C<$selector> with the content rendered by Template's I +directive applied to C<$template>. C<%locals> are passed as local +parameters to I. + +Uses jQuery's C function. Requires jQuery to be loaded. + +
TODO:
+
    +
  • First item
  • +
  • Second item
  • +
  • Another item
  • +
+ + + + + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut