Quiq/Html/Page.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 65.0% 13 / 20 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 86.7% 92 / 106 CC 16 SCAR 19.3
CC
16
Cov
87%
html
12.1
new
7.0
BEGIN
1.0
CRAP 6.9
« Quiq/Html/Construct.pm ↑ Summary Quiq/Gnuplot/Plot.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Page - HTML-Seite
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Html::Base>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Html::Page;
16
17 $h = Quiq::Html::Producer->new;
18
19 $obj = Quiq::Html::Page->new(
20 body => 'hello world!',
21 );
22
23 $html = $obj->html($h);
24  
25 =head1 ATTRIBUTES
26  
27 =over 4
28  
29 =item body => $str (Default: '')
30  
31 Rumpf der Seite.
32  
33 =item bodyOnly => $bool (Default: 0)
34  
35 Liefere als Returnwert von asHtml() nur den Body anstelle der
36 Gesamtseite.
37  
38 =item comment => $str (Default: undef)
39  
40 Kommentar am Anfang der Seite.
41  
42 =item components => \@components
43  
44 Liste der HTML-Komponenten, die in die Seite eingesetzt werden.
45 Die Komponentenbestandteile werden automatisch zu den Seitenbestandteilen
46 hinzugefüht. Im Seiten-HTML-Code wird der HTML-Code der Komponente
47 an der Postion des Platzhalters __<NAME>__ eingesetzt.
48  
49 =item encoding => $charset (Default: 'utf-8')
50  
51 Encoding der Seite, z.B. 'iso-8859-1'.
52  
53 =item head => $str (Default: '')
54  
55 Kopf der Seite.
56  
57 =item load => \@arr
58  
59 Liste von Ladeanweisungen für CSS- und JavaScript-Dateien. Die
60 Ladeanweisungen werden vor anderem CSS- und JavaScript-Code
61 (s. Attribute javaScript und styleSheet) in den Head der Seite
62 geschrieben. Eine CSS-Datei wird durch die Angabe eines Paars
63 css => $url, eine JavaScript-Datei durch die Angabe eines Paars
64 js => $url geladen. Hat $url die Endung .css oder .js, kann die
65 Typangabe auch weggelassen werden. Beispiel:
66  
67 load => [
68 css => 'https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css',
69 js => 'https://code.jquery.com/ui/1.12.1/jquery-ui.min.js',
70 ],
71  
72 Oder kurz (da die Dateiendungen den Typ verraten):
73  
74 load => [
75 'https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css',
76 'https://code.jquery.com/ui/1.12.1/jquery-ui.min.js',
77 ],
78  
79 =item noNewline => $bool (Default: 0)
80  
81 Füge kein Newline am Ende der Seite hinzu.
82  
83 =item placeholders => \@keyVal (Default: [])
84  
85 Ersetze im generierten HTML-Code die angegebenen Platzhalter durch
86 ihre Werte.
87  
88 =item javaScript => $url|$jsCode|[...] (Default: undef)
89  
90 URL oder JavaScript-Code am Ende der Seite. Mehrfach-Definition,
91 wenn Array-Referenz. Das Attribut kann mehrfach auftreten, die
92 Werte werden zu einer Liste zusammengefügt.
93  
94 =item javaScriptToHead => $bool (Default: 0)
95  
96 Setze den JavaScrip-Code nicht an das Ende des Body, sondern in
97 den Head der HTML-Seite.
98  
99 =item ready => $jsCode (Default: [])
100  
101 Führe JavaScript-Code $jsCode aus, wenn das DOM geladen ist.
102 Dies ist so implementiert, dass der Code in einen jQuery
103 ready-Handler eingebettet wird:
104  
105 $(function() {
106 $jsCode
107 });
108  
109 Das Konstrukt setzt also jQuery voraus. Das Attribut kann mehrfach
110 auftreten, es werden dann mehrere ready-Handler aufgesetzt.
111  
112 =item styleSheet => $spec | \@specs (Default: undef)
113  
114 Einzelne Style-Spezifikation oder Liste von Style-Spezifikationen.
115 Siehe Methode Quiq::Css->style(). Das Attribut kann mehrfach
116 auftreten, die Werte werden zu einer Liste zusammengefügt.
117  
118 =item title => $str (Default: undef)
119  
120 Titel der Seite.
121  
122 =item topIndentation => $n (Default: 2)
123  
124 Einrückung des Inhalts der obersten Elemente <head> und <body>.
125  
126 =back
127  
128 =cut
129  
130 # -----------------------------------------------------------------------------
131  
132 package Quiq::Html::Page;
133 6 ▶use base qw/Quiq::Html::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 6 our $VERSION = '1.240';
140  
141 6 ▶use Quiq::Html::Component::Bundle;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 6 ▶use Quiq::Css;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 6 ▶use Quiq::JQuery::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 6 ▶use Quiq::JavaScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 6 ▶use Quiq::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 # -----------------------------------------------------------------------------
148  
149 =head1 METHODS
150  
151 =head2 Konstruktor
152  
153 =head3 new() - Konstruktor
154  
155 =head4 Synopsis
156  
157 $obj = $class->new(@keyVal);
158  
159 =cut
160  
161 # -----------------------------------------------------------------------------
162  
163 4 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
164 4 my $class = shift;
165 # @_: @keyVal
166  
167 4 my $self = $class->SUPER::new(
168 body => '',
169 bodyOnly => 0,
170 comment => undef,
171 components => [],
172 encoding => 'utf-8',
173 head => '',
174 load => [],
175 noNewline => 0,
176 placeholders => [],
177 javaScript => [],
178 javaScriptToHead => 0,
179 ready => [],
180 styleSheet => [],
181 title => '',
182 topIndentation => 2,
183 );
184  
185 4 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 4 10
186 6 my $key = shift;
187 6 my $val = shift;
188  
189 6 ▶ if ($key eq 'javaScript' || $key eq 'load' || $key eq 'ready' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'javaScript' or $key eq 'load' or $key eq 'ready' or $key eq 'styleSheet') { } 4 2 6
Condition77%7/9
$key eq "javaScript" or $key eq "load" or $key eq "ready" or $key eq "styleSheet"
l!l&&r!l&&!r
402
$key eq "javaScript" or $key eq "load" or $key eq "ready"
l!l&&r!l&&!r
312
$key eq "javaScript" or $key eq "load"
l!l&&r!l&&!r
033
MC/DC50%2/4
$key eq "javaScript" or $key eq "load" or $key eq "ready" or $key eq "styleSheet"
$key eq "javaScript" $key eq "load" $key eq "ready" $key eq "styleSheet"
Truth table60%3/5
A or B or C or D
A= $key eq "javaScript"B= $key eq "load"C= $key eq "ready"D= $key eq "styleSheet"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
190 $key eq 'styleSheet') {
191 4 my $arr = $self->get($key);
192 4 ▶ push @$arr,ref $val? @$val: $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 3 1 4
193 }
194 else {
195 2 $self->set($key=>$val);
196 }
197 }
198  
199 4 return $self;
200 }
201  
202 # -----------------------------------------------------------------------------
203  
204 =head2 Objektmethoden
205  
206 =head3 html() - Generiere HTML
207  
208 =head4 Synopsis
209  
210 $html = $obj->html($h);
211 $html = $class->html($h,@keyVal);
212  
213 =cut
214  
215 # -----------------------------------------------------------------------------
216  
217 4 ▶sub html {
Subroutine html: called CC=10 SCAR=25.0
Pod documented
218 4 my $this = shift;
219 4 my $h = shift;
220  
221 4 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 4 4
222  
223 4 my ($body,$bodyOnly,$comment,$componentA,,$encoding,$head,$loadA,
224 $noNewline,$placeholderA,$title,$jsA,$javaScriptToHead,$readyA,
225 $cssA,$topIndentation) =
226 $self->get(qw/body bodyOnly comment components encoding head load
227 noNewline placeholders title javaScript javaScriptToHead ready
228 styleSheet topIndentation/);
229  
230 4 my $b = Quiq::Html::Component::Bundle->new($componentA);
231  
232 # Resourcen (CSS- und JavaScript-Dateien) laden
233 4 my $loadTags = $h->loadFiles(@$loadA,$b->resources);
234  
235 # Stylesheet-Defininition(en)
236 4 my $styleTags = Quiq::Css->style($h,@$cssA,$b->css);
237  
238 # ready-Handler
239  
240 4 for ($b->ready,@$readyA) {
241 1 push @$jsA,Quiq::JQuery::Function->ready($_);
242 }
243  
244 # Script-Definition(en)
245  
246 4 my $scriptTags;
247 4 for my $js ($b->js,@$jsA) {
248 1 $scriptTags .= Quiq::JavaScript->script($h,$js);
249 }
250  
251 # Wenn $body keinen body-Tag enthält, fügen wir ihn hinzu.
252  
253 4 ▶ if ($body !~ /^<body/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body =~ /^<body/i) 4 0 4
254 4 ▶ $body = $h->tag('body',
Branch: 1 branch
BranchTrueFalseTotal
$javaScriptToHead ? : 0 4 4
255 -ind => $topIndentation,
256 '-',
257 $body,
258 $javaScriptToHead? (): $scriptTags,
259 );
260 }
261  
262 4 ▶ my $html = $bodyOnly? $body: $h->cat(
Branch: 2 branches
BranchTrueFalseTotal
$bodyOnly ? : 0 4 4
$javaScriptToHead ? : 0 4 4
263 $h->doctype,
264 $h->comment(-nl=>2,$comment),
265 $h->tag('html',
266 '-',
267 $h->tag('head',
268 -ind => $topIndentation,
269 '-',
270 $h->tag('title',
271 -ignoreIf => !$title,
272 '-',
273 $title,
274 ),
275 $h->tag('meta',
276 'http-equiv' => 'content-type',
277 content => "text/html; charset=$encoding",
278 ),
279 $h->cat($head),
280 $loadTags,
281 $styleTags,
282 $javaScriptToHead? $scriptTags: (),
283 ),
284 $body,
285 ),
286 );
287  
288 # Platzhalter ersetzen
289  
290 4 my @placeholders = (@$placeholderA,$b->htmlPlaceholders);
291 4 ▶ if (@placeholders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@placeholders) 0 4 4
292 0 my $tpl = Quiq::Template->new('text',\$html);
293 0 $html = $tpl->replace(@placeholders)->asString;
294 }
295  
296 4 ▶ if ($noNewline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noNewline) 0 4 4
297 0 chomp $html;
298 }
299  
300 4 return $html;
301 }
302  
303 # -----------------------------------------------------------------------------
304  
305 =head1 VERSION
306  
307 1.240
308  
309 =head1 AUTHOR
310  
311 Frank Seitz, L<http://fseitz.de/>
312  
313 =head1 COPYRIGHT
314  
315 Copyright (C) 2026 Frank Seitz
316  
317 =head1 LICENSE
318  
319 This code is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself.
321  
322 =cut
323  
324 # -----------------------------------------------------------------------------
325  
326 6 1;
327  
328 # eof
« Quiq/Html/Construct.pm ↑ Summary Quiq/Gnuplot/Plot.pm »