Quiq/JavaScript.pm

filter: Statementstmt 88.5% 54 / 61 Branchbran 85.0% 17 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 83.9% 89 / 106 CC 13 SCAR 16.7
CC
13
Cov
85%
script
9.5
line
6.0
new
2.0
CRAP 5.3
« Quiq/Css.pm ↑ Summary Quiq/JQuery/Form/ViewEdit.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::JavaScript - Generierung von JavaScript-Code
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::JavaScript;
18 41 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 41 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 41 our $VERSION = '1.240';
25  
26 41 ▶use Quiq::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 41 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 41 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # -----------------------------------------------------------------------------
31  
32 =head1 METHODS
33  
34 =head2 Klassenmethoden
35  
36 =head2 Konstruktor
37  
38 =head3 new() - Instantiiere Objekt
39  
40 =head4 Synopsis
41  
42 $p = $class->new;
43  
44 =head4 Returns
45  
46 JavaScript-Objekt
47  
48 =head4 Description
49  
50 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
51 dieses Objekt zurück. Da die Klasse ausschließlich Klassenmethoden
52 enthält, hat das Objekt ausschließlich die Funktion, eine abkürzende
53 Aufrufschreibweise zu ermöglichen.
54  
55 =cut
56  
57 # -----------------------------------------------------------------------------
58  
59 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
60 0 my $class = shift;
61 0 return bless \(my $dummy),$class;
62 }
63  
64 # -----------------------------------------------------------------------------
65  
66 =head3 code() - Erstelle JavaScript-Code in Perl
67  
68 =head4 Synopsis
69  
70 $js = $this->code(@keyVal,$text);
71  
72 =head4 Arguments
73  
74 =over 4
75  
76 =item @keyVal
77  
78 Liste von Platzhalter/Wert-Paaren. Die Platzhalter beginnen
79 und enden mit zwei Unterstrichen.
80  
81 =item $text
82  
83 JavaScript-Code mit Platzhaltern (String)
84  
85 =back
86  
87 =head4 Returns
88  
89 JavaScript-Code (String)
90  
91 =head4 Description
92  
93 Setze die Platzhalter/Wert-Paare @keyVal in den JavaScript-Code $text
94 ein und liefere den resultierenden JavaScript-Code zurück. Als
95 Zeilenfortsetzungszeichen kann eine Tilde (~) verwendet werden
96 (Achtung: dann darf $text nicht mit q~...~ gequotet werden).
97  
98 Die Methode ist vor allem nützlich, wenn der JavaScript-Code
99 jQuery-Aufrufe mit Dollar-Zeichen ($) enthält oder die Zeilen
100 überlang sind.
101  
102 =head4 Example
103  
104 $js = $this->code(q°
105 var __NAME__ = (function() {
106 return {
107 x: __VALUE__,
108 };
109 })();°,
110 __NAME__ => 'dgr',
111 __VALUE__ => 4711,
112 );
113  
114 liefert
115  
116 var dgr = (function() {
117 return {
118 x: 4711,
119 };
120 })();
121  
122 =cut
123  
124 # -----------------------------------------------------------------------------
125  
126 1 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
127 1 my $this = shift;
128 1 my $text = shift;
129 # @_: @keyVal
130  
131 1 return Quiq::Template->combine(
132 placeholders => \@_,
133 template => $text,
134 );
135 }
136  
137 # -----------------------------------------------------------------------------
138  
139 =head3 line() - Mache JavaScript-Code einzeilig
140  
141 =head4 Synopsis
142  
143 $line = $this->line($code);
144  
145 =head4 Arguments
146  
147 =over 4
148  
149 =item $code
150  
151 Mehrzeiliger JavaScript-Code (String)
152  
153 =back
154  
155 =head4 Returns
156  
157 JavaScript-Code einzeilig (String)
158  
159 =head4 Description
160  
161 Wandele mehrzeiligen JavaScript-Code in einzeiligen JavaScript-Code
162 und liefere diesen zurück. Die Methode ist nützlich, wenn formatierter,
163 mehrzeiliger JavaScript-Code in ein HTML Tag-Attribut (JavaScript-Handler
164 wie onclick="..." oder onchange="...") eingesetzt werden soll.
165  
166 =head4 Example
167  
168 Aus
169  
170 var s = '';
171 for (var i = 0; i < 10; i++)
172 s += 'x';
173  
174 wird
175  
176 var s = ''; for (var i = 0; i < 10; i++) s += 'x';
177  
178 =head4 Details
179  
180 Die Regeln der Umwandlung:
181  
182 =over 2
183  
184 =item *
185  
186 ist $code C<undef>, wird C<undef> geliefert
187  
188 =item *
189  
190 Kommentare (\s*//.*) werden entfernt
191  
192 =item *
193  
194 Leerzeilen und Zeilen nur aus Whitespace werden entfernt
195  
196 =item *
197  
198 Whitespace (einschl. Zeilenumbruch) am Anfang und am Ende
199 jeder Zeile wird entfernt
200  
201 =item *
202  
203 alle Zeilen werden mit einem Leerzeichen als Trenner konkateniert
204  
205 =back
206  
207 Damit dies sicher funktioniert, muss jede JavaScript-Anweisung
208 mit einem Semikolon am Zeilenende beendet werden und darf nicht,
209 wie JavaScipt es auch erlaubt, weggelassen werden.
210  
211 =cut
212  
213 # -----------------------------------------------------------------------------
214  
215 12 ▶sub line {
Subroutine line: called CC=6 SCAR=17.9
Pod documented
216 12 my ($this,$code) = @_;
217  
218 12 ▶ if (!defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 11 1 12
219 11 return undef;
220 }
221  
222 1 my $line = '';
223 1 ▶ open my $fh,'<',\$code or $this->throw;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$code or $this->throw 1 0 1
224 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 6 1 7
225 6 s~(^|\s+)//.*~~; # Kommentar entfernen
226 6 s/^\s+//;
227 6 s/\s+$//;
228 6 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 3 3 6
229  
230 3 ▶ if ($line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "") 2 1 3
231 2 $line .= ' ';
232 }
233 3 $line .= $_;
234 }
235 1 close $fh;
236  
237 1 return $line;
238 }
239  
240 # -----------------------------------------------------------------------------
241  
242 =head3 script() - Generiere einen oder mehrere <script>-Tags
243  
244 =head4 Synopsis
245  
246 $html = Quiq::JavaScript->script($h,@specs);
247  
248 =head4 Arguments
249  
250 =over 4
251  
252 =item @specs
253  
254 Liste von Script-Spezifikationen.
255  
256 =back
257  
258 =head4 Description
259  
260 Übersetze die Code-Spezifikationen @specs in einen oder mehrere
261 Script-Tags.
262  
263 Mögliche Code-Spezifikationen:
264  
265 =over 4
266  
267 =item "inline:$file":
268  
269 Datei $file wird geladen und ihr Inhalt wird in einen Script-Tag
270 eingefasst.
271  
272 =item $string (Zeichenkette mit runden Klammern oder Leerzeichen)
273  
274 Zeichenkette $string wird in einen Script-Tag eingefasst.
275  
276 =item $url (Zeichenkette ohne runde Klammern oder Leerzeichen):
277  
278 Zeichenkette wird als URL interpretiert und in einen Script-Tag
279 mit src-Attribut übersetzt.
280  
281 =item \@specs (Arrayreferenz):
282  
283 Wird zu @specs expandiert.
284  
285 =back
286  
287 =head4 Examples
288  
289 Code zum Laden einer JavaScript-Datei über URL:
290  
291 $html = Quiq::JavaScript->script($h,'https://host.dom/scr.js');
292 =>
293 <script src="https://host.dom/scr.js" type="text/javascript"></script>
294  
295 Code aus Datei einfügen:
296  
297 $html = Quiq::JavaScript->script($h,'inline:js/script.css');
298 =>
299 <script type="text/javascript">
300 ...
301 </script>
302  
303 Code direkt einfügen:
304  
305 $html = Quiq::JavaScript->script($h,q|
306 ...
307 |);
308 =>
309 <script type="text/javascript">
310 ...
311 </script>
312  
313 Mehrere Code-Spezifikationen:
314  
315 $html = Quiq::JavaScript->script(
316 '...'
317 '...'
318 );
319  
320 Mehrere Code-Spezifikationen via Arrayreferenz:
321  
322 $html = Quiq::JavaScript->script(
323 ['...','...']
324 );
325  
326 Dies ist nützlich, wenn die Spezifikation von einem Parameter
327 einer umgebenden Methode kommt.
328  
329 =cut
330  
331 # -----------------------------------------------------------------------------
332  
333 11 ▶sub script {
Subroutine script: called CC=8 SCAR=22.5
Pod documented
334 11 my $this = shift;
335 11 my $h = shift;
336 # @_: @spec
337  
338 11 my $scriptTags = '';
339  
340 11 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 11 23
341 12 my $spec = shift;
342  
343 12 ▶ if (!defined $spec || $spec eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $spec or $spec eq "") 0 12 12
Condition33%1/3
not defined $spec or $spec eq ""
l!l&&r!l&&!r
0012
MC/DC0%0/2
not defined $spec or $spec eq ""
not defined $spec $spec eq ""
Truth table33%1/3
not(A) or B
A= not defined $specB= $spec eq ""
ABresult
1X1
011
000
344 0 next;
345 }
346  
347 12 my $type = Scalar::Util::reftype($spec);
348 12 ▶ if ($type && $type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type eq "ARRAY") 3 9 12
Condition66%2/3
$type and $type eq "ARRAY"
!ll&&!rl&&r
903
MC/DC50%1/2
$type and $type eq "ARRAY"
$type $type eq "ARRAY"
Truth table66%2/3
A and B
A= $typeB= $type eq "ARRAY"
ABresult
0X0
100
111
349 3 unshift @_,@$spec;
350 3 next;
351 }
352  
353 9 ▶ if ($spec =~ s/^inline://) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ s/^inline://) { } 0 9 9
elsif ($spec =~ /[\s\(]/) { } 3 6 9
354 0 my $data = Quiq::Path->read($spec);
355  
356 # "// eof" und Leerzeichen am Ende entfernen
357  
358 0 $data =~ s|\s+$||;
359 0 $data =~ s|\s*// eof$||;
360  
361 0 $scriptTags .= $h->tag('script',
362 $data
363 );
364 }
365 elsif ($spec =~ /[\s\(]/) {
366 # Javascript-Code, wenn Whitespace oder Klammer enthalten
367 3 $scriptTags .= $h->tag('script',
368 $spec
369 );
370 }
371 else {
372 # sonst URL
373 6 $scriptTags .= $h->tag('script',
374 type => 'text/javascript',
375 src => $spec,
376 );
377 }
378 }
379  
380 11 return $scriptTags;
381 }
382  
383 # -----------------------------------------------------------------------------
384  
385 =head1 VERSION
386  
387 1.240
388  
389 =head1 AUTHOR
390  
391 Frank Seitz, L<http://fseitz.de/>
392  
393 =head1 COPYRIGHT
394  
395 Copyright (C) 2026 Frank Seitz
396  
397 =head1 LICENSE
398  
399 This code is free software; you can redistribute it and/or modify
400 it under the same terms as Perl itself.
401  
402 =cut
403  
404 # -----------------------------------------------------------------------------
405  
406 41 1;
407  
408 # eof
« Quiq/Css.pm ↑ Summary Quiq/JQuery/Form/ViewEdit.pm »