Skip to content

Commit 5bbb75c

Browse files
committed
Added initial support for 'strict' parsing option
1 parent 85b21b7 commit 5bbb75c

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

src/PHPHtmlParser/Content.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function __construct($content)
4141
$this->pos = 0;
4242
}
4343

44+
public function getPosition()
45+
{
46+
return $this->pos;
47+
}
48+
4449
/**
4550
* Gets the current character we are at.
4651
*

src/PHPHtmlParser/Dom.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPHtmlParser\Dom\HtmlNode;
55
use PHPHtmlParser\Dom\TextNode;
66
use PHPHtmlParser\Exceptions\NotLoadedException;
7+
use PHPHtmlParser\Exceptions\StrictException;
78
use stringEncode\Encode;
89

910
class Dom {
@@ -539,6 +540,12 @@ protected function parseTag()
539540
else
540541
{
541542
// no value attribute
543+
if ($this->options->strict)
544+
{
545+
// can't have this in strict html
546+
$character = $this->content->getPosition();
547+
throw new StrictException("Tag '$tag' has an attribute '$name' with out a value! (character #$character)");
548+
}
542549
$node->getTag()->$name = [
543550
'value' => null,
544551
'doubleQuote' => true,
@@ -556,6 +563,14 @@ protected function parseTag()
556563
}
557564
elseif (in_array($tag, $this->selfClosing))
558565
{
566+
567+
// Should be a self closing tag, check if we are strict
568+
if ( $this->options->strict)
569+
{
570+
$character = $this->content->getPosition();
571+
throw new StrictException("Tag '$tag' is not self clossing! (character #$character)");
572+
}
573+
559574
// We force self closing on this tag.
560575
$node->getTag()->selfClosing();
561576
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace PHPHtmlParser\Exceptions;
3+
4+
final class StrictException extends \Exception {}

src/PHPHtmlParser/Options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Options {
1010
*/
1111
protected $defaults = [
1212
'whitespaceTextNode' => true,
13+
'strict' => false,
1314
];
1415

1516
/**

tests/Options/StrictTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use PHPHtmlParser\Dom;
4+
use PHPHtmlParser\Exceptions\StrictException;
5+
6+
class StrictTest extends PHPUnit_Framework_TestCase {
7+
8+
public function testConfigStrict()
9+
{
10+
$dom = new Dom;
11+
$dom->setOptions([
12+
'strict' => true,
13+
]);
14+
$dom->load('<div><p id="hey">Hey you</p> <p id="ya">Ya you!</p></div>');
15+
$this->assertEquals(' ', $dom->getElementById('hey')->nextSibling()->text);
16+
}
17+
18+
public function testConfigStrictMissingSelfClosing()
19+
{
20+
$dom = new Dom;
21+
$dom->setOptions([
22+
'strict' => true,
23+
]);
24+
try
25+
{
26+
// should throw an exception
27+
$dom->load('<div><p id="hey">Hey you</p><br><p id="ya">Ya you!</p></div>');
28+
// we should not get here
29+
$this->assertTrue(false);
30+
}
31+
catch (StrictException $e)
32+
{
33+
$this->assertEquals("Tag 'br' is not self clossing! (character #31)", $e->getMessage());
34+
}
35+
}
36+
37+
public function testConfigStrictMissingAttribute()
38+
{
39+
$dom = new Dom;
40+
$dom->setOptions([
41+
'strict' => true,
42+
]);
43+
try
44+
{
45+
// should throw an exception
46+
$dom->load('<div><p id="hey" block>Hey you</p> <p id="ya">Ya you!</p></div>');
47+
// we should not get here
48+
$this->assertTrue(false);
49+
}
50+
catch (StrictException $e)
51+
{
52+
$this->assertEquals("Tag 'p' has an attribute 'block' with out a value! (character #22)", $e->getMessage());
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)