Html.basic.phpt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Html basic usage.
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Html;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. test('', function () {
  10. $el = Html::el('img')->src('image.gif')->alt('');
  11. Assert::same('<img src="image.gif" alt="">', (string) $el);
  12. Assert::same('<img src="image.gif" alt="">', $el->toHtml());
  13. Assert::same('<img src="image.gif" alt="">', $el->startTag());
  14. Assert::same('', $el->endTag());
  15. });
  16. test('', function () {
  17. $el = Html::el('img')->setAttribute('src', 'image.gif')->setAttribute('alt', '');
  18. Assert::same('<img src="image.gif" alt="">', (string) $el);
  19. Assert::same('<img src="image.gif" alt="">', $el->startTag());
  20. Assert::same('', $el->endTag());
  21. });
  22. test('', function () {
  23. $el = Html::el('img')->accesskey(0, true)->alt('alt', false);
  24. Assert::same('<img accesskey="0">', (string) $el);
  25. Assert::same('<img accesskey="0 1">', (string) $el->accesskey(1, true));
  26. Assert::same('<img accesskey="0">', (string) $el->accesskey(1, false));
  27. Assert::same('<img accesskey="0">', (string) $el->accesskey(0, true));
  28. Assert::same('<img accesskey="0">', (string) $el->accesskey(0));
  29. unset($el->accesskey);
  30. Assert::same('<img>', (string) $el);
  31. });
  32. test('', function () {
  33. $el = Html::el('img')->appendAttribute('accesskey', 0)->setAttribute('alt', false);
  34. Assert::same('<img accesskey="0">', (string) $el);
  35. Assert::same('<img accesskey="0 1">', (string) $el->appendAttribute('accesskey', 1));
  36. Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 1, false));
  37. Assert::same('<img accesskey="0">', (string) $el->appendAttribute('accesskey', 0));
  38. Assert::same('<img accesskey="0">', (string) $el->setAttribute('accesskey', 0));
  39. Assert::same('<img>', (string) $el->removeAttribute('accesskey'));
  40. });
  41. test('', function () {
  42. $el = Html::el('img')->src('image.gif')->alt('')->setText('any content');
  43. Assert::same('<img src="image.gif" alt="">', (string) $el);
  44. Assert::same('<img src="image.gif" alt="">', $el->startTag());
  45. Assert::same('', $el->endTag());
  46. });
  47. test('', function () {
  48. $el = Html::el('img')->setSrc('image.gif')->setAlt('alt1')->setAlt('alt2');
  49. Assert::same('<img src="image.gif" alt="alt2">', (string) $el);
  50. Assert::same('image.gif', $el->getSrc());
  51. Assert::null($el->getTitle());
  52. Assert::null($el->getAttribute('title'));
  53. Assert::same('alt2', $el->getAlt());
  54. Assert::same('alt2', $el->getAttribute('alt'));
  55. $el->addAlt('alt3');
  56. Assert::same('<img src="image.gif" alt="alt2 alt3">', (string) $el);
  57. $el->style = 'float:left';
  58. $el->class = 'three';
  59. $el->lang = '';
  60. $el->title = '0';
  61. $el->checked = true;
  62. $el->selected = false;
  63. $el->name = 'testname';
  64. $el->setName('span');
  65. Assert::same('<span src="image.gif" alt="alt2 alt3" style="float:left" class="three" lang="" title="0" checked name="testname"></span>', (string) $el);
  66. });
  67. test('small & big numbers', function () {
  68. $el = Html::el('span');
  69. $el->small = 1e-8;
  70. $el->big = 1e20;
  71. Assert::same('<span small="0.00000001" big="100000000000000000000"></span>', (string) $el);
  72. });
  73. test('attributes escaping', function () {
  74. Assert::same('<a one=\'"\' two="\'" three="<>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));
  75. Assert::same('<a one="``xx "></a>', (string) Html::el('a')->one('``xx')); // mXSS
  76. });
  77. class BR implements Nette\HtmlStringable
  78. {
  79. public function __toString(): string
  80. {
  81. return '<br>';
  82. }
  83. }
  84. test('setText vs. setHtml', function () {
  85. Assert::same('<p>Hello &amp;ndash; World</p>', (string) Html::el('p')->setText('Hello &ndash; World'));
  86. Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->setHtml('Hello &ndash; World'));
  87. Assert::same('<p><br></p>', (string) Html::el('p')->setText(Html::el('br')));
  88. Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(Html::el('br')));
  89. Assert::same('<p><br></p>', (string) Html::el('p')->setText(new BR));
  90. Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(new BR));
  91. });
  92. test('addText vs. addHtml', function () {
  93. Assert::same('<p>Hello &amp;ndash; World</p>', (string) Html::el('p')->addText('Hello &ndash; World'));
  94. Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->addHtml('Hello &ndash; World'));
  95. Assert::same('<p><br></p>', (string) Html::el('p')->addText(Html::el('br')));
  96. Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(Html::el('br')));
  97. Assert::same('<p><br></p>', (string) Html::el('p')->addText(new BR));
  98. Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(new BR));
  99. });
  100. test('getText vs. getHtml', function () {
  101. $el = Html::el('p')->setHtml('Hello &ndash; World');
  102. $el->create('a')->setText('link');
  103. Assert::same('<p>Hello &ndash; World<a>link</a></p>', (string) $el);
  104. Assert::same('Hello – Worldlink', $el->getText());
  105. Assert::same('Hello – Worldlink', $el->toText());
  106. });
  107. test('email obfuscate', function () {
  108. Assert::same('<a href="mailto:dave&#64;example.com"></a>', (string) Html::el('a')->href('mailto:dave@example.com'));
  109. });
  110. test('href with query', function () {
  111. Assert::same('<a href="file.php?a=10"></a>', (string) Html::el('a')->href('file.php', ['a' => 10]));
  112. });
  113. test('isset', function () {
  114. Assert::false(isset(Html::el('a')->id));
  115. Assert::true(isset(Html::el('a')->id('')->id));
  116. Html::el('a')->id = null;
  117. Assert::false(isset(Html::el('a')->id));
  118. });
  119. test('isset', function () {
  120. Assert::true(isset(Html::el('a')->setAttribute('id', '')->id));
  121. Assert::false(isset(Html::el('a')->removeAttribute('id')->id));
  122. Assert::true(isset(Html::el('a')->setAttribute('id', '')->id));
  123. Assert::false(isset(Html::el('a')->setAttribute('id', null)->id));
  124. });
  125. test('removeAttributes', function () {
  126. $el = Html::el('a')->addAttributes(['onclick' => '', 'onmouseover' => '']);
  127. Assert::true(isset($el->onclick));
  128. Assert::true(isset($el->onmouseover));
  129. $el->removeAttributes(['onclick', 'onmouseover']);
  130. Assert::false(isset($el->onclick));
  131. Assert::false(isset($el->onmouseover));
  132. });
  133. test('html to text', function () {
  134. Assert::same('hello"', Html::htmlToText('<a href="#">hello&quot;</a>'));
  135. Assert::same(' text', Html::htmlToText('<!-- comment --> text'));
  136. Assert::same("' ' ' \"", Html::htmlToText('&apos; &#39; &#x27; &quot;'));
  137. });