From: Sven Schöling Date: Fri, 26 Mar 2021 18:19:47 +0000 (+0100) Subject: Presenter::EscapedText: escape_js_calls X-Git-Tag: kivitendo-mebil_0.1-0~9^2~110 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=1e0c26795a315e10345a6aafc0ca9b5aa42a0755;p=kivitendo-erp.git Presenter::EscapedText: escape_js_calls Helferfunktion um das manuelle Zusammenstückeln von onclick Callbacks zu erleichtern. --- diff --git a/SL/Presenter/EscapedText.pm b/SL/Presenter/EscapedText.pm index ca3c8ca62..1c92f6fa9 100644 --- a/SL/Presenter/EscapedText.pm +++ b/SL/Presenter/EscapedText.pm @@ -2,8 +2,9 @@ package SL::Presenter::EscapedText; use strict; use Exporter qw(import); +use Scalar::Util qw(looks_like_number); -our @EXPORT_OK = qw(escape is_escaped escape_js); +our @EXPORT_OK = qw(escape is_escaped escape_js escape_js_call); our %EXPORT_TAGS = (ALL => \@EXPORT_OK); use JSON (); @@ -54,6 +55,20 @@ sub escape_js { __PACKAGE__->new(text => $text, is_escaped => 1); } +sub escape_js_call { + my ($func, @args) = @_; + + escape( + sprintf "%s(%s)", + escape_js($func), + join ", ", map { + looks_like_number($_) + ? $_ + : '"' . escape_js($_) . '"' + } @args + ); +} + # internal magic sub escaped_text { my ($self) = @_; @@ -141,6 +156,18 @@ Static constructor, can be exported. Equivalent to calling C<< new(text => $text Static constructor, can be exported. Like C but also escapes Javascript. +=item C + +Static constructor, can be exported. Used to construct a javascript call than +can be used for onclick handlers in other Presenter functions. + +For example: + + L.button_tag( + P.escape_js_call("kivi.Package.some_func", arg_one, arg_two, arg_three) + title + ) + =back =head1 METHODS