Html.style.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Html style & class attribute.
  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('div');
  11. $el->style[] = 'text-align:right';
  12. $el->style[] = null;
  13. $el->style[] = 'background-color: blue';
  14. $el->class[] = 'one';
  15. $el->class[] = null;
  16. $el->class[] = 'two';
  17. Assert::same('<div style="text-align:right;background-color: blue" class="one two"></div>', (string) $el);
  18. $el->style = null;
  19. $el->style['text-align'] = 'left';
  20. $el->style['background-color'] = 'green';
  21. Assert::same('<div style="text-align:left;background-color:green" class="one two"></div>', (string) $el);
  22. });
  23. test('', function () {
  24. $el = Html::el('div');
  25. $el->appendAttribute('style', 'text-align:right');
  26. $el->appendAttribute('style', null);
  27. $el->appendAttribute('style', 'background-color: blue');
  28. $el->appendAttribute('class', 'one');
  29. $el->appendAttribute('class', null);
  30. $el->appendAttribute('class', 'two');
  31. Assert::same('<div style="text-align:right;background-color: blue" class="one two"></div>', (string) $el);
  32. $el->setAttribute('style', null);
  33. $el->appendAttribute('style', 'text-align', 'left');
  34. $el->appendAttribute('style', 'background-color', 'green');
  35. Assert::same('<div style="text-align:left;background-color:green" class="one two"></div>', (string) $el);
  36. $el->setAttribute('style', [
  37. 'text-align' => 'right',
  38. 'background-color' => 'red',
  39. ]);
  40. Assert::same('<div style="text-align:right;background-color:red" class="one two"></div>', (string) $el);
  41. $el->appendAttribute('style', [
  42. 'text-align' => 'center',
  43. 'color' => 'orange',
  44. ]);
  45. Assert::same('<div style="text-align:center;color:orange;background-color:red" class="one two"></div>', (string) $el);
  46. });
  47. test('append', function () {
  48. $el = Html::el('div');
  49. $el->style('color', 'white');
  50. $el->style('background-color', 'blue');
  51. $el->appendAttribute('style', 'text-align', 'left');
  52. $el->class = 'one';
  53. $el->class('', true);
  54. $el->class('two', true);
  55. $el->id('my', true);
  56. Assert::same('<div style="color:white;background-color:blue;text-align:left" class="one two" id="my"></div>', (string) $el);
  57. });
  58. test('append II', function () {
  59. $el = Html::el('div');
  60. $el->style[] = 'text-align:right';
  61. $el->style('', true);
  62. $el->style('background-color: blue', true);
  63. $el->appendAttribute('style', 'color: orange', true);
  64. Assert::same('<div style="text-align:right;background-color: blue;color: orange"></div>', (string) $el);
  65. });
  66. test('append III', function () {
  67. $el = Html::el('div');
  68. $el->class('top', true);
  69. $el->class('active', true);
  70. $el->appendAttribute('class', 'pull-right', true);
  71. Assert::same('<div class="top active pull-right"></div>', (string) $el);
  72. $el->class('top', null);
  73. $el->class('active', false);
  74. $el->appendAttribute('class', 'pull-right', false);
  75. Assert::same('<div></div>', (string) $el);
  76. });