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