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