File: Html.style.phpt

package info (click to toggle)
php-nette-utils 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,420 kB
  • sloc: php: 4,069; xml: 12; makefile: 4
file content (105 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download | duplicates (2)
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
<?php

/**
 * Test: Nette\Utils\Html style & class attribute.
 */

declare(strict_types=1);

use Nette\Utils\Html;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('appending styles and classes with array syntax', function () {
	$el = Html::el('div');
	$el->style[] = 'text-align:right';
	$el->style[] = null;
	$el->style[] = 'background-color: blue';
	$el->class[] = 'one';
	$el->class[] = null;
	$el->class[] = 'two';

	Assert::same('<div style="text-align:right;background-color: blue" class="one two"></div>', (string) $el);


	$el->style = null;
	$el->style['text-align'] = 'left';
	$el->style['background-color'] = 'green';
	Assert::same('<div style="text-align:left;background-color:green" class="one two"></div>', (string) $el);
});


test('manipulating style and class attributes with append and set', function () {
	$el = Html::el('div');
	$el->appendAttribute('style', 'text-align:right');
	$el->appendAttribute('style', null);
	$el->appendAttribute('style', 'background-color: blue');
	$el->appendAttribute('class', 'one');
	$el->appendAttribute('class', null);
	$el->appendAttribute('class', 'two');

	Assert::same('<div style="text-align:right;background-color: blue" class="one two"></div>', (string) $el);


	$el->setAttribute('style', null);
	$el->appendAttribute('style', 'text-align', 'left');
	$el->appendAttribute('style', 'background-color', 'green');
	Assert::same('<div style="text-align:left;background-color:green" class="one two"></div>', (string) $el);


	$el->setAttribute('style', [
		'text-align' => 'right',
		'background-color' => 'red',
	]);
	Assert::same('<div style="text-align:right;background-color:red" class="one two"></div>', (string) $el);


	$el->appendAttribute('style', [
		'text-align' => 'center',
		'color' => 'orange',
	]);
	Assert::same('<div style="text-align:center;color:orange;background-color:red" class="one two"></div>', (string) $el);
});


test('combining style, class, and id methods', function () {
	$el = Html::el('div');
	$el->style('color', 'white');
	$el->style('background-color', 'blue');
	$el->appendAttribute('style', 'text-align', 'left');

	$el->class = 'one';
	$el->class('', true);
	$el->class('two', true);

	$el->id('my', true);
	Assert::same('<div style="color:white;background-color:blue;text-align:left" class="one two" id="my"></div>', (string) $el);
});


test('appending style attributes with forced addition', function () {
	$el = Html::el('div');
	$el->style[] = 'text-align:right';
	$el->style('', true);
	$el->style('background-color: blue', true);
	$el->appendAttribute('style', 'color: orange', true);
	Assert::same('<div style="text-align:right;background-color: blue;color: orange"></div>', (string) $el);
});


test('toggling class names with boolean flags', function () {
	$el = Html::el('div');
	$el->class('top', true);
	$el->class('active', true);
	$el->appendAttribute('class', 'pull-right', true);
	Assert::same('<div class="top active pull-right"></div>', (string) $el);


	$el->class('top', null);
	$el->class('active', false);
	$el->appendAttribute('class', 'pull-right', false);
	Assert::same('<div></div>', (string) $el);
});