Skip to content

Commit afa6fdf

Browse files
committed
Html: removed $xhtml (BC break)
1 parent ed98c12 commit afa6fdf

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

src/Utils/Html.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,6 @@ class Html implements \ArrayAccess, \Countable, \IteratorAggregate, HtmlStringab
238238
/** @var array<string, mixed> element's attributes */
239239
public $attrs = [];
240240

241-
public static bool $xhtml = false;
242-
243241
/** void elements */
244242
public static $emptyElements = [
245243
'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
@@ -740,7 +738,7 @@ final public function __toString(): string
740738
final public function startTag(): string
741739
{
742740
return $this->name
743-
? '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>')
741+
? '<' . $this->name . $this->attributes() . '>'
744742
: '';
745743
}
746744

@@ -771,11 +769,7 @@ final public function attributes(): string
771769
continue;
772770

773771
} elseif ($value === true) {
774-
if (static::$xhtml) {
775-
$s .= ' ' . $key . '="' . $key . '"';
776-
} else {
777-
$s .= ' ' . $key;
778-
}
772+
$s .= ' ' . $key;
779773
continue;
780774

781775
} elseif (is_array($value)) {
@@ -810,7 +804,7 @@ final public function attributes(): string
810804
$s .= ' ' . $key . '=' . $q
811805
. str_replace(
812806
['&', $q, '<'],
813-
['&amp;', $q === '"' ? '&quot;' : '&#39;', self::$xhtml ? '&lt;' : '<'],
807+
['&amp;', $q === '"' ? '&quot;' : '&#39;', '<'],
814808
$value,
815809
)
816810
. (str_contains($value, '`') && strpbrk($value, ' <>"\'') === false ? ' ' : '')

tests/Utils/Html.basic.phpt

+19-31
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,55 @@ require __DIR__ . '/../bootstrap.php';
1414

1515

1616
test('', function () {
17-
Html::$xhtml = true;
1817
$el = Html::el('img')->src('image.gif')->alt('');
19-
Assert::same('<img src="image.gif" alt="" />', (string) $el);
20-
Assert::same('<img src="image.gif" alt="" />', $el->toHtml());
21-
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
18+
Assert::same('<img src="image.gif" alt="">', (string) $el);
19+
Assert::same('<img src="image.gif" alt="">', $el->toHtml());
20+
Assert::same('<img src="image.gif" alt="">', $el->startTag());
2221
Assert::same('', $el->endTag());
2322
});
2423

2524

2625
test('', function () {
27-
Html::$xhtml = true;
2826
$el = Html::el('img')->setAttribute('src', 'image.gif')->setAttribute('alt', '');
29-
Assert::same('<img src="image.gif" alt="" />', (string) $el);
30-
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
27+
Assert::same('<img src="image.gif" alt="">', (string) $el);
28+
Assert::same('<img src="image.gif" alt="">', $el->startTag());
3129
Assert::same('', $el->endTag());
3230
});
3331

3432

3533
test('', function () {
36-
Html::$xhtml = true;
3734
$el = Html::el('img')->accesskey(0, true)->alt('alt', false);
38-
Assert::same('<img accesskey="0" />', (string) $el);
39-
Assert::same('<img accesskey="0 1" />', (string) $el->accesskey(1, true));
40-
Assert::same('<img accesskey="0" />', (string) $el->accesskey(1, false));
41-
Assert::same('<img accesskey="0" />', (string) $el->accesskey(0, true));
42-
Assert::same('<img accesskey="0" />', (string) $el->accesskey(0));
35+
Assert::same('<img accesskey="0">', (string) $el);
36+
Assert::same('<img accesskey="0 1">', (string) $el->accesskey(1, true));
37+
Assert::same('<img accesskey="0">', (string) $el->accesskey(1, false));
38+
Assert::same('<img accesskey="0">', (string) $el->accesskey(0, true));
39+
Assert::same('<img accesskey="0">', (string) $el->accesskey(0));
4340

4441
unset($el->accesskey);
45-
Assert::same('<img />', (string) $el);
42+
Assert::same('<img>', (string) $el);
4643
});
4744

4845

4946
test('', function () {
50-
Html::$xhtml = true;
5147
$el = Html::el('img')->appendAttribute('accesskey', 0)->setAttribute('alt', false);
52-
Assert::same('<img accesskey="0" />', (string) $el);
53-
Assert::same('<img accesskey="0 1" />', (string) $el->appendAttribute('accesskey', 1));
54-
Assert::same('<img accesskey="0" />', (string) $el->appendAttribute('accesskey', 1, false));
55-
Assert::same('<img accesskey="0" />', (string) $el->appendAttribute('accesskey', 0));
56-
Assert::same('<img accesskey="0" />', (string) $el->setAttribute('accesskey', 0));
57-
Assert::same('<img />', (string) $el->removeAttribute('accesskey'));
48+
Assert::same('<img accesskey="0">', (string) $el);
49+
Assert::same('<img accesskey="0 1">', (string) $el->appendAttribute('accesskey', 1));
50+
Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 1, false));
51+
Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 0));
52+
Assert::same('<img accesskey="0">', (string) $el->setAttribute('accesskey', 0));
53+
Assert::same('<img>', (string) $el->removeAttribute('accesskey'));
5854
});
5955

6056

6157
test('', function () {
6258
$el = Html::el('img')->src('image.gif')->alt('')->setText('any content');
63-
Assert::same('<img src="image.gif" alt="" />', (string) $el);
64-
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
65-
Assert::same('', $el->endTag());
66-
67-
Html::$xhtml = false;
6859
Assert::same('<img src="image.gif" alt="">', (string) $el);
60+
Assert::same('<img src="image.gif" alt="">', $el->startTag());
61+
Assert::same('', $el->endTag());
6962
});
7063

7164

7265
test('', function () {
73-
Html::$xhtml = false;
7466
$el = Html::el('img')->setSrc('image.gif')->setAlt('alt1')->setAlt('alt2');
7567
Assert::same('<img src="image.gif" alt="alt2">', (string) $el);
7668
Assert::same('image.gif', $el->getSrc());
@@ -104,10 +96,6 @@ test('small & big numbers', function () {
10496

10597

10698
test('attributes escaping', function () {
107-
Html::$xhtml = true;
108-
Assert::same('<a one=\'"\' two="\'" three="&lt;>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));
109-
110-
Html::$xhtml = false;
11199
Assert::same('<a one=\'"\' two="\'" three="<>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));
112100
Assert::same('<a one="``xx "></a>', (string) Html::el('a')->one('``xx')); // mXSS
113101
});

0 commit comments

Comments
 (0)