@@ -10,217 +10,217 @@ |
||
10 | 10 | |
11 | 11 | class ComplexTagMatcher extends TagMatcher { |
12 | 12 | |
13 | - /** |
|
14 | - * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors |
|
15 | - * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257 |
|
16 | - */ |
|
17 | - const XML_UNKNOWN_TAG_ERROR_CODE = 801; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - private $tagHtmlOutline; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var Matcher |
|
26 | - */ |
|
27 | - private $matcher; |
|
28 | - |
|
29 | - /** |
|
30 | - * @param string $htmlOutline |
|
31 | - * |
|
32 | - * @return self |
|
33 | - */ |
|
34 | - public static function tagMatchingOutline( $htmlOutline ) { |
|
35 | - return new self( $htmlOutline ); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $tagHtmlRepresentation |
|
40 | - */ |
|
41 | - public function __construct( $tagHtmlRepresentation ) { |
|
42 | - parent::__construct(); |
|
43 | - |
|
44 | - $this->tagHtmlOutline = $tagHtmlRepresentation; |
|
45 | - $this->matcher = $this->createMatcherFromHtml( $tagHtmlRepresentation ); |
|
46 | - } |
|
47 | - |
|
48 | - public function describeTo( Description $description ) { |
|
49 | - $description->appendText( 'tag matching outline `' ) |
|
50 | - ->appendText( $this->tagHtmlOutline ) |
|
51 | - ->appendText( '` ' ); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param \DOMElement $item |
|
56 | - * @param Description $mismatchDescription |
|
57 | - * |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
61 | - $result = $this->matcher->matches( $item ); |
|
62 | - if ( !$result ) { |
|
63 | - $mismatchDescription->appendText( 'was `' ) |
|
64 | - ->appendText( $this->elementToString( $item ) ) |
|
65 | - ->appendText( '`' ); |
|
66 | - } |
|
67 | - return $result; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $htmlOutline |
|
72 | - * |
|
73 | - * @return Matcher |
|
74 | - */ |
|
75 | - private function createMatcherFromHtml( $htmlOutline ) { |
|
76 | - $document = $this->parseHtml( $htmlOutline ); |
|
77 | - $targetTag = $this->getSingleTagFromThe( $document ); |
|
78 | - |
|
79 | - $this->assertTagDoesNotContainChildren( $targetTag ); |
|
80 | - |
|
81 | - $attributeMatchers = $this->createAttributeMatchers( $htmlOutline, $targetTag ); |
|
82 | - $classMatchers = $this->createClassMatchers( $targetTag ); |
|
83 | - |
|
84 | - return AllOf::allOf( |
|
85 | - new TagNameMatcher( IsEqual::equalTo( $targetTag->tagName ) ), |
|
86 | - call_user_func_array( [ AllOf::class, 'allOf' ], $attributeMatchers ), |
|
87 | - call_user_func_array( [ AllOf::class, 'allOf' ], $classMatchers ) |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param \LibXMLError $error |
|
93 | - * |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - private function isUnknownTagError( \LibXMLError $error ) { |
|
97 | - return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param string $inputHtml |
|
102 | - * @param string $attributeName |
|
103 | - * |
|
104 | - * @return bool |
|
105 | - */ |
|
106 | - private function isBooleanAttribute( $inputHtml, $attributeName ) { |
|
107 | - $quotedName = preg_quote( $attributeName, '/' ); |
|
108 | - |
|
109 | - $attributeHasValueAssigned = preg_match( "/\b{$quotedName}\s*=/ui", $inputHtml ); |
|
110 | - return !$attributeHasValueAssigned; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @param string $html |
|
115 | - * |
|
116 | - * @return \DOMDocument |
|
117 | - * @throws \InvalidArgumentException |
|
118 | - */ |
|
119 | - private function parseHtml( $html ) { |
|
120 | - $internalErrors = libxml_use_internal_errors( true ); |
|
121 | - $document = new \DOMDocument(); |
|
122 | - |
|
123 | - // phpcs:ignore Generic.PHP.NoSilencedErrors |
|
124 | - if ( !@$document->loadHTML( $html ) ) { |
|
125 | - throw new \InvalidArgumentException( "There was some parsing error of `$html`" ); |
|
126 | - } |
|
127 | - |
|
128 | - $errors = libxml_get_errors(); |
|
129 | - libxml_clear_errors(); |
|
130 | - libxml_use_internal_errors( $internalErrors ); |
|
131 | - |
|
132 | - /** @var \LibXMLError $error */ |
|
133 | - foreach ( $errors as $error ) { |
|
134 | - if ( $this->isUnknownTagError( $error ) ) { |
|
135 | - continue; |
|
136 | - } |
|
137 | - |
|
138 | - throw new \InvalidArgumentException( |
|
139 | - 'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line |
|
140 | - ); |
|
141 | - } |
|
142 | - |
|
143 | - return $document; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param \DOMDocument $document |
|
148 | - * |
|
149 | - * @return \DOMElement |
|
150 | - * @throws \InvalidArgumentException |
|
151 | - */ |
|
152 | - private function getSingleTagFromThe( \DOMDocument $document ) { |
|
153 | - $directChildren = iterator_to_array( $document->documentElement->childNodes ); |
|
154 | - |
|
155 | - $body = array_shift( $directChildren ); |
|
156 | - $directChildren = iterator_to_array( $body->childNodes ); |
|
157 | - |
|
158 | - if ( count( $directChildren ) !== 1 ) { |
|
159 | - throw new InvalidArgumentException( |
|
160 | - 'Expected exactly 1 tag description, got ' . count( $directChildren ) |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - return $directChildren[0]; |
|
165 | - } |
|
166 | - |
|
167 | - private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) { |
|
168 | - if ( $targetTag->childNodes->length > 0 ) { |
|
169 | - throw new InvalidArgumentException( 'Nested elements are not allowed' ); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param string $inputHtml |
|
175 | - * @param \DOMElement $targetTag |
|
176 | - * |
|
177 | - * @return AttributeMatcher[] |
|
178 | - */ |
|
179 | - private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) { |
|
180 | - $attributeMatchers = []; |
|
181 | - /** @var \DOMAttr $attribute */ |
|
182 | - foreach ( $targetTag->attributes as $attribute ) { |
|
183 | - if ( $attribute->name === 'class' ) { |
|
184 | - continue; |
|
185 | - } |
|
186 | - |
|
187 | - $attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) ); |
|
188 | - if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) { |
|
189 | - $attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) ); |
|
190 | - } |
|
191 | - |
|
192 | - $attributeMatchers[] = $attributeMatcher; |
|
193 | - } |
|
194 | - return $attributeMatchers; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param \DOMElement $targetTag |
|
199 | - * |
|
200 | - * @return ClassMatcher[] |
|
201 | - */ |
|
202 | - private function createClassMatchers( \DOMElement $targetTag ) { |
|
203 | - $classMatchers = []; |
|
204 | - $classValue = $targetTag->getAttribute( 'class' ); |
|
205 | - foreach ( explode( ' ', $classValue ) as $expectedClass ) { |
|
206 | - if ( $expectedClass === '' ) { |
|
207 | - continue; |
|
208 | - } |
|
209 | - $classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) ); |
|
210 | - } |
|
211 | - return $classMatchers; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @param \DOMElement $element |
|
216 | - * |
|
217 | - * @return string |
|
218 | - */ |
|
219 | - private function elementToString( \DOMElement $element ) { |
|
220 | - $newDocument = new \DOMDocument(); |
|
221 | - $cloned = $element->cloneNode( true ); |
|
222 | - $newDocument->appendChild( $newDocument->importNode( $cloned, true ) ); |
|
223 | - return trim( $newDocument->saveHTML() ); |
|
224 | - } |
|
13 | + /** |
|
14 | + * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors |
|
15 | + * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257 |
|
16 | + */ |
|
17 | + const XML_UNKNOWN_TAG_ERROR_CODE = 801; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + private $tagHtmlOutline; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var Matcher |
|
26 | + */ |
|
27 | + private $matcher; |
|
28 | + |
|
29 | + /** |
|
30 | + * @param string $htmlOutline |
|
31 | + * |
|
32 | + * @return self |
|
33 | + */ |
|
34 | + public static function tagMatchingOutline( $htmlOutline ) { |
|
35 | + return new self( $htmlOutline ); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $tagHtmlRepresentation |
|
40 | + */ |
|
41 | + public function __construct( $tagHtmlRepresentation ) { |
|
42 | + parent::__construct(); |
|
43 | + |
|
44 | + $this->tagHtmlOutline = $tagHtmlRepresentation; |
|
45 | + $this->matcher = $this->createMatcherFromHtml( $tagHtmlRepresentation ); |
|
46 | + } |
|
47 | + |
|
48 | + public function describeTo( Description $description ) { |
|
49 | + $description->appendText( 'tag matching outline `' ) |
|
50 | + ->appendText( $this->tagHtmlOutline ) |
|
51 | + ->appendText( '` ' ); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param \DOMElement $item |
|
56 | + * @param Description $mismatchDescription |
|
57 | + * |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
61 | + $result = $this->matcher->matches( $item ); |
|
62 | + if ( !$result ) { |
|
63 | + $mismatchDescription->appendText( 'was `' ) |
|
64 | + ->appendText( $this->elementToString( $item ) ) |
|
65 | + ->appendText( '`' ); |
|
66 | + } |
|
67 | + return $result; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $htmlOutline |
|
72 | + * |
|
73 | + * @return Matcher |
|
74 | + */ |
|
75 | + private function createMatcherFromHtml( $htmlOutline ) { |
|
76 | + $document = $this->parseHtml( $htmlOutline ); |
|
77 | + $targetTag = $this->getSingleTagFromThe( $document ); |
|
78 | + |
|
79 | + $this->assertTagDoesNotContainChildren( $targetTag ); |
|
80 | + |
|
81 | + $attributeMatchers = $this->createAttributeMatchers( $htmlOutline, $targetTag ); |
|
82 | + $classMatchers = $this->createClassMatchers( $targetTag ); |
|
83 | + |
|
84 | + return AllOf::allOf( |
|
85 | + new TagNameMatcher( IsEqual::equalTo( $targetTag->tagName ) ), |
|
86 | + call_user_func_array( [ AllOf::class, 'allOf' ], $attributeMatchers ), |
|
87 | + call_user_func_array( [ AllOf::class, 'allOf' ], $classMatchers ) |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param \LibXMLError $error |
|
93 | + * |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + private function isUnknownTagError( \LibXMLError $error ) { |
|
97 | + return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param string $inputHtml |
|
102 | + * @param string $attributeName |
|
103 | + * |
|
104 | + * @return bool |
|
105 | + */ |
|
106 | + private function isBooleanAttribute( $inputHtml, $attributeName ) { |
|
107 | + $quotedName = preg_quote( $attributeName, '/' ); |
|
108 | + |
|
109 | + $attributeHasValueAssigned = preg_match( "/\b{$quotedName}\s*=/ui", $inputHtml ); |
|
110 | + return !$attributeHasValueAssigned; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @param string $html |
|
115 | + * |
|
116 | + * @return \DOMDocument |
|
117 | + * @throws \InvalidArgumentException |
|
118 | + */ |
|
119 | + private function parseHtml( $html ) { |
|
120 | + $internalErrors = libxml_use_internal_errors( true ); |
|
121 | + $document = new \DOMDocument(); |
|
122 | + |
|
123 | + // phpcs:ignore Generic.PHP.NoSilencedErrors |
|
124 | + if ( !@$document->loadHTML( $html ) ) { |
|
125 | + throw new \InvalidArgumentException( "There was some parsing error of `$html`" ); |
|
126 | + } |
|
127 | + |
|
128 | + $errors = libxml_get_errors(); |
|
129 | + libxml_clear_errors(); |
|
130 | + libxml_use_internal_errors( $internalErrors ); |
|
131 | + |
|
132 | + /** @var \LibXMLError $error */ |
|
133 | + foreach ( $errors as $error ) { |
|
134 | + if ( $this->isUnknownTagError( $error ) ) { |
|
135 | + continue; |
|
136 | + } |
|
137 | + |
|
138 | + throw new \InvalidArgumentException( |
|
139 | + 'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line |
|
140 | + ); |
|
141 | + } |
|
142 | + |
|
143 | + return $document; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param \DOMDocument $document |
|
148 | + * |
|
149 | + * @return \DOMElement |
|
150 | + * @throws \InvalidArgumentException |
|
151 | + */ |
|
152 | + private function getSingleTagFromThe( \DOMDocument $document ) { |
|
153 | + $directChildren = iterator_to_array( $document->documentElement->childNodes ); |
|
154 | + |
|
155 | + $body = array_shift( $directChildren ); |
|
156 | + $directChildren = iterator_to_array( $body->childNodes ); |
|
157 | + |
|
158 | + if ( count( $directChildren ) !== 1 ) { |
|
159 | + throw new InvalidArgumentException( |
|
160 | + 'Expected exactly 1 tag description, got ' . count( $directChildren ) |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + return $directChildren[0]; |
|
165 | + } |
|
166 | + |
|
167 | + private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) { |
|
168 | + if ( $targetTag->childNodes->length > 0 ) { |
|
169 | + throw new InvalidArgumentException( 'Nested elements are not allowed' ); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param string $inputHtml |
|
175 | + * @param \DOMElement $targetTag |
|
176 | + * |
|
177 | + * @return AttributeMatcher[] |
|
178 | + */ |
|
179 | + private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) { |
|
180 | + $attributeMatchers = []; |
|
181 | + /** @var \DOMAttr $attribute */ |
|
182 | + foreach ( $targetTag->attributes as $attribute ) { |
|
183 | + if ( $attribute->name === 'class' ) { |
|
184 | + continue; |
|
185 | + } |
|
186 | + |
|
187 | + $attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) ); |
|
188 | + if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) { |
|
189 | + $attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) ); |
|
190 | + } |
|
191 | + |
|
192 | + $attributeMatchers[] = $attributeMatcher; |
|
193 | + } |
|
194 | + return $attributeMatchers; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param \DOMElement $targetTag |
|
199 | + * |
|
200 | + * @return ClassMatcher[] |
|
201 | + */ |
|
202 | + private function createClassMatchers( \DOMElement $targetTag ) { |
|
203 | + $classMatchers = []; |
|
204 | + $classValue = $targetTag->getAttribute( 'class' ); |
|
205 | + foreach ( explode( ' ', $classValue ) as $expectedClass ) { |
|
206 | + if ( $expectedClass === '' ) { |
|
207 | + continue; |
|
208 | + } |
|
209 | + $classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) ); |
|
210 | + } |
|
211 | + return $classMatchers; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @param \DOMElement $element |
|
216 | + * |
|
217 | + * @return string |
|
218 | + */ |
|
219 | + private function elementToString( \DOMElement $element ) { |
|
220 | + $newDocument = new \DOMDocument(); |
|
221 | + $cloned = $element->cloneNode( true ); |
|
222 | + $newDocument->appendChild( $newDocument->importNode( $cloned, true ) ); |
|
223 | + return trim( $newDocument->saveHTML() ); |
|
224 | + } |
|
225 | 225 | |
226 | 226 | } |
@@ -8,106 +8,106 @@ |
||
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 | - $document = new \DOMDocument(); |
|
51 | - |
|
52 | - $html = $this->escapeScriptTagContents( $html ); |
|
53 | - |
|
54 | - // phpcs:ignore Generic.PHP.NoSilencedErrors |
|
55 | - 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 | - $errors = libxml_get_errors(); |
|
61 | - libxml_clear_errors(); |
|
62 | - libxml_use_internal_errors( $internalErrors ); |
|
63 | - |
|
64 | - $result = true; |
|
65 | - /** @var \LibXMLError $error */ |
|
66 | - foreach ( $errors as $error ) { |
|
67 | - if ( $this->isUnknownTagError( $error ) ) { |
|
68 | - continue; |
|
69 | - } |
|
70 | - |
|
71 | - $mismatchDescription->appendText( 'there was parsing error: ' ) |
|
72 | - ->appendText( trim( $error->message ) ) |
|
73 | - ->appendText( ' on line ' ) |
|
74 | - ->appendText( $error->line ); |
|
75 | - $result = false; |
|
76 | - } |
|
77 | - |
|
78 | - if ( $result === false ) { |
|
79 | - return $result; |
|
80 | - } |
|
81 | - $mismatchDescription->appendText( 'valid html piece ' ); |
|
82 | - |
|
83 | - if ( $this->elementMatcher ) { |
|
84 | - $result = $this->elementMatcher->matches( $document ); |
|
85 | - $this->elementMatcher->describeMismatch( $document, $mismatchDescription ); |
|
86 | - } |
|
87 | - |
|
88 | - $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html ); |
|
89 | - |
|
90 | - return $result; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param \LibXMLError $error |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - private function isUnknownTagError( \LibXMLError $error ) { |
|
99 | - return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $html |
|
104 | - * |
|
105 | - * @return string HTML |
|
106 | - */ |
|
107 | - private function escapeScriptTagContents( $html ) { |
|
108 | - return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) { |
|
109 | - return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3]; |
|
110 | - }, $html ); |
|
111 | - } |
|
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 | + $document = new \DOMDocument(); |
|
51 | + |
|
52 | + $html = $this->escapeScriptTagContents( $html ); |
|
53 | + |
|
54 | + // phpcs:ignore Generic.PHP.NoSilencedErrors |
|
55 | + 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 | + $errors = libxml_get_errors(); |
|
61 | + libxml_clear_errors(); |
|
62 | + libxml_use_internal_errors( $internalErrors ); |
|
63 | + |
|
64 | + $result = true; |
|
65 | + /** @var \LibXMLError $error */ |
|
66 | + foreach ( $errors as $error ) { |
|
67 | + if ( $this->isUnknownTagError( $error ) ) { |
|
68 | + continue; |
|
69 | + } |
|
70 | + |
|
71 | + $mismatchDescription->appendText( 'there was parsing error: ' ) |
|
72 | + ->appendText( trim( $error->message ) ) |
|
73 | + ->appendText( ' on line ' ) |
|
74 | + ->appendText( $error->line ); |
|
75 | + $result = false; |
|
76 | + } |
|
77 | + |
|
78 | + if ( $result === false ) { |
|
79 | + return $result; |
|
80 | + } |
|
81 | + $mismatchDescription->appendText( 'valid html piece ' ); |
|
82 | + |
|
83 | + if ( $this->elementMatcher ) { |
|
84 | + $result = $this->elementMatcher->matches( $document ); |
|
85 | + $this->elementMatcher->describeMismatch( $document, $mismatchDescription ); |
|
86 | + } |
|
87 | + |
|
88 | + $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html ); |
|
89 | + |
|
90 | + return $result; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param \LibXMLError $error |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + private function isUnknownTagError( \LibXMLError $error ) { |
|
99 | + return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $html |
|
104 | + * |
|
105 | + * @return string HTML |
|
106 | + */ |
|
107 | + private function escapeScriptTagContents( $html ) { |
|
108 | + return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) { |
|
109 | + return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3]; |
|
110 | + }, $html ); |
|
111 | + } |
|
112 | 112 | |
113 | 113 | } |