Skip to content

Dev/3.1.1 #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 3.1.1

### Changed
- Fixed issue with numbers in comments.
- Updated minimume php version to correct version.
- Comment tags are now self-closing when cleanup input is set to false.

## 3.1.0

### Changed
- Updated to include Tidelift subscription option.
- Removed php-coverall.
- Removed Guzzle 6 Adapter.
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
PHP Html Parser
==========================

Version 3.1.0

[![Build Status](https://travis-ci.org/paquettg/php-html-parser.png)](https://travis-ci.org/paquettg/php-html-parser)
[![Coverage Status](https://coveralls.io/repos/paquettg/php-html-parser/badge.png)](https://coveralls.io/r/paquettg/php-html-parser)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/paquettg/php-html-parser/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/paquettg/php-html-parser/?branch=master)
Expand Down Expand Up @@ -258,7 +256,7 @@ unset($a);
echo $dom; // '<div class="all"><p>Hey bro, <br /> :)</p></div>');
```

You can modify the text of `TextNode` objects easely. Please note that, if you set an encoding, the new text will be encoded using the existing encoding.
You can modify the text of `TextNode` objects easily. Please note that, if you set an encoding, the new text will be encoded using the existing encoding.

```php
use PHPHtmlParser\Dom;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-mbstring": "*",
"ext-zlib": "*",
"ext-curl": "*",
Expand Down
16 changes: 16 additions & 0 deletions src/PHPHtmlParser/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ public function char(?int $char = null): string
return $this->content[$char ?? $this->pos] ?? '';
}

/**
* Gets a string from the current character position.
*
* @param int $length
* @return string
*/
public function string(int $length = 1): string
{
$string = '';
$position = $this->pos;
do {
$string .= $this->char($position++);
} while ($position < $this->pos + $length);
return $string;
}

/**
* Moves the current position forward.
*
Expand Down
8 changes: 8 additions & 0 deletions src/PHPHtmlParser/Dom/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
->setOpening('<?')
->setClosing(' ?>')
->selfClosing();
} elseif($content->string(3) == '!--') {
// comment tag
$tag = $content->fastForward(3)
->copyByToken(StringToken::CLOSECOMMENT(), true);
$tag = (new Tag($tag))
->setOpening('<!--')
->setClosing('-->')
->selfClosing();
} else {
$tag = \strtolower($content->copyByToken(StringToken::SLASH(), true));
if (\trim($tag) == '') {
Expand Down
2 changes: 2 additions & 0 deletions src/PHPHtmlParser/Dom/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ public function makeOpeningTag()
} catch (AttributeNotFoundException $e) {
// attribute that was in the array not found in the array... let's continue.
continue;
} catch (\TypeError $e) {
$val = null;
}
$val = $attributeDTO->getValue();
if (\is_null($val)) {
Expand Down
2 changes: 2 additions & 0 deletions src/PHPHtmlParser/Enum/StringToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
* @method static StringToken EQUAL()
* @method static StringToken SLASH()
* @method static StringToken ATTR()
* @method static StringToken CLOSECOMMENT()
*/
class StringToken extends Enum
{
private const BLANK = " \t\r\n";
private const EQUAL = ' =/>';
private const SLASH = " />\r\n\t";
private const ATTR = ' >';
private const CLOSECOMMENT = '-->';
}
34 changes: 34 additions & 0 deletions tests/Dom/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use PHPHtmlParser\Dom;
use PHPHtmlParser\Options;
use PHPUnit\Framework\TestCase;

class CommentTest extends TestCase
{
/**
* @var Dom
*/
private $dom;

public function setUp()
{
$dom = new Dom();
$options = new Options();
$options->setCleanupInput(false);
$dom->loadStr('<!-- test comment with number 2 -->', $options);
$this->dom = $dom;
}

public function tearDown()
{
Mockery::close();
}

public function testLoadCommentInnerHtml()
{
$this->assertEquals('<!-- test comment with number 2 -->', $this->dom->innerHtml);
}
}
1 change: 1 addition & 0 deletions tests/Node/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPHtmlParser\Dom;
use PHPHtmlParser\Dom\Node\TextNode;
use PHPHtmlParser\Options;
use PHPUnit\Framework\TestCase;
use stringEncode\Encode;

Expand Down