1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WMDE\HamcrestHtml; |
4
|
|
|
|
5
|
|
|
use Hamcrest\Description; |
6
|
|
|
use Hamcrest\DiagnosingMatcher; |
7
|
|
|
use Hamcrest\Matcher; |
8
|
|
|
|
9
|
|
|
class HtmlMatcher extends DiagnosingMatcher |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors |
13
|
|
|
* @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257 |
14
|
|
|
*/ |
15
|
|
|
const XML_UNKNOWN_TAG_ERROR_CODE = 801; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Matcher |
19
|
|
|
*/ |
20
|
|
|
private $elementMatcher; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Matcher $elementMatcher |
24
|
|
|
* |
25
|
|
|
* @return HtmlMatcher |
26
|
|
|
*/ |
27
|
65 |
|
public static function htmlPiece(Matcher $elementMatcher = null) |
28
|
|
|
{ |
29
|
65 |
|
return new static($elementMatcher); |
30
|
|
|
} |
31
|
|
|
|
32
|
65 |
|
private function __construct(Matcher $elementMatcher = null) |
33
|
|
|
{ |
34
|
65 |
|
$this->elementMatcher = $elementMatcher; |
35
|
65 |
|
} |
36
|
|
|
|
37
|
15 |
|
public function describeTo(Description $description) |
38
|
|
|
{ |
39
|
15 |
|
$description->appendText('valid html piece '); |
40
|
15 |
|
if ($this->elementMatcher) { |
41
|
14 |
|
$description->appendDescriptionOf($this->elementMatcher); |
42
|
14 |
|
} |
43
|
15 |
|
} |
44
|
|
|
|
45
|
65 |
|
protected function matchesWithDiagnosticDescription($html, Description $mismatchDescription) |
46
|
|
|
{ |
47
|
65 |
|
$internalErrors = libxml_use_internal_errors(true); |
48
|
65 |
|
$document = new \DOMDocument(); |
49
|
|
|
|
50
|
65 |
|
$html = $this->escapeScriptTagContents($html); |
51
|
|
|
|
52
|
65 |
|
if (!@$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) { |
53
|
|
|
$mismatchDescription->appendText('there was some parsing error'); |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
65 |
|
$errors = libxml_get_errors(); |
58
|
65 |
|
libxml_clear_errors(); |
59
|
65 |
|
libxml_use_internal_errors($internalErrors); |
60
|
|
|
|
61
|
65 |
|
$result = true; |
62
|
|
|
/** @var \LibXMLError $error */ |
63
|
65 |
|
foreach ($errors as $error) { |
64
|
32 |
|
if ($this->isUnknownTagError($error)) { |
65
|
31 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$mismatchDescription->appendText('there was parsing error: ') |
69
|
1 |
|
->appendText(trim($error->message)) |
70
|
1 |
|
->appendText(' on line ') |
71
|
1 |
|
->appendText($error->line); |
72
|
1 |
|
$result = false; |
73
|
65 |
|
} |
74
|
|
|
|
75
|
65 |
|
if ($result === false) { |
76
|
1 |
|
return $result; |
77
|
|
|
} |
78
|
64 |
|
$mismatchDescription->appendText('valid html piece '); |
79
|
|
|
|
80
|
64 |
|
if ($this->elementMatcher) { |
81
|
30 |
|
$result = $this->elementMatcher->matches($document); |
82
|
30 |
|
$this->elementMatcher->describeMismatch($document, $mismatchDescription); |
83
|
30 |
|
} |
84
|
|
|
|
85
|
64 |
|
$mismatchDescription->appendText("\nActual html:\n")->appendText($html); |
86
|
|
|
|
87
|
64 |
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
32 |
|
private function isUnknownTagError(\LibXMLError $error) |
91
|
|
|
{ |
92
|
32 |
|
return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $html |
97
|
|
|
* |
98
|
|
|
* @return string HTML |
99
|
|
|
*/ |
100
|
|
|
private function escapeScriptTagContents($html) |
101
|
|
|
{ |
102
|
65 |
|
return preg_replace_callback('#(<script.*>)(.*)(</script>)#isU', function ($matches) |
103
|
|
|
{ |
104
|
3 |
|
return $matches[1] . str_replace('</', '<\/', $matches[2]) . $matches[3]; |
105
|
65 |
|
}, $html); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|