src('image.gif')->alt('');
Assert::same('
', (string) $el);
Assert::same('
', $el->toHtml());
Assert::same('
', $el->startTag());
Assert::same('', $el->endTag());
});
test('', function () {
$el = Html::el('img')->setAttribute('src', 'image.gif')->setAttribute('alt', '');
Assert::same('
', (string) $el);
Assert::same('
', $el->startTag());
Assert::same('', $el->endTag());
});
test('', function () {
$el = Html::el('img')->accesskey(0, true)->alt('alt', false);
Assert::same('', (string) $el);
Assert::same('
', (string) $el->accesskey(1, true));
Assert::same('
', (string) $el->accesskey(1, false));
Assert::same('
', (string) $el->accesskey(0, true));
Assert::same('
', (string) $el->accesskey(0));
unset($el->accesskey);
Assert::same('
', (string) $el);
});
test('', function () {
$el = Html::el('img')->appendAttribute('accesskey', 0)->setAttribute('alt', false);
Assert::same('
', (string) $el);
Assert::same('
', (string) $el->appendAttribute('accesskey', 1));
Assert::same('
', (string) $el->appendAttribute('accesskey', 1, false));
Assert::same('
', (string) $el->appendAttribute('accesskey', 0));
Assert::same('
', (string) $el->setAttribute('accesskey', 0));
Assert::same('
', (string) $el->removeAttribute('accesskey'));
});
test('', function () {
$el = Html::el('img')->src('image.gif')->alt('')->setText('any content');
Assert::same('
', (string) $el);
Assert::same('
', $el->startTag());
Assert::same('', $el->endTag());
});
test('', function () {
$el = Html::el('img')->setSrc('image.gif')->setAlt('alt1')->setAlt('alt2');
Assert::same('
', (string) $el);
Assert::same('image.gif', $el->getSrc());
Assert::null($el->getTitle());
Assert::null($el->getAttribute('title'));
Assert::same('alt2', $el->getAlt());
Assert::same('alt2', $el->getAttribute('alt'));
$el->addAlt('alt3');
Assert::same('
', (string) $el);
$el->style = 'float:left';
$el->class = 'three';
$el->lang = '';
$el->title = '0';
$el->checked = true;
$el->selected = false;
$el->name = 'testname';
$el->setName('span');
Assert::same('', (string) $el);
});
test('small & big numbers', function () {
$el = Html::el('span');
$el->small = 1e-8;
$el->big = 1e20;
Assert::same('', (string) $el);
});
test('attributes escaping', function () {
Assert::same('', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&'));
Assert::same('', (string) Html::el('a')->one('``xx')); // mXSS
});
class BR implements Nette\HtmlStringable
{
public function __toString(): string
{
return '
';
}
}
test('setText vs. setHtml', function () {
Assert::same('
Hello – World
', (string) Html::el('p')->setText('Hello – World')); Assert::same('Hello – World
', (string) Html::el('p')->setHtml('Hello – World')); Assert::same('Hello – World
', (string) Html::el('p')->addText('Hello – World')); Assert::same('Hello – World
', (string) Html::el('p')->addHtml('Hello – World')); Assert::same('Hello – Worldlink
', (string) $el); Assert::same('Hello – Worldlink', $el->getText()); Assert::same('Hello – Worldlink', $el->toText()); }); test('email obfuscate', function () { Assert::same('', (string) Html::el('a')->href('mailto:dave@example.com')); }); test('href with query', function () { Assert::same('', (string) Html::el('a')->href('file.php', ['a' => 10])); }); test('isset', function () { Assert::false(isset(Html::el('a')->id)); Assert::true(isset(Html::el('a')->id('')->id)); Html::el('a')->id = null; Assert::false(isset(Html::el('a')->id)); }); test('isset', function () { Assert::true(isset(Html::el('a')->setAttribute('id', '')->id)); Assert::false(isset(Html::el('a')->removeAttribute('id')->id)); Assert::true(isset(Html::el('a')->setAttribute('id', '')->id)); Assert::false(isset(Html::el('a')->setAttribute('id', null)->id)); }); test('removeAttributes', function () { $el = Html::el('a')->addAttributes(['onclick' => '', 'onmouseover' => '']); Assert::true(isset($el->onclick)); Assert::true(isset($el->onmouseover)); $el->removeAttributes(['onclick', 'onmouseover']); Assert::false(isset($el->onclick)); Assert::false(isset($el->onmouseover)); }); test('html to text', function () { Assert::same('hello"', Html::htmlToText('hello"')); Assert::same(' text', Html::htmlToText(' text')); Assert::same("' ' ' \"", Html::htmlToText('' ' ' "')); });