Skip to content

Commit fdd0670

Browse files
committed
1 parent cec2cfa commit fdd0670

20 files changed

+143
-84
lines changed

autoload.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function d(...$data) {
3232

3333
spl_autoload_register(function ($class) {
3434
$class = trim($class, '\\');
35-
$path = 'PhpTemplates\\Dom';
35+
$path = 'PhpDom\\';
3636
if (strpos($class, $path) === 0) {
3737
$class = str_replace($path, '', $class);
3838
$file = __DIR__.'/src/'.$class.'.php';

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"autoload": {
1515
"psr-4": {
16-
"PhpTemplates\\": "src/"
16+
"PhpDom\\": "src/"
1717
}
1818
}
1919
}

src/Contracts/DomElementInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Contracts;
3+
namespace PhpDom\Contracts;
44

5-
use PhpTemplates\Dom\DomNodeIterator;
5+
use PhpDom\DomNodeIterator;
66

77
interface DomElementInterface
88
{

src/Contracts/DomNodeAttrInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Contracts;
3+
namespace PhpDom\Contracts;
44

55
interface DomNodeAttrInterface
66
{

src/Contracts/DomNodeInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Contracts;
3+
namespace PhpDom\Contracts;
44

55
interface DomNodeInterface extends DomElementInterface
66
{

src/Contracts/TextNodeInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Contracts;
3+
namespace PhpDom\Contracts;
44

55
interface TextNodeInterface extends DomElementInterface
66
{

src/DomNode.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomNodeAttrInterface;
6-
use PhpTemplates\Dom\Contracts\DomNodeInterface;
7-
use PhpTemplates\Dom\Traits\DomElement;
8-
use PhpTemplates\Dom\Traits\QuerySelector;
5+
use PhpDom\Contracts\DomNodeAttrInterface;
6+
use PhpDom\Contracts\DomNodeInterface;
7+
use PhpDom\Traits\DomElement;
8+
use PhpDom\Traits\QuerySelector;
99

1010
/**
1111
* @inheritdoc

src/DomNodeAttr.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomNodeAttrInterface;
5+
use PhpDom\Contracts\DomNodeAttrInterface;
66

77
class DomNodeAttr implements DomNodeAttrInterface
88
{

src/DomNodeIterator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomElementInterface;
5+
use PhpDom\Contracts\DomElementInterface;
66

77
class DomNodeIterator implements \Iterator, DomElementInterface
88
{

src/DomNodeList.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomElementInterface;
5+
use PhpDom\Contracts\DomElementInterface;
66

77
class DomNodeList implements \Iterator, DomElementInterface
88
{

src/DomPath.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomElementInterface;
6-
use PhpTemplates\Dom\Contracts\DomNodeInterface;
5+
use PhpDom\Contracts\DomElementInterface;
6+
use PhpDom\Contracts\DomNodeInterface;
77
use Closure;
88

99
class DomPath

src/Parser.php

+71-45
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,92 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

55
use Closure;
6-
use PhpTemplates\InvalidNodeException;
7-
use PhpTemplates\Source;
6+
use SplFileInfo;
7+
88
// todo: validari cu tipete
99
class Parser
1010
{
11-
private $dom;
12-
private $nodeQueue = [];
11+
public static $repair_html = false;
12+
public static $throw_errors = false;
13+
14+
private ?DomNode $dom;
15+
private array $nodeQueue = [];
1316

14-
private $line = 1;
15-
private $scope = 'text';
16-
private $buildingNode;
17-
private $buildingAttr;
17+
private ?SplFileInfo $file;
18+
private int $line = 1;
19+
private string $scope = 'text';
20+
private ?DomElementInterface $buildingNode;
21+
private ?DomNodeAttrInterface $buildingAttr;
1822

19-
private $options = [
20-
'repair_html' => false,
21-
'throw_errors' => false,
22-
];
23+
private $options = [];
2324

2425
public function __construct(array $options = [])
2526
{
26-
$this->options = array_merge($this->options, $options);
27+
$this->options = array_merge([
28+
'repair_html' => self::$repair_html,
29+
'throw_errors' => self::$throw_errors,
30+
], $options);
2731
}
2832

29-
public function parse(/*Source*/ $source)
30-
{todo source class
31-
$this->dom = new DomNode('#root');
33+
public function parse(Source $source)
34+
{
35+
// reset
36+
$this->file = new SplFileInfo($source->getFile());
37+
$this->line = 1;
38+
$this->dom = new DomNode('');
3239
$this->nodeQueue = [];
40+
$this->scope = 'text';
41+
$this->buildingNode = null;
42+
$this->buildingAttr = null;
3343

34-
$html = (string)$source;
35-
$chars = array_map('preg_quote', [
36-
'<',
37-
'>',
38-
'=',
39-
'"',
40-
'\'',
41-
//'!',
42-
//'?',
43-
//'-',
44-
//'\\',
45-
]);
46-
$chars = implode('|', array_merge([
47-
'<[a-zA-Z0-9_\-]+',
48-
'<\/[a-zA-Z0-9_\-]+>',
49-
'\/>',
50-
'<!--',
51-
'-->',
52-
'= *"',
53-
'= *\'',
54-
'[\s\t ]+',
55-
'[\n\r]',
56-
], $chars));
57-
58-
$tokens = preg_split("/($chars)/ms", $html, -1, PREG_SPLIT_DELIM_CAPTURE);
44+
// parse
45+
$tokens = $this->tokenize((string)$source);
5946
foreach ($tokens as $token) {
6047
$this->add($token);
6148
}
6249

50+
// return nodelist or node (if only one root element found)
6351
if ($this->dom->getChildNodes()->count() > 1) {
6452
return $this->dom->getChildNodes();
6553
}
6654

6755
return $this->dom->getChildNodes()->first();
6856
}
57+
58+
// split html string into relevant tokens to be interpreted in context
59+
protected function tokenize(string $html)
60+
{
61+
static $chars;
62+
if (!$chars)
63+
{
64+
$chars = array_map('preg_quote', [
65+
'<',
66+
'>',
67+
'=',
68+
'"',
69+
'\'',
70+
//'!',
71+
//'?',
72+
//'-',
73+
//'\\',
74+
]);
75+
$chars = implode('|', array_merge([
76+
'<[a-zA-Z0-9_\-]+',
77+
'<\/[a-zA-Z0-9_\-]+>',
78+
'\/>',
79+
'<!--',
80+
'-->',
81+
'= *"',
82+
'= *\'',
83+
'[\s\t ]+',
84+
'[\n\r]',
85+
], $chars));
86+
}
87+
88+
return preg_split("/($chars)/ms", $html, -1, PREG_SPLIT_DELIM_CAPTURE);
89+
}
6990

7091
protected function add($token)
7192
{
@@ -100,7 +121,8 @@ protected function textScope($token)
100121
// treat it as text
101122
return $this->buildingNode->append($token);
102123
}
103-
throw new \Exception("Unexpected token $token at line {$this->line}, expecting end tag for node <{$parentNode->getNodeName()}> started at line {$parentNode->meta['lineNumber']}");
124+
$inFile = $this->file ? 'in ' . $this->file->getRealPath() : '';
125+
throw new \Exception("Unexpected token $token $inFile at line {$this->line}, expecting end tag for node <{$parentNode->getNodeName()}> started at line {$parentNode->meta['line']}");
104126
}
105127

106128
if (trim($this->buildingNode->getNodeValue()) !== '') {
@@ -119,7 +141,8 @@ protected function textScope($token)
119141
}
120142

121143
$this->buildingNode = new DomNode($m[1]);
122-
$this->buildingNode->meta['lineNumber'] = $this->line;
144+
$this->buildingNode->meta['file'] = $this->file;
145+
$this->buildingNode->meta['line'] = $this->line;
123146
$this->scope = 'nodeDeclaration';
124147
}
125148

@@ -239,7 +262,10 @@ protected function tryCloseTag($name)
239262
$parentNode = $parentNode ? $parentNode : $this->dom;
240263

241264
if ($name == 'br') {
242-
return $parentNode->appendChild(new DomNode('br'));
265+
$br = new DomNode('br');
266+
$br->meta['file'] = $this->file;
267+
$br->meta['line'] = $this->line;
268+
return $parentNode->appendChild($br);
243269
}
244270

245271
$max = count($this->nodeQueue) -1;

src/QuerySelector.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\DomNodeInterface;
6-
use PhpTemplates\Dom\Contracts\DomElementInterface;
5+
use PhpDom\Contracts\DomNodeInterface;
6+
use PhpDom\Contracts\DomElementInterface;
77
use Closure;
88

99
/*

src/Source.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PhpDom;
4+
5+
/**
6+
* This class propose is to represent a source file and its executable content
7+
*/
8+
class Source
9+
{
10+
private $code;
11+
private $file;
12+
private $startLine;
13+
14+
public function __construct(string $code, string $file, int $startLine = 0)
15+
{
16+
$this->code = $code;
17+
$this->file = $file;
18+
$this->startLine = $startLine;
19+
}
20+
21+
public function __toString()
22+
{
23+
return $this->code;
24+
}
25+
26+
public function getFile()
27+
{
28+
return $this->file;
29+
}
30+
31+
public function getStartLine()
32+
{
33+
return $this->startLine;
34+
}
35+
}

src/TextNode.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace PhpTemplates\Dom;
3+
namespace PhpDom;
44

5-
use PhpTemplates\Dom\Contracts\TextNodeInterface;
6-
use PhpTemplates\Dom\Traits\DomElement;
5+
use PhpDom\Contracts\TextNodeInterface;
6+
use PhpDom\Traits\DomElement;
77

88
/**
99
* @inheritdoc

src/Traits/DomElement.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Traits;
3+
namespace PhpDom\Traits;
44

5-
use PhpTemplates\Dom\Contracts\DomElementInterface;
6-
use PhpTemplates\Dom\DomNodeIterator;
5+
use PhpDom\Contracts\DomElementInterface;
6+
use PhpDom\DomNodeIterator;
77

88
trait DomElement
99
{

src/Traits/QuerySelector.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PhpTemplates\Dom\Traits;
3+
namespace PhpDom\Traits;
44

5-
use PhpTemplates\Dom\QuerySelector as QuerySelectorClass;
5+
use PhpDom\QuerySelector as QuerySelectorClass;
66

77
trait QuerySelector
88
{

tests/basic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require('./../autoload.php');
44

5-
$parser = new PhpTemplates\Dom\Parser([
5+
$parser = new PhpDom\Parser([
66

77
]);
88

tests/query.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
require('./../autoload.php');
44

5-
use PhpTemplates\Dom\Parser;
6-
use PhpTemplates\Dom\DomNode;
7-
use PhpTemplates\Dom\QuerySelector;
5+
use PhpDom\Parser;
6+
use PhpDom\DomNode;
7+
use PhpDom\QuerySelector;
88

9-
$parser = new PhpTemplates\Dom\Parser([
9+
$parser = new PhpDom\Parser([
1010

1111
]);
1212

todo.md

-2
This file was deleted.

0 commit comments

Comments
 (0)