Skip to content

Commit

Permalink
Fixed #187 and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Jul 15, 2020
1 parent 1a1c3eb commit e37e8ef
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/PHPHtmlParser/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PHPHtmlParser;

use PHPHtmlParser\Exceptions\ContentLengthException;
use PHPHtmlParser\Exceptions\LogicalException;

/**
Expand Down Expand Up @@ -74,14 +75,27 @@ public function char(?int $char = null): string
* Moves the current position forward.
*
* @chainable
* @throws ContentLengthException
*/
public function fastForward(int $count): Content
{
if (!$this->canFastForward()) {
// trying to go over the content length, throw exception
throw new ContentLengthException('Attempt to fastForward pass the length of the content.');
}
$this->pos += $count;

return $this;
}

/**
* Checks if we can move the position forward.
*/
public function canFastForward(): bool
{
return \strlen($this->content) > $this->pos;
}

/**
* Moves the current position backward.
*
Expand Down Expand Up @@ -197,14 +211,15 @@ public function copyByToken(string $token, bool $char = false, bool $escape = fa
/**
* Skip a given set of characters.
*
* @return Content|string
* @throws LogicalException
*/
public function skip(string $string, bool $copy = false)
public function skip(string $string, bool $copy = false): string
{
$len = \strspn($this->content, $string, $this->pos);

// make it chainable if they don't want a copy
$return = $this;
if ($len === false) {
throw new LogicalException('Strspn returned false with position ' . $this->pos . '.');
}
$return = '';
if ($copy) {
$return = \substr($this->content, $this->pos, $len);
if ($return === false) {
Expand Down
20 changes: 17 additions & 3 deletions src/PHPHtmlParser/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPHtmlParser\Dom\TextNode;
use PHPHtmlParser\Exceptions\ChildNotFoundException;
use PHPHtmlParser\Exceptions\CircularException;
use PHPHtmlParser\Exceptions\ContentLengthException;
use PHPHtmlParser\Exceptions\CurlException;
use PHPHtmlParser\Exceptions\LogicalException;
use PHPHtmlParser\Exceptions\NotLoadedException;
Expand Down Expand Up @@ -646,7 +647,13 @@ private function parseTag(): array
}

// check if this is a closing tag
if ($this->content->fastForward(1)->char() == '/') {
try {
$this->content->fastForward(1);
} catch (ContentLengthException $exception) {
// we are at the end of the file
return $return;
}
if ($this->content->char() == '/') {
// end tag
$tag = $this->content->fastForward(1)
->copyByToken('slash', true);
Expand Down Expand Up @@ -683,7 +690,12 @@ private function parseTag(): array
) {
$space = $this->content->skipByToken('blank', true);
if (empty($space)) {
$this->content->fastForward(1);
try {
$this->content->fastForward(1);
} catch (ContentLengthException $exception) {
// reached the end of the content
break;
}
continue;
}

Expand Down Expand Up @@ -764,7 +776,9 @@ private function parseTag(): array
}
}

$this->content->fastForward(1);
if ($this->content->canFastForward()) {
$this->content->fastForward(1);
}

$return['status'] = true;
$return['node'] = $node;
Expand Down
14 changes: 14 additions & 0 deletions src/PHPHtmlParser/Exceptions/ContentLengthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PHPHtmlParser\Exceptions;

use Exception;

/**
* Class EmptyCollectionException.
*/
final class ContentLengthException extends Exception
{
}
8 changes: 8 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,12 @@ public function testCompatibleWithWordPressShortcode()
$this->assertEquals(' [wprs_alert type="success" content="this is a short code" /] ', $node->innerHtml);

}

public function testBrokenHtml()
{
$dom = new Dom();
$dom->loadStr('<the thing broke itV');

$this->assertEquals('<the thing broke itv></the>', $dom->outerHtml);
}
}

0 comments on commit e37e8ef

Please sign in to comment.