From 1e0c26795a315e10345a6aafc0ca9b5aa42a0755 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Fri, 26 Mar 2021 19:19:47 +0100 Subject: [PATCH] Presenter::EscapedText: escape_js_calls MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Helferfunktion um das manuelle Zusammenstückeln von onclick Callbacks zu erleichtern. --- SL/Presenter/EscapedText.pm | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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 -- 2.20.1