|
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
|
|
|
const SCRIPT_BODY_REPLACEMENT = 'Contents were removed by HtmlMatcher'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Matcher |
|
21
|
|
|
*/ |
|
22
|
|
|
private $elementMatcher; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Matcher $elementMatcher |
|
26
|
|
|
* |
|
27
|
|
|
* @return HtmlMatcher |
|
28
|
|
|
*/ |
|
29
|
65 |
|
public static function htmlPiece(Matcher $elementMatcher = null) |
|
30
|
|
|
{ |
|
31
|
65 |
|
return new static($elementMatcher); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
65 |
|
private function __construct(Matcher $elementMatcher = null) |
|
35
|
|
|
{ |
|
36
|
65 |
|
$this->elementMatcher = $elementMatcher; |
|
37
|
65 |
|
} |
|
38
|
|
|
|
|
39
|
15 |
|
public function describeTo(Description $description) |
|
40
|
|
|
{ |
|
41
|
15 |
|
$description->appendText('valid html piece '); |
|
42
|
15 |
|
if ($this->elementMatcher) { |
|
43
|
14 |
|
$description->appendDescriptionOf($this->elementMatcher); |
|
44
|
14 |
|
} |
|
45
|
15 |
|
} |
|
46
|
|
|
|
|
47
|
65 |
|
protected function matchesWithDiagnosticDescription($html, Description $mismatchDescription) |
|
48
|
|
|
{ |
|
49
|
65 |
|
$internalErrors = libxml_use_internal_errors(true); |
|
50
|
65 |
|
$document = new \DOMDocument(); |
|
51
|
|
|
|
|
52
|
65 |
|
$html = $this->stripScriptsContents($html); |
|
53
|
|
|
|
|
54
|
65 |
|
if (!@$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) { |
|
55
|
|
|
$mismatchDescription->appendText('there was some parsing error'); |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
65 |
|
$errors = libxml_get_errors(); |
|
60
|
65 |
|
libxml_clear_errors(); |
|
61
|
65 |
|
libxml_use_internal_errors($internalErrors); |
|
62
|
|
|
|
|
63
|
65 |
|
$result = true; |
|
64
|
|
|
/** @var \LibXMLError $error */ |
|
65
|
65 |
|
foreach ($errors as $error) { |
|
66
|
32 |
|
if ($this->isUnknownTagError($error)) { |
|
67
|
31 |
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$mismatchDescription->appendText('there was parsing error: ') |
|
71
|
1 |
|
->appendText(trim($error->message)) |
|
72
|
1 |
|
->appendText(' on line ') |
|
73
|
1 |
|
->appendText($error->line); |
|
74
|
1 |
|
$result = false; |
|
75
|
65 |
|
} |
|
76
|
|
|
|
|
77
|
65 |
|
if ($result === false) { |
|
78
|
1 |
|
return $result; |
|
79
|
|
|
} |
|
80
|
64 |
|
$mismatchDescription->appendText('valid html piece '); |
|
81
|
|
|
|
|
82
|
64 |
|
if ($this->elementMatcher) { |
|
83
|
30 |
|
$result = $this->elementMatcher->matches($document); |
|
84
|
30 |
|
$this->elementMatcher->describeMismatch($document, $mismatchDescription); |
|
85
|
30 |
|
} |
|
86
|
|
|
|
|
87
|
64 |
|
$mismatchDescription->appendText("\nActual html:\n")->appendText($html); |
|
88
|
|
|
|
|
89
|
64 |
|
return $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
32 |
|
private function isUnknownTagError(\LibXMLError $error) |
|
93
|
|
|
{ |
|
94
|
32 |
|
return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $html |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
65 |
|
private function stripScriptsContents($html) |
|
102
|
|
|
{ |
|
103
|
65 |
|
preg_match_all("#(<script.*>).*</script>#sU", $html, $scripts); |
|
104
|
65 |
|
foreach ($scripts[0] as $index => $script) { |
|
105
|
3 |
|
$openTag = $scripts[1][$index]; |
|
106
|
3 |
|
$replacement = $openTag . self::SCRIPT_BODY_REPLACEMENT . '</script>'; |
|
107
|
3 |
|
$html = str_replace($script, $replacement, $html); |
|
108
|
65 |
|
} |
|
109
|
65 |
|
return $html; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|