Skip to content

Commit

Permalink
Html: removed $xhtml (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 11, 2021
1 parent c18603c commit 75af0ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
12 changes: 3 additions & 9 deletions src/Utils/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ class Html implements \ArrayAccess, \Countable, \IteratorAggregate, HtmlStringab
/** @var array<string, mixed> element's attributes */
public $attrs = [];

public static bool $xhtml = false;

/** void elements */
public static $emptyElements = [
'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
Expand Down Expand Up @@ -740,7 +738,7 @@ final public function __toString(): string
final public function startTag(): string
{
return $this->name
? '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>')
? '<' . $this->name . $this->attributes() . '>'
: '';
}

Expand Down Expand Up @@ -771,11 +769,7 @@ final public function attributes(): string
continue;

} elseif ($value === true) {
if (static::$xhtml) {
$s .= ' ' . $key . '="' . $key . '"';
} else {
$s .= ' ' . $key;
}
$s .= ' ' . $key;
continue;

} elseif (is_array($value)) {
Expand Down Expand Up @@ -810,7 +804,7 @@ final public function attributes(): string
$s .= ' ' . $key . '=' . $q
. str_replace(
['&', $q, '<'],
['&amp;', $q === '"' ? '&quot;' : '&#39;', self::$xhtml ? '&lt;' : '<'],
['&amp;', $q === '"' ? '&quot;' : '&#39;', '<'],
$value,
)
. (str_contains($value, '`') && strpbrk($value, ' <>"\'') === false ? ' ' : '')
Expand Down
50 changes: 19 additions & 31 deletions tests/Utils/Html.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,55 @@ require __DIR__ . '/../bootstrap.php';


test('', function () {
Html::$xhtml = true;
$el = Html::el('img')->src('image.gif')->alt('');
Assert::same('<img src="image.gif" alt="" />', (string) $el);
Assert::same('<img src="image.gif" alt="" />', $el->toHtml());
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
Assert::same('<img src="image.gif" alt="">', (string) $el);
Assert::same('<img src="image.gif" alt="">', $el->toHtml());
Assert::same('<img src="image.gif" alt="">', $el->startTag());
Assert::same('', $el->endTag());
});


test('', function () {
Html::$xhtml = true;
$el = Html::el('img')->setAttribute('src', 'image.gif')->setAttribute('alt', '');
Assert::same('<img src="image.gif" alt="" />', (string) $el);
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
Assert::same('<img src="image.gif" alt="">', (string) $el);
Assert::same('<img src="image.gif" alt="">', $el->startTag());
Assert::same('', $el->endTag());
});


test('', function () {
Html::$xhtml = true;
$el = Html::el('img')->accesskey(0, true)->alt('alt', false);
Assert::same('<img accesskey="0" />', (string) $el);
Assert::same('<img accesskey="0 1" />', (string) $el->accesskey(1, true));
Assert::same('<img accesskey="0" />', (string) $el->accesskey(1, false));
Assert::same('<img accesskey="0" />', (string) $el->accesskey(0, true));
Assert::same('<img accesskey="0" />', (string) $el->accesskey(0));
Assert::same('<img accesskey="0">', (string) $el);
Assert::same('<img accesskey="0 1">', (string) $el->accesskey(1, true));
Assert::same('<img accesskey="0">', (string) $el->accesskey(1, false));
Assert::same('<img accesskey="0">', (string) $el->accesskey(0, true));
Assert::same('<img accesskey="0">', (string) $el->accesskey(0));

unset($el->accesskey);
Assert::same('<img />', (string) $el);
Assert::same('<img>', (string) $el);
});


test('', function () {
Html::$xhtml = true;
$el = Html::el('img')->appendAttribute('accesskey', 0)->setAttribute('alt', false);
Assert::same('<img accesskey="0" />', (string) $el);
Assert::same('<img accesskey="0 1" />', (string) $el->appendAttribute('accesskey', 1));
Assert::same('<img accesskey="0" />', (string) $el->appendAttribute('accesskey', 1, false));
Assert::same('<img accesskey="0" />', (string) $el->appendAttribute('accesskey', 0));
Assert::same('<img accesskey="0" />', (string) $el->setAttribute('accesskey', 0));
Assert::same('<img />', (string) $el->removeAttribute('accesskey'));
Assert::same('<img accesskey="0">', (string) $el);
Assert::same('<img accesskey="0 1">', (string) $el->appendAttribute('accesskey', 1));
Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 1, false));
Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 0));
Assert::same('<img accesskey="0">', (string) $el->setAttribute('accesskey', 0));
Assert::same('<img>', (string) $el->removeAttribute('accesskey'));
});


test('', function () {
$el = Html::el('img')->src('image.gif')->alt('')->setText('any content');
Assert::same('<img src="image.gif" alt="" />', (string) $el);
Assert::same('<img src="image.gif" alt="" />', $el->startTag());
Assert::same('', $el->endTag());

Html::$xhtml = false;
Assert::same('<img src="image.gif" alt="">', (string) $el);
Assert::same('<img src="image.gif" alt="">', $el->startTag());
Assert::same('', $el->endTag());
});


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


test('attributes escaping', function () {
Html::$xhtml = true;
Assert::same('<a one=\'"\' two="\'" three="&lt;>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));

Html::$xhtml = false;
Assert::same('<a one=\'"\' two="\'" three="<>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));
Assert::same('<a one="``xx "></a>', (string) Html::el('a')->one('``xx')); // mXSS
});
Expand Down

0 comments on commit 75af0ff

Please sign in to comment.