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