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