Quiq/Css.pm

filter: Statementstmt 88.7% 95 / 107 Branchbran 86.9% 40 / 46 Conditioncond 62.5% 10 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 93.3% 14 / 15 Podpod 100% 9 / 9 totaltotal 84.6% 171 / 202 CC 30 SCAR 17.1
CC
30
Cov
86%
style
8.5
properties
8.1
rulesFromObject
7.0
CRAP 5.5
« Quiq/Http/Cookie.pm ↑ Summary Quiq/JavaScript.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Css - Generiere CSS Code
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 ATTRIBUTES
14  
15 =over 4
16  
17 =item format => 'normal', 'flat' (Default: 'normal')
18  
19 Format des generierten CSS-Code.
20  
21 =back
22  
23 =cut
24  
25 # -----------------------------------------------------------------------------
26  
27 package Quiq::Css;
28 39 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 39 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 39 our $VERSION = '1.240';
35  
36 39 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 39 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # -----------------------------------------------------------------------------
40  
41 =head1 METHODS
42  
43 =head2 Konstruktor
44  
45 =head3 new() - Instantiiere CSS-Generator
46  
47 =head4 Synopsis
48  
49 $css = $class->new;
50 $css = $class->new($format);
51  
52 =head4 Arguments
53  
54 =over 4
55  
56 =item $format (Default: 'normal')
57  
58 Format des generierten CSS-Code. Zulässige Werte:
59  
60 =over 4
61  
62 =item 'normal'
63  
64 =back
65  
66 Der CSS-Code wird mehrzeilig generiert:
67  
68 .comment {
69 color: #408080;
70 font-style: italic;
71 }
72  
73 =over 4
74  
75 =item 'flat'
76  
77 =back
78  
79 Der CSS-Code wird einzeilig generiert:
80  
81 .comment { color: #408080; font-style: italic; }
82  
83 =back
84  
85 =head4 Returns
86  
87 Referenz auf CSS-Generator-Objekt.
88  
89 =head4 Description
90  
91 Instantiiere ein CSS-Generator-Objekt und liefere eine Referenz
92 auf dieses Objekt zurück.
93  
94 =cut
95  
96 # -----------------------------------------------------------------------------
97  
98 11 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
99 11 my ($class,$format) = @_;
100  
101 11 ▶ my $self = $class->SUPER::new(
Condition100%2/2
$format // "normal"
l!l
92
MC/DC100%1/1
$format // "normal"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
102 format => $format // 'normal',
103 );
104  
105 11 return $self;
106 }
107  
108 # -----------------------------------------------------------------------------
109  
110 =head2 Objektmethoden
111  
112 =head3 properties() - Zeichenkette aus CSS Properties
113  
114 =head4 Synopsis
115  
116 $properties = $this->properties(@properties);
117 $properties = $this->properties(\@properties);
118  
119 =head4 Description
120  
121 Generiere aus den Property/Wert-Paaren @properties eine
122 Zeichenkette aus CSS Properties. Ist die Liste der
123 Property/Wert-Paare leer oder haben alle Schlüssel keinen Wert
124 (C<undef> oder Leerstring) liefere C<undef> (keinen Leerstring,
125 damit von der Methode tag() kein style-Attribut mit Leerstring
126 erzeugt wird!).
127  
128 Diese Methode ist nützlich, wenn der Wert eines HTML
129 style-Attributs erzeugt werden soll. Wenn als Wert des Attributs
130 C<style> eines Quiq::Html::Tag eine Array-Referenz angegeben
131 wird, wird diese Methode gerufen.
132  
133 =head4 Example
134  
135 Erzeuge Properties für HTML style-Attribut:
136  
137 $properties = Quiq::Css->properties(
138 fontStyle => 'italic',
139 marginLeft => '0.5cm',
140 marginRight => '0.5cm',
141 );
142  
143 liefert
144  
145 font-style: italic; margin-left: 0.5cm; margin-right: 0.5cm;
146  
147 =cut
148  
149 # -----------------------------------------------------------------------------
150  
151 16 ▶sub properties {
Subroutine properties: called CC=8 SCAR=20.9
Pod documented
152 16 my $this = shift;
153 16 ▶ my $propertyA = ref $_[0]? shift: \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12 4 16
154  
155 16 ▶ my $self = ref $this? $this: $this->new('flat');
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 13 3 16
156 16 ▶ my $sep = $self->{'format'} eq 'flat'? ' ': "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'format'} eq 'flat' ? : 14 2 16
157  
158 16 my $code;
159 16 ▶ for (my $i = 0; $i < @$propertyA; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$propertyA) 29 16 45
160 29 my $key = $propertyA->[$i];
161 29 my $val = $propertyA->[$i+1];
162  
163 29 $key =~ s/([a-z])([A-Z])/$1-\L$2/g;
164  
165 29 ▶ if (defined $val && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 29 0 29
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
166 29 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 15 14 29
167 15 $code .= $sep;
168 }
169 29 $code .= "$key: $val;";
170 }
171 }
172  
173 16 return $code;
174 }
175  
176 # -----------------------------------------------------------------------------
177  
178 =head3 rule() - Generiere CSS Style Rule
179  
180 =head4 Synopsis
181  
182 $rule = $this->rule($selector,\@properties);
183 $rule = $this->rule($selector,@properties);
184  
185 =head4 Description
186  
187 Generiere eine CSS Style Rule, bestehend aus Selector $selector
188 und den Property/Value-Paaren @properties und liefere diese als
189 Zeichenkette zurück. Ist die Liste der Properties leer oder haben
190 alle Schlüssel keinen Wert (C<undef> oder Leerstring) liefere
191 einen Leerstring ('').
192  
193 =head4 Example
194  
195 Erzeuge eine einfache Style Rule:
196  
197 $rule = Quiq::Css->rule('p.abstract',
198 fontStyle => 'italic',
199 marginLeft => '0.5cm',
200 marginRight => '0.5cm',
201 );
202  
203 liefert
204  
205 p.abstract {
206 font-style: italic;
207 margin-left: 0.5cm;
208 margin-right: 0.5cm;
209 }
210  
211 =cut
212  
213 # -----------------------------------------------------------------------------
214  
215 11 ▶sub rule {
Subroutine rule: called CC=6 SCAR=17.9
Pod documented
216 11 my $this = shift;
217 11 my $selector = shift;
218 11 ▶ my $propertyA = ref $_[0]? shift: \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 7 11
219  
220 11 ▶ my $self = ref $this? $this: $this->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 10 1 11
221
222 11 ▶ my $rule = $self->properties($propertyA) // '';
Condition100%2/2
$self->properties($propertyA) // ""
l!l
101
MC/DC100%1/1
$self->properties($propertyA) // ""
$self->properties($propertyA)
Truth table100%2/2
A
A= $self->properties($propertyA)
Aresult
11
00
223 11 ▶ if ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule) 10 1 11
224 10 ▶ if ($self->{'format'} eq 'flat') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'format'} eq 'flat') { } 9 1 10
225 9 $rule = "$selector { $rule }\n";
226 }
227 else {
228 1 $rule =~ s/^/ /mg;
229 1 $rule = "$selector {\n$rule\n}\n";
230 }
231 }
232  
233 11 return $rule;
234 }
235  
236 # -----------------------------------------------------------------------------
237  
238 =head3 rules() - Generiere mehrere CSS Style Rules
239  
240 =head4 Synopsis
241  
242 $rules = $css->rules($selector=>\@properties,...);
243  
244 =head4 Arguments
245  
246 =over 4
247  
248 =item $selector
249  
250 CSS-Selector. Z.B. 'p.abstract'.
251  
252 =item \@properties
253  
254 Liste von Property/Wert-Paaren. Z.B. [color=>'red',fontStyle=>'italic'].
255  
256 =back
257  
258 =head4 Returns
259  
260 CSS-Regeln (String)
261  
262 =head4 Description
263  
264 Wie $css->rule(), nur für mehrere CSS-Regeln auf einmal.
265  
266 =cut
267  
268 # -----------------------------------------------------------------------------
269  
270 1 ▶sub rules {
Subroutine rules: called CC=2 SCAR=6.9
Pod documented
271 1 my $self = shift;
272 # @_: $selector=>\@properties,...
273  
274 1 my $rules = '';
275 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
276 2 $rules .= $self->rule(shift,shift);
277 }
278  
279 1 return $rules;
280 }
281  
282 # -----------------------------------------------------------------------------
283  
284 =head3 restrictedRules() - Generiere lokale CSS Style Rules
285  
286 =head4 Synopsis
287  
288 $rules = $css->restrictedRules($localSelector,
289 $selector => \@properties,
290 ...
291 );
292  
293 =head4 Arguments
294  
295 =over 4
296  
297 =item $localSelector
298  
299 Selector, der allen folgenden Selektoren vorangestellt wird.
300 Z.B. '#table01'.
301  
302 =item $selector
303  
304 Sub-Selector, der dem $localSelector mit einem Leerzeichen
305 getrennt, nachgestellt wird. Wenn Leerstring (''), wird der
306 Sub-Selector fortgelassen, die @properties also direkt dem
307 $localSelector zugeordnet. Beginnt $selector mit einem
308 Kaufmanns-Und (&), werden $localSelector und $selector ohne
309 trennendes Leerzeichen konkateniert (gleiche Logik wie bei Sass).
310  
311 =item \@properties
312  
313 Liste von Property/Wert-Paaren. Z.B. [color=>'red',fontStyle=>'italic'].
314  
315 =back
316  
317 =head4 Returns
318  
319 CSS-Regeln (String)
320  
321 =head4 Description
322  
323 Wie $css->rules(), nur mit zusätzlicher Einschränkung auf einen
324 lokalen Selektor.
325  
326 =cut
327  
328 # -----------------------------------------------------------------------------
329  
330 1 ▶sub restrictedRules {
Subroutine restrictedRules: called CC=4 SCAR=14.7
Pod documented
331 1 my $self = shift;
332 1 my $localSelector = shift;
333 # @_: $selector=>\@properties,...
334  
335 1 my $rules = '';
336 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
337 2 my $selector = shift;
338 2 ▶ if (substr($selector,0,1) eq '&') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($selector, 0, 1) eq '&') { } 0 2 2
elsif ($selector eq '') { } 0 2 2
339 0 substr($selector,0,1) = '';
340 0 $selector = $localSelector.$selector;
341 }
342 elsif ($selector eq '') {
343 0 $selector = $localSelector;
344 }
345 else {
346 2 $selector = "$localSelector $selector";
347 }
348 2 $rules .= $self->rule($selector,shift);
349 }
350  
351 1 return $rules;
352 }
353  
354 # -----------------------------------------------------------------------------
355  
356 =head3 rulesFromObject() - Generiere CSS Style Rules aus Objekt-Attributen
357  
358 =head4 Synopsis
359  
360 $rules = $css->rulesFromObject($obj,
361 $key => [$selector,@properties],
362 ...
363 );
364  
365 =head4 Arguments
366  
367 =over 4
368  
369 =item $obj
370  
371 Das Objekt, aus dessen Objektattributen $key, ... die
372 CSS-Regeln generiert werden.
373  
374 =item $key => [$selector,@properties], ...
375  
376 Liste der Objekt-Attribute $key, ..., ihre entsprechenden
377 Selektoren und Default-Properties.
378  
379 =back
380  
381 =head4 Returns
382  
383 CSS-Regeln (String)
384  
385 =head4 Description
386  
387 Die Methode erzeugt CSS-Regeln auf Basis von Objekt-Attributen.
388 Jedes Attribut entspricht einem CSS-Selektor und definiert dessen
389 Properties. Es können Default-Properties hinterlegt werden, die
390 der Aufrufer ergänzen ('+' als erstes Element der Property-Liste)
391 oder ersetzen oder löschen kann (siehe Example).
392  
393 =head4 Example
394  
395 B<< Beispiel aus Quiq::Html::Verbatim >>
396  
397 Im Konstruktor werden die Objekt-Attribute vereinbart. Diese
398 können bei der Instantiierung des Objektes gesetzt werden.
399  
400 my $self = $class->SUPER::new(
401 cssTableProperties => undef,
402 cssLnProperties => undef,
403 cssMarginProperties => undef,
404 cssTextProperties => undef,
405 ...
406 );
407  
408 In der Methode, die die CSS-Regeln erzeugt, werden die zugehörigen
409 Selektoren und Default-Properties vereinbart.
410  
411 $rules .= $css->rulesFromObject($self,
412 cssTableProperties => [".xxx-table"],
413 cssLnProperties => [".xxx-ln",color=>'#808080'],
414 cssMarginProperties => [".xxx-margin",width=>'0.6em'],
415 cssTextProperties => [".xxx-text"],
416 );
417  
418 Beim Konstruktor-Aufruf können die Default-Properties ergänzt ('+'
419 als erstes Element in der Property-Liste) oder ersetzt (keine
420 Angabe) oder gelöscht werden (leere Liste).
421  
422 my $obj = Quiq::Hash->new(
423 cssTableProperties => [backgroundColor=>'#f0f0f0'], # ersetzen
424 cssLnProperties => ['>',color=>'black'], # ersetzen
425 cssMarginProperties => ['+',backgroundColor=>'red'], # ergänzen
426 cssTextProperties => [], # löschen
427 );
428  
429 Resultierende CSS-Regeln:
430  
431 .xxx-table { background-color: #f0f0f0; }
432 .xxx-ln { color: black; }
433 .xxx-margin { width: 0.6em; background-color: red; }
434  
435 =cut
436  
437 # -----------------------------------------------------------------------------
438  
439 1 ▶sub rulesFromObject {
Subroutine rulesFromObject: called CC=7 SCAR=19.5
Pod documented
440 1 my $self = shift;
441 1 my $obj = shift;
442 # @_: $key => [$selector,@properties], ...
443  
444 1 my $rules = '';
445 1 ▶ for (my $i = 0; $i < @_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 4 1 5
446 4 my $key = $_[$i];
447 4 my ($selector,@defaults) = @{$_[$i+1]};
448 4 my $propA = $obj->get($key);
449  
450 4 my (@prop,$op);
451 4 ▶ if ($propA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($propA) 4 0 4
452 4 @prop = @$propA;
453 4 ▶ if (@prop % 2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prop % 2 == 1) 1 3 4
454 1 $op = shift @prop;
455 }
456 }
457 4 ▶ if (!$propA || defined($op) && $op eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $propA or defined $op and $op eq "+") 1 3 4
Condition66%4/6
not $propA or defined $op and $op eq "+"
l!l&&r!l&&!r
013
defined $op and $op eq "+"
!ll&&!rl&&r
301
MC/DC33%1/3
not $propA or defined $op and $op eq "+"
not $propA defined $op $op eq "+"
Truth table50%2/4
not(A) or B and C
A= not $propAB= defined $opC= $op eq "+"
ABCresult
1XX1
0111
00X0
0100
458 1 unshift @prop,@defaults;
459 }
460 4 $rules .= $self->rule($selector,@prop);
461 }
462  
463 1 return $rules;
464 }
465  
466 # -----------------------------------------------------------------------------
467  
468 =head2 Klassenmethoden
469  
470 =head3 makeFlat() - Mache CSS-Regeln einzeilig
471  
472 =head4 Synopsis
473  
474 $rules = $this->makeFlat($rules);
475  
476 =head4 Arguments
477  
478 =over 4
479  
480 =item $rules
481  
482 CSS-Regeln (String)
483  
484 =back
485  
486 =cut
487  
488 # -----------------------------------------------------------------------------
489  
490 1 ▶sub makeFlat {
Subroutine makeFlat: called CC=1 SCAR=0.0
Pod documented
491 1 my ($this,$rules) = @_;
492  
493 1 $rules =~ s/\s+/ /g;
494 1 $rules =~ s/\} /}\n/g;
495  
496 1 return $rules;
497 }
498  
499 # -----------------------------------------------------------------------------
500  
501 =head3 oneLine() - Mache CSS-Code insgesamt einzeilig
502  
503 =head4 Synopsis
504  
505 $cssCode = $this->oneLine($cssCode);
506  
507 =head4 Arguments
508  
509 =over 4
510  
511 =item $cssCode
512  
513 (String) CSS-Code als Text (Content des style-Tag).
514  
515 =back
516  
517 =cut
518  
519 # -----------------------------------------------------------------------------
520  
521 0 ▶sub oneLine {
Subroutine oneLine: not called CC=1 SCAR=6.9
Pod documented
522 0 my ($this,$css) = @_;
523  
524 0 $css =~ s/\s+/ /g;
525  
526 0 return $css;
527 }
528  
529 # -----------------------------------------------------------------------------
530  
531 =head3 style() - Generiere StyleSheet-Tags
532  
533 =head4 Synopsis
534  
535 $styleTags = Quiq::Css->style($h,@specs);
536  
537 =head4 Arguments
538  
539 =over 4
540  
541 =item @specs
542  
543 Liste von Style-Spezifikationen.
544  
545 =back
546  
547 =head4 Description
548  
549 Übersetze die Style-Spezifikationen @specs in eine Folge von
550 <link>- und/oder <style>-Tags.
551  
552 Mögliche Style-Spezifikationen:
553  
554 =over 4
555  
556 =item "inline:$file":
557  
558 Datei $file wird geladen und ihr Inhalt wird hinzugefügt.
559  
560 =item $string (Zeichenkette mit enthaltenen '{')
561  
562 Zeichenkette $string wird hinzugefügt.
563  
564 =item $url (Zeichenkette ohne '{'):
565  
566 Zeichenkette wird als URL interpretiert und ein <link>-Tag
567  
568 <link rel="stylesheet" type="text/css" href="$url" />
569  
570 hinzugefügt.
571  
572 =item \@specs (Arrayreferenz):
573  
574 Wird zu @specs expandiert.
575  
576 =back
577  
578 =head4 Example
579  
580 B<Code zum Laden eines externen Stylesheet:>
581  
582 $style = Quiq::Css->style('/css/stylesheet.css');
583 =>
584 <link rel="stylesheet" type="text/css" href="/css/stylesheet.css" />
585  
586 B<Stylesheet aus Datei einfügen:>
587  
588 $style = Quiq::Css->style('inline:/css/stylesheet.css');
589 =>
590 <Inhalt der Datei /css/stylesheet.css>
591  
592 B<Mehrere Stylesheet-Spezifikationen:>
593  
594 $style = Quiq::Css->style(
595 '/css/stylesheet1.css'
596 '/css/stylesheet2.css'
597 );
598 =>
599 <link rel="stylesheet" type="text/css" href="/css/stylesheet1.css" />
600 <link rel="stylesheet" type="text/css" href="/css/stylesheet2.css" />
601  
602 B<Mehrere Stylesheet-Spezifikationen via Arrayreferenz:>
603  
604 $style = Quiq::Css->style(
605 ['/css/stylesheet1.css','/css/stylesheet2.css']
606 );
607  
608 Dies ist nützlich, wenn die Spezifikation von einem Parameter
609 einer umgebenden Methode kommt.
610  
611 =cut
612  
613 # -----------------------------------------------------------------------------
614  
615 13 ▶sub style {
Subroutine style: called CC=7 SCAR=21.4
Pod documented
616 13 my $class = shift;
617 13 my $h = shift;
618 # @_: @spec
619  
620 13 my $linkTags = '';
621 13 my $style = '';
622  
623 13 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 13 25
624 12 my $spec = shift;
625  
626 12 ▶ if (ref $spec) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $spec) { } 3 9 12
elsif (not defined $spec or $spec eq '') { } 0 9 9
elsif ($spec =~ s/^inline://) { } 0 9 9
elsif ($spec =~ /\{/) { } 4 5 9
627 3 unshift @_,@$spec;
628 3 next;
629 }
630 ▶ elsif (!defined $spec || $spec eq '') {
Condition33%1/3
not defined $spec or $spec eq ""
l!l&&r!l&&!r
009
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
631 0 next;
632 }
633 elsif ($spec =~ s/^inline://) {
634 0 my $data = Quiq::Path->read($spec);
635 # FIXME: Optional Kommentare entfernen
636  
637 # Leerzeilen entfernen
638 0 $data =~ s|\n\s*\n+|\n|g;
639  
640 # /* eof */ und Leerzeichen am Ende entfernen
641  
642 0 $data =~ s|\s+$||;
643 0 $data =~ s|\s*/\* eof \*/$||;
644  
645 0 $style .= "$data\n";
646 }
647 elsif ($spec =~ /\{/) {
648 # Stylesheet-Definitionen, wenn { enthalten
649 4 Quiq::String->removeIndentation(\$spec);
650 4 $style .= "$spec\n";
651 }
652 else {
653 5 $linkTags .= $h->tag('link',
654 rel => 'stylesheet',
655 type => 'text/css',
656 href => $spec,
657 );
658 }
659 }
660
661 13 return $h->cat(
662 $linkTags,
663 $h->tag('style',
664 -ignoreIfNull => 1,
665 $style
666 ),
667 );
668 }
669  
670 # -----------------------------------------------------------------------------
671  
672 =head1 VERSION
673  
674 1.240
675  
676 =head1 AUTHOR
677  
678 Frank Seitz, L<http://fseitz.de/>
679  
680 =head1 COPYRIGHT
681  
682 Copyright (C) 2026 Frank Seitz
683  
684 =head1 LICENSE
685  
686 This code is free software; you can redistribute it and/or modify
687 it under the same terms as Perl itself.
688  
689 =cut
690  
691 # -----------------------------------------------------------------------------
692  
693 39 1;
694  
695 # eof
« Quiq/Http/Cookie.pm ↑ Summary Quiq/JavaScript.pm »