4
4
5
5
namespace MicroHTML ;
6
6
7
+ /**
8
+ * @phpstan-type Attrs array<string,string|null|bool|int|float>
9
+ * @phpstan-type Child \MicroHTML\HTMLElement|string|null|bool|int|float
10
+ * @phpstan-type Arg Attrs|Child
11
+ */
7
12
class HTMLElement
8
13
{
9
14
protected string $ tag ;
15
+ /** @var Attrs $attrs */
10
16
protected array $ attrs ;
17
+ /** @var array<Child> $children */
11
18
protected array $ children ;
12
19
20
+ /** @param array<Arg> $args */
13
21
public function __construct (string $ tag , array $ args )
14
22
{
15
23
$ this ->tag = $ tag ;
16
24
17
25
if (count ($ args ) > 0 && is_array ($ args [0 ])) {
18
26
$ this ->attrs = $ args [0 ];
27
+ // @phpstan-ignore-next-line
19
28
$ this ->children = array_slice ($ args , 1 );
20
29
} else {
21
30
$ this ->attrs = [];
31
+ // @phpstan-ignore-next-line
22
32
$ this ->children = $ args ;
23
33
}
24
34
}
25
35
36
+ /** @param Child $args */
26
37
public function appendChild (...$ args ): void
27
38
{
28
39
foreach ($ args as $ arg ) {
@@ -58,10 +69,10 @@ protected function renderChildren(): string
58
69
if ($ child instanceof HTMLElement) {
59
70
$ sub .= $ child ;
60
71
} else {
61
- if (is_null ($ child )) {
72
+ if (is_null ($ child ) || is_bool ( $ child ) ) {
62
73
$ child = "" ;
63
74
}
64
- if (is_numeric ($ child ) || is_bool ( $ child ) ) {
75
+ if (is_numeric ($ child )) {
65
76
$ child = (string )$ child ;
66
77
}
67
78
$ sub .= htmlentities ($ child , ENT_QUOTES , "UTF-8 " );
@@ -79,8 +90,21 @@ public function __toString(): string
79
90
}
80
91
}
81
92
93
+ /**
94
+ * https://developer.mozilla.org/en-US/docs/Glossary/Void_element
95
+ *
96
+ * @phpstan-import-type Attrs from HTMLElement
97
+ */
82
98
class SelfClosingHTMLElement extends HTMLElement
83
99
{
100
+ /**
101
+ * @param Attrs $attrs
102
+ */
103
+ public function __construct (string $ tag , array $ attrs )
104
+ {
105
+ parent ::__construct ($ tag , [$ attrs ]);
106
+ }
107
+
84
108
public function __toString (): string
85
109
{
86
110
$ tag = $ this ->tag ;
@@ -89,8 +113,14 @@ public function __toString(): string
89
113
}
90
114
}
91
115
116
+ /**
117
+ * @phpstan-import-type Child from HTMLElement
118
+ */
92
119
class EmptyHTMLElement extends HTMLElement
93
120
{
121
+ /**
122
+ * @param array<Child> $args
123
+ */
94
124
public function __construct (array $ args )
95
125
{
96
126
parent ::__construct ("" , $ args );
@@ -103,9 +133,14 @@ public function __toString(): string
103
133
}
104
134
}
105
135
136
+ function emptyHTML (...$ args ): HTMLElement
137
+ {
138
+ return new EmptyHTMLElement ($ args );
139
+ }
140
+
106
141
class RawHTMLElement extends HTMLElement
107
142
{
108
- private $ html ;
143
+ private string $ html ;
109
144
110
145
public function __construct (string $ html )
111
146
{
@@ -123,15 +158,18 @@ function rawHTML(string $html): HTMLElement
123
158
{
124
159
return new RawHTMLElement ($ html );
125
160
}
126
- function emptyHTML (... $ args ): HTMLElement
127
- {
128
- return new EmptyHTMLElement ( $ args );
129
- }
130
- function joinHTML (HTMLElement |string $ glue , array $ pieces ): HTMLElement
161
+
162
+ /**
163
+ * @param array<\MicroHTML\HTMLElement|string|null|bool|int|float> $pieces
164
+ */
165
+ function joinHTML (HTMLElement |string $ glue , array $ pieces, bool $ filterNulls = false ): HTMLElement
131
166
{
132
167
$ out = emptyHTML ();
133
168
$ n = 0 ;
134
169
foreach ($ pieces as $ piece ) {
170
+ if ($ filterNulls && $ piece === null ) {
171
+ continue ;
172
+ }
135
173
if ($ n ++ > 0 ) {
136
174
$ out ->appendChild ($ glue );
137
175
}
@@ -147,21 +185,24 @@ function HTML(...$args): HTMLElement
147
185
}
148
186
149
187
# Document metadata
150
- function BASE (...$ args ): HTMLElement
188
+ /** @param array<string,string|null|bool|int|float> $attrs */
189
+ function BASE (array $ attrs = []): SelfClosingHTMLElement
151
190
{
152
- return new SelfClosingHTMLElement ("base " , $ args );
191
+ return new SelfClosingHTMLElement ("base " , $ attrs );
153
192
}
154
193
function HEAD (...$ args ): HTMLElement
155
194
{
156
195
return new HTMLElement ("head " , $ args );
157
196
}
158
- function LINK (...$ args ): HTMLElement
197
+ /** @param array<string,string|null|bool|int|float> $attrs */
198
+ function LINK (array $ attrs = []): SelfClosingHTMLElement
159
199
{
160
- return new SelfClosingHTMLElement ("link " , $ args );
200
+ return new SelfClosingHTMLElement ("link " , $ attrs );
161
201
}
162
- function META (...$ args ): HTMLElement
202
+ /** @param array<string,string|null|bool|int|float> $attrs */
203
+ function META (array $ attrs = []): SelfClosingHTMLElement
163
204
{
164
- return new SelfClosingHTMLElement ("meta " , $ args );
205
+ return new SelfClosingHTMLElement ("meta " , $ attrs );
165
206
}
166
207
function STYLE (...$ args ): HTMLElement
167
208
{
@@ -273,9 +314,10 @@ function FIGURE(...$args): HTMLElement
273
314
{
274
315
return new HTMLElement ("figure " , $ args );
275
316
}
276
- function HR (...$ args ): HTMLElement
317
+ /** @param array<string,string|null|bool|int|float> $attrs */
318
+ function HR (array $ attrs = []): SelfClosingHTMLElement
277
319
{
278
- return new SelfClosingHTMLElement ("hr " , $ args );
320
+ return new SelfClosingHTMLElement ("hr " , $ attrs );
279
321
}
280
322
function LI (...$ args ): HTMLElement
281
323
{
@@ -319,9 +361,10 @@ function BDO(...$args): HTMLElement
319
361
{
320
362
return new HTMLElement ("bdo " , $ args );
321
363
}
322
- function BR (...$ args ): HTMLElement
364
+ /** @param array<string,string|null|bool|int|float> $attrs */
365
+ function BR (array $ attrs = []): SelfClosingHTMLElement
323
366
{
324
- return new SelfClosingHTMLElement ("br " , $ args );
367
+ return new SelfClosingHTMLElement ("br " , $ attrs );
325
368
}
326
369
function CITE (...$ args ): HTMLElement
327
370
{
@@ -423,31 +466,35 @@ function VAR_(...$args): HTMLElement
423
466
{
424
467
return new HTMLElement ("var " , $ args );
425
468
}
426
- function WBR (...$ args ): HTMLElement
469
+ /** @param array<string,string|null|bool|int|float> $attrs */
470
+ function WBR (array $ attrs = []): SelfClosingHTMLElement
427
471
{
428
- return new HTMLElement ("wbr " , $ args );
472
+ return new SelfClosingHTMLElement ("wbr " , $ attrs );
429
473
}
430
474
431
475
# Image and multimedia
432
- function AREA (...$ args ): HTMLElement
476
+ /** @param array<string,string|null|bool|int|float> $attrs */
477
+ function AREA (array $ attrs = []): SelfClosingHTMLElement
433
478
{
434
- return new SelfClosingHTMLElement ("area " , $ args );
479
+ return new SelfClosingHTMLElement ("area " , $ attrs );
435
480
}
436
481
function AUDIO (...$ args ): HTMLElement
437
482
{
438
483
return new HTMLElement ("audio " , $ args );
439
484
}
440
- function IMG (...$ args ): HTMLElement
485
+ /** @param array<string,string|null|bool|int|float> $attrs */
486
+ function IMG (array $ attrs = []): SelfClosingHTMLElement
441
487
{
442
- return new SelfClosingHTMLElement ("img " , $ args );
488
+ return new SelfClosingHTMLElement ("img " , $ attrs );
443
489
}
444
490
function MAP (...$ args ): HTMLElement
445
491
{
446
492
return new HTMLElement ("map " , $ args );
447
493
}
448
- function TRACK (...$ args ): HTMLElement
494
+ /** @param array<string,string|null|bool|int|float> $attrs */
495
+ function TRACK (array $ attrs = []): SelfClosingHTMLElement
449
496
{
450
- return new SelfClosingHTMLElement ("track " , $ args );
497
+ return new SelfClosingHTMLElement ("track " , $ attrs );
451
498
}
452
499
function VIDEO (...$ args ): HTMLElement
453
500
{
@@ -459,9 +506,10 @@ function APPLET(...$args): HTMLElement
459
506
{
460
507
return new HTMLElement ("applet " , $ args );
461
508
}
462
- function EMBED (...$ args ): HTMLElement
509
+ /** @param array<string,string|null|bool|int|float> $attrs */
510
+ function EMBED (array $ attrs = []): SelfClosingHTMLElement
463
511
{
464
- return new HTMLElement ("embed " , $ args );
512
+ return new SelfClosingHTMLElement ("embed " , $ attrs );
465
513
}
466
514
function IFRAME (...$ args ): HTMLElement
467
515
{
@@ -475,17 +523,19 @@ function OBJECT(...$args): HTMLElement
475
523
{
476
524
return new HTMLElement ("object " , $ args );
477
525
}
478
- function PARAM (...$ args ): HTMLElement
526
+ /** @param array<string,string|null|bool|int|float> $attrs */
527
+ function PARAM (array $ attrs = []): SelfClosingHTMLElement
479
528
{
480
- return new HTMLElement ("param " , $ args );
529
+ return new SelfClosingHTMLElement ("param " , $ attrs );
481
530
}
482
531
function PICTURE (...$ args ): HTMLElement
483
532
{
484
533
return new HTMLElement ("picture " , $ args );
485
534
}
486
- function SOURCE (...$ args ): HTMLElement
535
+ /** @param array<string,string|null|bool|int|float> $attrs */
536
+ function SOURCE (array $ attrs = []): SelfClosingHTMLElement
487
537
{
488
- return new HTMLElement ("source " , $ args );
538
+ return new SelfClosingHTMLElement ("source " , $ attrs );
489
539
}
490
540
491
541
# Scripting
@@ -517,9 +567,10 @@ function CAPTION(...$args): HTMLElement
517
567
{
518
568
return new HTMLElement ("caption " , $ args );
519
569
}
520
- function COL (...$ args ): HTMLElement
570
+ /** @param array<string,string|null|bool|int|float> $attrs */
571
+ function COL (array $ attrs = []): SelfClosingHTMLElement
521
572
{
522
- return new SelfClosingHTMLElement ("col " , $ args );
573
+ return new SelfClosingHTMLElement ("col " , $ attrs );
523
574
}
524
575
function COLGROUP (...$ args ): HTMLElement
525
576
{
@@ -571,9 +622,10 @@ function FORM(...$args): HTMLElement
571
622
{
572
623
return new HTMLElement ("form " , $ args );
573
624
}
574
- function INPUT (...$ args ): HTMLElement
625
+ /** @param array<string,string|null|bool|int|float> $attrs */
626
+ function INPUT (array $ attrs = []): SelfClosingHTMLElement
575
627
{
576
- return new SelfClosingHTMLElement ("input " , $ args );
628
+ return new SelfClosingHTMLElement ("input " , $ attrs );
577
629
}
578
630
function LABEL (...$ args ): HTMLElement
579
631
{
0 commit comments