Skip to content

Commit 40c335b

Browse files
committed
fix #233 - Made comments self-closing
1 parent ec1bc10 commit 40c335b

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111
- Fixed issue with numbers in comments.
1212
- Updated minimume php version to correct version.
13+
- Comment tags are now self-closing when cleanup input is set to false.
1314

1415
## 3.1.0
1516

src/PHPHtmlParser/Content.php

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public function char(?int $char = null): string
7272
return $this->content[$char ?? $this->pos] ?? '';
7373
}
7474

75+
/**
76+
* Gets a string from the current character position.
77+
*
78+
* @param int $length
79+
* @return string
80+
*/
81+
public function string(int $length = 1): string
82+
{
83+
$string = '';
84+
$position = $this->pos;
85+
do {
86+
$string .= $this->char($position++);
87+
} while ($position < $this->pos + $length);
88+
return $string;
89+
}
90+
7591
/**
7692
* Moves the current position forward.
7793
*

src/PHPHtmlParser/Dom/Parser.php

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
183183
->setOpening('<?')
184184
->setClosing(' ?>')
185185
->selfClosing();
186+
} elseif($content->string(3) == '!--') {
187+
// comment tag
188+
$tag = $content->fastForward(3)
189+
->copyByToken(StringToken::CLOSECOMMENT(), true);
190+
$tag = (new Tag($tag))
191+
->setOpening('<!--')
192+
->setClosing('-->')
193+
->selfClosing();
186194
} else {
187195
$tag = \strtolower($content->copyByToken(StringToken::SLASH(), true));
188196
if (\trim($tag) == '') {

src/PHPHtmlParser/Enum/StringToken.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
* @method static StringToken EQUAL()
1212
* @method static StringToken SLASH()
1313
* @method static StringToken ATTR()
14+
* @method static StringToken CLOSECOMMENT()
1415
*/
1516
class StringToken extends Enum
1617
{
1718
private const BLANK = " \t\r\n";
1819
private const EQUAL = ' =/>';
1920
private const SLASH = " />\r\n\t";
2021
private const ATTR = ' >';
22+
private const CLOSECOMMENT = '-->';
2123
}

tests/Dom/CommentTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPHtmlParser\Dom;
6+
use PHPHtmlParser\Options;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class CommentTest extends TestCase
10+
{
11+
/**
12+
* @var Dom
13+
*/
14+
private $dom;
15+
16+
public function setUp()
17+
{
18+
$dom = new Dom();
19+
$options = new Options();
20+
$options->setCleanupInput(false);
21+
$dom->loadStr('<!-- test comment with number 2 -->', $options);
22+
$this->dom = $dom;
23+
}
24+
25+
public function tearDown()
26+
{
27+
Mockery::close();
28+
}
29+
30+
public function testLoadCommentInnerHtml()
31+
{
32+
$this->assertEquals('<!-- test comment with number 2 -->', $this->dom->innerHtml);
33+
}
34+
}

tests/Node/TextTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,4 @@ public function testSetTextEncoded()
7575
$node->setText('biz baz');
7676
$this->assertEquals('biz baz', $node->text());
7777
}
78-
79-
public function testCommentWithNumbers() {
80-
$dom = new Dom;
81-
$options = new Options();
82-
$options->setCleanupInput(false);
83-
$dom->setOptions($options);
84-
$dom->loadStr('<!-- test comment with number 2 -->');
85-
$output = $dom->outerHtml;
86-
$this->assertContains('<!-- test comment with number 2 -->', $output);
87-
}
8878
}

0 commit comments

Comments
 (0)