Skip to content

Commit 71dab03

Browse files
committed
joinHTML function
1 parent 4285614 commit 71dab03

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/microhtml.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ function emptyHTML(...$args): HTMLElement
127127
{
128128
return new EmptyHTMLElement($args);
129129
}
130+
function joinHTML(HTMLElement|string $glue, array $pieces): HTMLElement
131+
{
132+
$out = emptyHTML();
133+
$n = 0;
134+
foreach ($pieces as $piece) {
135+
if ($n++ > 0) {
136+
$out->appendChild($glue);
137+
}
138+
$out->appendChild($piece);
139+
}
140+
return $out;
141+
}
130142

131143
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element
132144
function HTML(...$args): HTMLElement

tests/HTMLElementTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function MicroHTML\emptyHTML;
1212
use function MicroHTML\DIV;
1313
use function MicroHTML\rawHTML;
14+
use function MicroHTML\joinHTML;
1415

1516
class HTMLElementTest extends \PHPUnit\Framework\TestCase
1617
{
@@ -185,4 +186,16 @@ public function testRaw(): void
185186
P(rawHTML("<bacon>"))
186187
);
187188
}
189+
190+
public function testJoin(): void
191+
{
192+
$this->assertEquals(
193+
"<p>A</p>, <p>B</p>, C",
194+
joinHTML(", ", [P("A"), P("B"), "C"])
195+
);
196+
$this->assertEquals(
197+
"<p>A</p><br /><p>B</p><br />C",
198+
joinHTML(BR(), [P("A"), P("B"), "C"])
199+
);
200+
}
188201
}

0 commit comments

Comments
 (0)