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
|
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Tests\Unit\Util;
use League\CommonMark\Util\HtmlElement;
use PHPUnit\Framework\TestCase;
final class HtmlElementTest extends TestCase
{
public function testConstructorOneArgument(): void
{
$p = new HtmlElement('p');
$this->assertEquals('p', $p->getTagName());
$this->assertEmpty($p->getAllAttributes());
$this->assertEmpty($p->getContents());
}
public function testConstructorTwoArguments(): void
{
$img = new HtmlElement('img', ['src' => 'foo.jpg']);
$this->assertEquals('img', $img->getTagName());
$this->assertCount(1, $img->getAllAttributes());
$this->assertEquals('foo.jpg', $img->getAttribute('src'));
$this->assertEmpty($img->getContents());
}
public function testConstructorThreeArguments(): void
{
$li = new HtmlElement('li', ['class' => 'odd'], 'Foo');
$this->assertEquals('li', $li->getTagName());
$this->assertCount(1, $li->getAllAttributes());
$this->assertEquals('odd', $li->getAttribute('class'));
$this->assertEquals('Foo', $li->getContents());
}
public function testNonSelfClosingElement(): void
{
$p = new HtmlElement('p', [], '', false);
$this->assertEquals('<p></p>', (string) $p);
}
public function testSelfClosingElement(): void
{
$hr = new HtmlElement('hr', [], '', true);
$this->assertEquals('<hr />', (string) $hr);
}
public function testGetSetExistingAttribute(): void
{
$p = new HtmlElement('p', ['class' => 'foo']);
$this->assertCount(1, $p->getAllAttributes());
$this->assertEquals('foo', $p->getAttribute('class'));
$p->setAttribute('class', 'bar');
$this->assertCount(1, $p->getAllAttributes());
$this->assertEquals('bar', $p->getAttribute('class'));
}
public function testGetSetNonExistingAttribute(): void
{
$p = new HtmlElement('p', ['class' => 'foo']);
$this->assertCount(1, $p->getAllAttributes());
$this->assertNull($p->getAttribute('id'));
$p->setAttribute('id', 'bar');
$this->assertCount(2, $p->getAllAttributes());
$this->assertEquals('bar', $p->getAttribute('id'));
$this->assertEquals('foo', $p->getAttribute('class'));
}
public function testGetSetAttributeWithStringAndArrayValues(): void
{
$p = new HtmlElement('p', ['class' => ['foo', 'bar']]);
$this->assertCount(1, $p->getAllAttributes());
$this->assertSame('foo bar', $p->getAttribute('class'));
$p->setAttribute('class', 'baz');
$this->assertSame('baz', $p->getAttribute('class'));
$p->setAttribute('class', ['foo', 'bar', 'baz']);
$this->assertSame('foo bar baz', $p->getAttribute('class'));
$p->setAttribute('class', 'foo bar');
$this->assertSame('foo bar', $p->getAttribute('class'));
}
public function testAttributesWithArrayValues(): void
{
$p = new HtmlElement('p', ['class' => ['a', 'b', 'a']]);
$this->assertCount(1, $p->getAllAttributes());
$this->assertSame('a b', $p->getAttribute('class'));
$this->assertSame('<p class="a b"></p>', $p->__toString());
$p->setAttribute('class', ['foo', 'bar', 'foo']);
$this->assertSame('foo bar', $p->getAttribute('class'));
$this->assertSame('<p class="foo bar"></p>', $p->__toString());
// String attribute values do not have duplicate values removed
$p->setAttribute('class', 'x y z x a');
$this->assertSame('x y z x a', $p->getAttribute('class'));
$this->assertSame('<p class="x y z x a"></p>', $p->__toString());
}
public function testAttributesWithBooleanTrueValues(): void
{
$checkbox = new HtmlElement('input', ['type' => 'checkbox', 'checked' => true], '', true);
$this->assertSame('<input type="checkbox" checked>', $checkbox->__toString());
$checkbox->setAttribute('checked', false);
$this->assertSame('<input type="checkbox">', $checkbox->__toString());
$checkbox->setAttribute('checked', true);
$this->assertSame('<input type="checkbox" checked>', $checkbox->__toString());
$checkbox->setAttribute('disabled'); // implicitly true
$this->assertSame('<input type="checkbox" checked disabled>', $checkbox->__toString());
}
public function testToString(): void
{
$img = new HtmlElement('img', [], '', true);
$p = new HtmlElement('p');
$div = new HtmlElement('div');
$div->setContents([$p, $img]);
$this->assertIsString($div->getContents(true));
$this->assertEquals('<p></p><img />', $div->getContents(true));
$this->assertEquals('<div><p></p><img /></div>', $div->__toString());
}
public function testToStringWithUnescapedAttribute(): void
{
$element = new HtmlElement('p', ['id' => 'foo', 'class' => 'test" onclick="javascript:doBadThings();'], 'click me');
$this->assertEquals('<p id="foo" class="test" onclick="javascript:doBadThings();">click me</p>', $element->__toString());
}
public function testNullContentConstructor(): void
{
$img = new HtmlElement('img', [], null);
$this->assertTrue($img->getContents(false) === '');
}
public function testNullContentSetter(): void
{
$img = new HtmlElement('img');
$img->setContents(null);
$this->assertTrue($img->getContents(false) === '');
}
/**
* See https://github.com/thephpleague/commonmark/issues/376
*/
public function testRegressionWith0NotBeingRendered(): void
{
$element = new HtmlElement('em');
$element->setContents('0');
$this->assertSame('0', $element->getContents());
$element = new HtmlElement('em', [], '0');
$this->assertSame('0', $element->getContents());
}
}
|