1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
/**
* Test: Nette\Utils\Html basic usage.
*/
declare(strict_types=1);
use Nette\Utils\Html;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
test('basic image element generation', function () {
$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('', $el->endTag());
});
test('setting attributes with setAttribute', function () {
$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('', $el->endTag());
});
test('accesskey manipulation and removal', function () {
$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));
unset($el->accesskey);
Assert::same('<img>', (string) $el);
});
test('appending attribute values', function () {
$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'));
});
test('ignoring text for void element', 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());
});
test('chained attribute methods and property assignments', function () {
$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());
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('<img src="image.gif" alt="alt2 alt3">', (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('<span src="image.gif" alt="alt2 alt3" style="float:left" class="three" lang="" title="0" checked name="testname"></span>', (string) $el);
});
test('formatting numeric attribute values', function () {
$el = Html::el('span');
$el->small = 1e-8;
$el->big = 1e20;
Assert::same('<span small="0.00000001" big="100000000000000000000"></span>', (string) $el);
});
test('escaping special characters in attributes', function () {
Assert::same('<a one=\'"\' two="\'" three="<>" four="&amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&'));
Assert::same('<a one="``xx "></a>', (string) Html::el('a')->one('``xx')); // mXSS
});
class BR implements Nette\HtmlStringable
{
public function __toString(): string
{
return '<br>';
}
}
test('content setting with text and HTML conversion', function () {
Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->setText('Hello – World'));
Assert::same('<p>Hello – World</p>', (string) Html::el('p')->setHtml('Hello – World'));
Assert::same('<p><br></p>', (string) Html::el('p')->setText(Html::el('br')));
Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(Html::el('br')));
Assert::same('<p><br></p>', (string) Html::el('p')->setText(new BR));
Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(new BR));
});
test('appending text and HTML with stringable content', function () {
Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->addText('Hello – World'));
Assert::same('<p>Hello – World</p>', (string) Html::el('p')->addHtml('Hello – World'));
Assert::same('<p><br></p>', (string) Html::el('p')->addText(Html::el('br')));
Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(Html::el('br')));
Assert::same('<p><br></p>', (string) Html::el('p')->addText(new BR));
Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(new BR));
});
test('child element creation and text extraction', function () {
$el = Html::el('p')->setHtml('Hello – World');
$el->create('a')->setText('link');
Assert::same('<p>Hello – World<a>link</a></p>', (string) $el);
Assert::same('Hello – Worldlink', $el->getText());
Assert::same('Hello – Worldlink', $el->toText());
});
test('email link obfuscation', function () {
Assert::same('<a href="mailto:dave@example.com"></a>', (string) Html::el('a')->href('mailto:dave@example.com'));
});
test('URL generation with query parameters', function () {
Assert::same('<a href="file.php?a=10"></a>', (string) Html::el('a')->href('file.php', ['a' => 10]));
});
test('magic property existence for attributes', 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('attribute state after modifications', 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('batch attribute addition and removal', 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 plain text conversion', function () {
Assert::same('hello"', Html::htmlToText('<a href="#">hello"</a>'));
Assert::same(' text', Html::htmlToText('<!-- comment --> text'));
Assert::same("' ' ' \"", Html::htmlToText('' ' ' "'));
});
|