@@ -4,27 +4,27 @@ |
||
| 4 | 4 | |
| 5 | 5 | class XmlNodeRecursiveIterator extends \ArrayIterator { |
| 6 | 6 | |
| 7 | - public function __construct( \DOMNodeList $nodeList ) { |
|
| 8 | - $queue = $this->addElementsToQueue( [], $nodeList ); |
|
| 9 | - parent::__construct( $queue ); |
|
| 10 | - } |
|
| 7 | + public function __construct( \DOMNodeList $nodeList ) { |
|
| 8 | + $queue = $this->addElementsToQueue( [], $nodeList ); |
|
| 9 | + parent::__construct( $queue ); |
|
| 10 | + } |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @param \DOMNode[] $queue |
|
| 14 | - * @param \DOMNodeList $nodeList |
|
| 15 | - * |
|
| 16 | - * @return \DOMNode[] New queue |
|
| 17 | - */ |
|
| 18 | - private function addElementsToQueue( array $queue, \DOMNodeList $nodeList ) { |
|
| 19 | - /** @var \DOMNode $node */ |
|
| 20 | - foreach ( $nodeList as $node ) { |
|
| 21 | - $queue[] = $node; |
|
| 22 | - if ( $node->childNodes !== null ) { |
|
| 23 | - $queue = $this->addElementsToQueue( $queue, $node->childNodes ); |
|
| 24 | - } |
|
| 25 | - } |
|
| 12 | + /** |
|
| 13 | + * @param \DOMNode[] $queue |
|
| 14 | + * @param \DOMNodeList $nodeList |
|
| 15 | + * |
|
| 16 | + * @return \DOMNode[] New queue |
|
| 17 | + */ |
|
| 18 | + private function addElementsToQueue( array $queue, \DOMNodeList $nodeList ) { |
|
| 19 | + /** @var \DOMNode $node */ |
|
| 20 | + foreach ( $nodeList as $node ) { |
|
| 21 | + $queue[] = $node; |
|
| 22 | + if ( $node->childNodes !== null ) { |
|
| 23 | + $queue = $this->addElementsToQueue( $queue, $node->childNodes ); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - return $queue; |
|
| 28 | - } |
|
| 27 | + return $queue; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | } |
@@ -8,99 +8,99 @@ |
||
| 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 | - protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) { |
|
| 43 | - $internalErrors = libxml_use_internal_errors( true ); |
|
| 44 | - $document = new \DOMDocument(); |
|
| 45 | - |
|
| 46 | - $html = $this->escapeScriptTagContents( $html ); |
|
| 47 | - |
|
| 48 | - if ( !@$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) { |
|
| 49 | - $mismatchDescription->appendText( 'there was some parsing error' ); |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - $errors = libxml_get_errors(); |
|
| 54 | - libxml_clear_errors(); |
|
| 55 | - libxml_use_internal_errors( $internalErrors ); |
|
| 56 | - |
|
| 57 | - $result = true; |
|
| 58 | - /** @var \LibXMLError $error */ |
|
| 59 | - foreach ( $errors as $error ) { |
|
| 60 | - if ( $this->isUnknownTagError( $error ) ) { |
|
| 61 | - continue; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $mismatchDescription->appendText( 'there was parsing error: ' ) |
|
| 65 | - ->appendText( trim( $error->message ) ) |
|
| 66 | - ->appendText( ' on line ' ) |
|
| 67 | - ->appendText( $error->line ); |
|
| 68 | - $result = false; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ( $result === false ) { |
|
| 72 | - return $result; |
|
| 73 | - } |
|
| 74 | - $mismatchDescription->appendText( 'valid html piece ' ); |
|
| 75 | - |
|
| 76 | - if ( $this->elementMatcher ) { |
|
| 77 | - $result = $this->elementMatcher->matches( $document ); |
|
| 78 | - $this->elementMatcher->describeMismatch( $document, $mismatchDescription ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html ); |
|
| 82 | - |
|
| 83 | - return $result; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param \LibXMLError $error |
|
| 88 | - * |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - private function isUnknownTagError( \LibXMLError $error ) { |
|
| 92 | - return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param string $html |
|
| 97 | - * |
|
| 98 | - * @return string HTML |
|
| 99 | - */ |
|
| 100 | - private function escapeScriptTagContents( $html ) { |
|
| 101 | - return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) { |
|
| 102 | - return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3]; |
|
| 103 | - }, $html ); |
|
| 104 | - } |
|
| 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 | + protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) { |
|
| 43 | + $internalErrors = libxml_use_internal_errors( true ); |
|
| 44 | + $document = new \DOMDocument(); |
|
| 45 | + |
|
| 46 | + $html = $this->escapeScriptTagContents( $html ); |
|
| 47 | + |
|
| 48 | + if ( !@$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) { |
|
| 49 | + $mismatchDescription->appendText( 'there was some parsing error' ); |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + $errors = libxml_get_errors(); |
|
| 54 | + libxml_clear_errors(); |
|
| 55 | + libxml_use_internal_errors( $internalErrors ); |
|
| 56 | + |
|
| 57 | + $result = true; |
|
| 58 | + /** @var \LibXMLError $error */ |
|
| 59 | + foreach ( $errors as $error ) { |
|
| 60 | + if ( $this->isUnknownTagError( $error ) ) { |
|
| 61 | + continue; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $mismatchDescription->appendText( 'there was parsing error: ' ) |
|
| 65 | + ->appendText( trim( $error->message ) ) |
|
| 66 | + ->appendText( ' on line ' ) |
|
| 67 | + ->appendText( $error->line ); |
|
| 68 | + $result = false; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ( $result === false ) { |
|
| 72 | + return $result; |
|
| 73 | + } |
|
| 74 | + $mismatchDescription->appendText( 'valid html piece ' ); |
|
| 75 | + |
|
| 76 | + if ( $this->elementMatcher ) { |
|
| 77 | + $result = $this->elementMatcher->matches( $document ); |
|
| 78 | + $this->elementMatcher->describeMismatch( $document, $mismatchDescription ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html ); |
|
| 82 | + |
|
| 83 | + return $result; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param \LibXMLError $error |
|
| 88 | + * |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + private function isUnknownTagError( \LibXMLError $error ) { |
|
| 92 | + return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param string $html |
|
| 97 | + * |
|
| 98 | + * @return string HTML |
|
| 99 | + */ |
|
| 100 | + private function escapeScriptTagContents( $html ) { |
|
| 101 | + return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) { |
|
| 102 | + return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3]; |
|
| 103 | + }, $html ); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | } |
@@ -3,87 +3,87 @@ |
||
| 3 | 3 | use Hamcrest\Matcher; |
| 4 | 4 | |
| 5 | 5 | if ( !function_exists( 'htmlPiece' ) ) { |
| 6 | - /** |
|
| 7 | - * @param Matcher|null $elementMatcher |
|
| 8 | - * |
|
| 9 | - * @return \WMDE\HamcrestHtml\HtmlMatcher |
|
| 10 | - */ |
|
| 11 | - function htmlPiece( Matcher $elementMatcher = null ) { |
|
| 12 | - return \WMDE\HamcrestHtml\HtmlMatcher::htmlPiece( $elementMatcher ); |
|
| 13 | - } |
|
| 6 | + /** |
|
| 7 | + * @param Matcher|null $elementMatcher |
|
| 8 | + * |
|
| 9 | + * @return \WMDE\HamcrestHtml\HtmlMatcher |
|
| 10 | + */ |
|
| 11 | + function htmlPiece( Matcher $elementMatcher = null ) { |
|
| 12 | + return \WMDE\HamcrestHtml\HtmlMatcher::htmlPiece( $elementMatcher ); |
|
| 13 | + } |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | if ( !function_exists( 'havingRootElement' ) ) { |
| 17 | - function havingRootElement( Matcher $matcher = null ) { |
|
| 18 | - return \WMDE\HamcrestHtml\RootElementMatcher::havingRootElement( $matcher ); |
|
| 19 | - } |
|
| 17 | + function havingRootElement( Matcher $matcher = null ) { |
|
| 18 | + return \WMDE\HamcrestHtml\RootElementMatcher::havingRootElement( $matcher ); |
|
| 19 | + } |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if ( !function_exists( 'havingDirectChild' ) ) { |
| 23 | - function havingDirectChild( Matcher $elementMatcher = null ) { |
|
| 24 | - return \WMDE\HamcrestHtml\DirectChildElementMatcher::havingDirectChild( $elementMatcher ); |
|
| 25 | - } |
|
| 23 | + function havingDirectChild( Matcher $elementMatcher = null ) { |
|
| 24 | + return \WMDE\HamcrestHtml\DirectChildElementMatcher::havingDirectChild( $elementMatcher ); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | if ( !function_exists( 'havingChild' ) ) { |
| 29 | - function havingChild( Matcher $elementMatcher = null ) { |
|
| 30 | - return \WMDE\HamcrestHtml\ChildElementMatcher::havingChild( $elementMatcher ); |
|
| 31 | - } |
|
| 29 | + function havingChild( Matcher $elementMatcher = null ) { |
|
| 30 | + return \WMDE\HamcrestHtml\ChildElementMatcher::havingChild( $elementMatcher ); |
|
| 31 | + } |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | if ( !function_exists( 'withTagName' ) ) { |
| 35 | - /** |
|
| 36 | - * @param Matcher|string $tagName |
|
| 37 | - * |
|
| 38 | - * @return \WMDE\HamcrestHtml\TagNameMatcher |
|
| 39 | - */ |
|
| 40 | - function withTagName( $tagName ) { |
|
| 41 | - return \WMDE\HamcrestHtml\TagNameMatcher::withTagName( $tagName ); |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * @param Matcher|string $tagName |
|
| 37 | + * |
|
| 38 | + * @return \WMDE\HamcrestHtml\TagNameMatcher |
|
| 39 | + */ |
|
| 40 | + function withTagName( $tagName ) { |
|
| 41 | + return \WMDE\HamcrestHtml\TagNameMatcher::withTagName( $tagName ); |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | if ( !function_exists( 'withAttribute' ) ) { |
| 46 | - /** |
|
| 47 | - * @param Matcher|string $attributeName |
|
| 48 | - * |
|
| 49 | - * @return \WMDE\HamcrestHtml\AttributeMatcher |
|
| 50 | - */ |
|
| 51 | - function withAttribute( $attributeName ) { |
|
| 52 | - return \WMDE\HamcrestHtml\AttributeMatcher::withAttribute( $attributeName ); |
|
| 53 | - } |
|
| 46 | + /** |
|
| 47 | + * @param Matcher|string $attributeName |
|
| 48 | + * |
|
| 49 | + * @return \WMDE\HamcrestHtml\AttributeMatcher |
|
| 50 | + */ |
|
| 51 | + function withAttribute( $attributeName ) { |
|
| 52 | + return \WMDE\HamcrestHtml\AttributeMatcher::withAttribute( $attributeName ); |
|
| 53 | + } |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | if ( !function_exists( 'withClass' ) ) { |
| 57 | - /** |
|
| 58 | - * @param Matcher|string $class |
|
| 59 | - * |
|
| 60 | - * @return \WMDE\HamcrestHtml\ClassMatcher |
|
| 61 | - */ |
|
| 62 | - function withClass( $class ) { |
|
| 63 | - // TODO don't allow to call with empty string |
|
| 57 | + /** |
|
| 58 | + * @param Matcher|string $class |
|
| 59 | + * |
|
| 60 | + * @return \WMDE\HamcrestHtml\ClassMatcher |
|
| 61 | + */ |
|
| 62 | + function withClass( $class ) { |
|
| 63 | + // TODO don't allow to call with empty string |
|
| 64 | 64 | |
| 65 | - return \WMDE\HamcrestHtml\ClassMatcher::withClass( $class ); |
|
| 66 | - } |
|
| 65 | + return \WMDE\HamcrestHtml\ClassMatcher::withClass( $class ); |
|
| 66 | + } |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | if ( !function_exists( 'havingTextContents' ) ) { |
| 70 | - /** |
|
| 71 | - * @param Matcher|string $text |
|
| 72 | - * |
|
| 73 | - * @return \WMDE\HamcrestHtml\TextContentsMatcher |
|
| 74 | - */ |
|
| 75 | - function havingTextContents( $text ) { |
|
| 76 | - return \WMDE\HamcrestHtml\TextContentsMatcher::havingTextContents( $text ); |
|
| 77 | - } |
|
| 70 | + /** |
|
| 71 | + * @param Matcher|string $text |
|
| 72 | + * |
|
| 73 | + * @return \WMDE\HamcrestHtml\TextContentsMatcher |
|
| 74 | + */ |
|
| 75 | + function havingTextContents( $text ) { |
|
| 76 | + return \WMDE\HamcrestHtml\TextContentsMatcher::havingTextContents( $text ); |
|
| 77 | + } |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | if ( !function_exists( 'tagMatchingOutline' ) ) { |
| 81 | - /** |
|
| 82 | - * @param string $htmlOutline |
|
| 83 | - * |
|
| 84 | - * @return \WMDE\HamcrestHtml\ComplexTagMatcher |
|
| 85 | - */ |
|
| 86 | - function tagMatchingOutline( $htmlOutline ) { |
|
| 87 | - return \WMDE\HamcrestHtml\ComplexTagMatcher::tagMatchingOutline( $htmlOutline ); |
|
| 88 | - } |
|
| 81 | + /** |
|
| 82 | + * @param string $htmlOutline |
|
| 83 | + * |
|
| 84 | + * @return \WMDE\HamcrestHtml\ComplexTagMatcher |
|
| 85 | + */ |
|
| 86 | + function tagMatchingOutline( $htmlOutline ) { |
|
| 87 | + return \WMDE\HamcrestHtml\ComplexTagMatcher::tagMatchingOutline( $htmlOutline ); |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -8,80 +8,80 @@ |
||
| 8 | 8 | |
| 9 | 9 | class AttributeMatcher extends TagMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $attributeNameMatcher; |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $attributeNameMatcher; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var Matcher|null |
|
| 18 | - */ |
|
| 19 | - private $valueMatcher; |
|
| 16 | + /** |
|
| 17 | + * @var Matcher|null |
|
| 18 | + */ |
|
| 19 | + private $valueMatcher; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param Matcher|string $attributeName |
|
| 23 | - * |
|
| 24 | - * @return self |
|
| 25 | - */ |
|
| 26 | - public static function withAttribute( $attributeName ) { |
|
| 27 | - return new static( Util::wrapValueWithIsEqual( $attributeName ) ); |
|
| 28 | - } |
|
| 21 | + /** |
|
| 22 | + * @param Matcher|string $attributeName |
|
| 23 | + * |
|
| 24 | + * @return self |
|
| 25 | + */ |
|
| 26 | + public static function withAttribute( $attributeName ) { |
|
| 27 | + return new static( Util::wrapValueWithIsEqual( $attributeName ) ); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param Matcher $attributeNameMatcher |
|
| 32 | - */ |
|
| 33 | - public function __construct( Matcher $attributeNameMatcher ) { |
|
| 34 | - parent::__construct(); |
|
| 30 | + /** |
|
| 31 | + * @param Matcher $attributeNameMatcher |
|
| 32 | + */ |
|
| 33 | + public function __construct( Matcher $attributeNameMatcher ) { |
|
| 34 | + parent::__construct(); |
|
| 35 | 35 | |
| 36 | - $this->attributeNameMatcher = $attributeNameMatcher; |
|
| 37 | - } |
|
| 36 | + $this->attributeNameMatcher = $attributeNameMatcher; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param Matcher|string $value |
|
| 41 | - * |
|
| 42 | - * @return AttributeMatcher |
|
| 43 | - */ |
|
| 44 | - public function havingValue( $value ) { |
|
| 45 | - // TODO: Throw exception if value is set |
|
| 46 | - $result = clone $this; |
|
| 47 | - $result->valueMatcher = Util::wrapValueWithIsEqual( $value ); |
|
| 39 | + /** |
|
| 40 | + * @param Matcher|string $value |
|
| 41 | + * |
|
| 42 | + * @return AttributeMatcher |
|
| 43 | + */ |
|
| 44 | + public function havingValue( $value ) { |
|
| 45 | + // TODO: Throw exception if value is set |
|
| 46 | + $result = clone $this; |
|
| 47 | + $result->valueMatcher = Util::wrapValueWithIsEqual( $value ); |
|
| 48 | 48 | |
| 49 | - return $result; |
|
| 50 | - } |
|
| 49 | + return $result; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function describeTo( Description $description ) { |
|
| 53 | - $description->appendText( 'with attribute ' ) |
|
| 54 | - ->appendDescriptionOf( $this->attributeNameMatcher ); |
|
| 55 | - if ( $this->valueMatcher ) { |
|
| 56 | - $description->appendText( ' having value ' ) |
|
| 57 | - ->appendDescriptionOf( $this->valueMatcher ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 52 | + public function describeTo( Description $description ) { |
|
| 53 | + $description->appendText( 'with attribute ' ) |
|
| 54 | + ->appendDescriptionOf( $this->attributeNameMatcher ); |
|
| 55 | + if ( $this->valueMatcher ) { |
|
| 56 | + $description->appendText( ' having value ' ) |
|
| 57 | + ->appendDescriptionOf( $this->valueMatcher ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param \DOMElement $item |
|
| 63 | - * @param Description $mismatchDescription |
|
| 64 | - * |
|
| 65 | - * @return bool |
|
| 66 | - */ |
|
| 67 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 68 | - /** @var \DOMAttr $attribute */ |
|
| 69 | - foreach ( $item->attributes as $attribute ) { |
|
| 70 | - if ( $this->valueMatcher ) { |
|
| 71 | - if ( |
|
| 72 | - $this->attributeNameMatcher->matches( $attribute->name ) |
|
| 73 | - && $this->valueMatcher->matches( $attribute->value ) |
|
| 74 | - ) { |
|
| 75 | - return true; |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - if ( $this->attributeNameMatcher->matches( $attribute->name ) ) { |
|
| 79 | - return true; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - } |
|
| 61 | + /** |
|
| 62 | + * @param \DOMElement $item |
|
| 63 | + * @param Description $mismatchDescription |
|
| 64 | + * |
|
| 65 | + * @return bool |
|
| 66 | + */ |
|
| 67 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 68 | + /** @var \DOMAttr $attribute */ |
|
| 69 | + foreach ( $item->attributes as $attribute ) { |
|
| 70 | + if ( $this->valueMatcher ) { |
|
| 71 | + if ( |
|
| 72 | + $this->attributeNameMatcher->matches( $attribute->name ) |
|
| 73 | + && $this->valueMatcher->matches( $attribute->value ) |
|
| 74 | + ) { |
|
| 75 | + return true; |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + if ( $this->attributeNameMatcher->matches( $attribute->name ) ) { |
|
| 79 | + return true; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - return false; |
|
| 85 | - } |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
@@ -8,46 +8,46 @@ |
||
| 8 | 8 | |
| 9 | 9 | class ClassMatcher extends TagMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $classMatcher; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @param Matcher|string $class |
|
| 18 | - * |
|
| 19 | - * @return self |
|
| 20 | - */ |
|
| 21 | - public static function withClass( $class ) { |
|
| 22 | - return new static( Util::wrapValueWithIsEqual( $class ) ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function __construct( Matcher $class ) { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->classMatcher = $class; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function describeTo( Description $description ) { |
|
| 31 | - $description->appendText( 'with class ' )->appendDescriptionOf( $this->classMatcher ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param \DOMElement $item |
|
| 36 | - * @param Description $mismatchDescription |
|
| 37 | - * |
|
| 38 | - * @return bool |
|
| 39 | - */ |
|
| 40 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 41 | - $classAttribute = $item->getAttribute( 'class' ); |
|
| 42 | - |
|
| 43 | - $classes = preg_split( '/\s+/u', $classAttribute ); |
|
| 44 | - foreach ( $classes as $class ) { |
|
| 45 | - if ( $this->classMatcher->matches( $class ) ) { |
|
| 46 | - return true; |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $classMatcher; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @param Matcher|string $class |
|
| 18 | + * |
|
| 19 | + * @return self |
|
| 20 | + */ |
|
| 21 | + public static function withClass( $class ) { |
|
| 22 | + return new static( Util::wrapValueWithIsEqual( $class ) ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function __construct( Matcher $class ) { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->classMatcher = $class; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function describeTo( Description $description ) { |
|
| 31 | + $description->appendText( 'with class ' )->appendDescriptionOf( $this->classMatcher ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param \DOMElement $item |
|
| 36 | + * @param Description $mismatchDescription |
|
| 37 | + * |
|
| 38 | + * @return bool |
|
| 39 | + */ |
|
| 40 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 41 | + $classAttribute = $item->getAttribute( 'class' ); |
|
| 42 | + |
|
| 43 | + $classes = preg_split( '/\s+/u', $classAttribute ); |
|
| 44 | + foreach ( $classes as $class ) { |
|
| 45 | + if ( $this->classMatcher->matches( $class ) ) { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | } |
@@ -10,214 +10,214 @@ |
||
| 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 | - if ( !@$document->loadHTML( $html ) ) { |
|
| 124 | - throw new \InvalidArgumentException( "There was some parsing error of `$html`" ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $errors = libxml_get_errors(); |
|
| 128 | - libxml_clear_errors(); |
|
| 129 | - libxml_use_internal_errors( $internalErrors ); |
|
| 130 | - |
|
| 131 | - /** @var \LibXMLError $error */ |
|
| 132 | - foreach ( $errors as $error ) { |
|
| 133 | - if ( $this->isUnknownTagError( $error ) ) { |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - throw new \InvalidArgumentException( |
|
| 138 | - 'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $document; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param \DOMDocument $document |
|
| 147 | - * |
|
| 148 | - * @return \DOMElement |
|
| 149 | - * @throws \InvalidArgumentException |
|
| 150 | - */ |
|
| 151 | - private function getSingleTagFromThe( \DOMDocument $document ) { |
|
| 152 | - $directChildren = iterator_to_array( $document->documentElement->childNodes ); |
|
| 153 | - |
|
| 154 | - $body = array_shift( $directChildren ); |
|
| 155 | - $directChildren = iterator_to_array( $body->childNodes ); |
|
| 156 | - |
|
| 157 | - if ( count( $directChildren ) !== 1 ) { |
|
| 158 | - throw new InvalidArgumentException( 'Expected exactly 1 tag description, got ' . count( $directChildren ) ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return $directChildren[0]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) { |
|
| 165 | - if ( $targetTag->childNodes->length > 0 ) { |
|
| 166 | - throw new InvalidArgumentException( 'Nested elements are not allowed' ); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $inputHtml |
|
| 172 | - * @param \DOMElement $targetTag |
|
| 173 | - * |
|
| 174 | - * @return AttributeMatcher[] |
|
| 175 | - */ |
|
| 176 | - private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) { |
|
| 177 | - $attributeMatchers = []; |
|
| 178 | - /** @var \DOMAttr $attribute */ |
|
| 179 | - foreach ( $targetTag->attributes as $attribute ) { |
|
| 180 | - if ( $attribute->name === 'class' ) { |
|
| 181 | - continue; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) ); |
|
| 185 | - if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) { |
|
| 186 | - $attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $attributeMatchers[] = $attributeMatcher; |
|
| 190 | - } |
|
| 191 | - return $attributeMatchers; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param \DOMElement $targetTag |
|
| 196 | - * |
|
| 197 | - * @return ClassMatcher[] |
|
| 198 | - */ |
|
| 199 | - private function createClassMatchers( \DOMElement $targetTag ) { |
|
| 200 | - $classMatchers = []; |
|
| 201 | - $classValue = $targetTag->getAttribute( 'class' ); |
|
| 202 | - foreach ( explode( ' ', $classValue ) as $expectedClass ) { |
|
| 203 | - if ( $expectedClass === '' ) { |
|
| 204 | - continue; |
|
| 205 | - } |
|
| 206 | - $classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) ); |
|
| 207 | - } |
|
| 208 | - return $classMatchers; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param \DOMElement $element |
|
| 213 | - * |
|
| 214 | - * @return string |
|
| 215 | - */ |
|
| 216 | - private function elementToString( \DOMElement $element ) { |
|
| 217 | - $newDocument = new \DOMDocument(); |
|
| 218 | - $cloned = $element->cloneNode( true ); |
|
| 219 | - $newDocument->appendChild( $newDocument->importNode( $cloned, true ) ); |
|
| 220 | - return trim( $newDocument->saveHTML() ); |
|
| 221 | - } |
|
| 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 | + if ( !@$document->loadHTML( $html ) ) { |
|
| 124 | + throw new \InvalidArgumentException( "There was some parsing error of `$html`" ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $errors = libxml_get_errors(); |
|
| 128 | + libxml_clear_errors(); |
|
| 129 | + libxml_use_internal_errors( $internalErrors ); |
|
| 130 | + |
|
| 131 | + /** @var \LibXMLError $error */ |
|
| 132 | + foreach ( $errors as $error ) { |
|
| 133 | + if ( $this->isUnknownTagError( $error ) ) { |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + throw new \InvalidArgumentException( |
|
| 138 | + 'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $document; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param \DOMDocument $document |
|
| 147 | + * |
|
| 148 | + * @return \DOMElement |
|
| 149 | + * @throws \InvalidArgumentException |
|
| 150 | + */ |
|
| 151 | + private function getSingleTagFromThe( \DOMDocument $document ) { |
|
| 152 | + $directChildren = iterator_to_array( $document->documentElement->childNodes ); |
|
| 153 | + |
|
| 154 | + $body = array_shift( $directChildren ); |
|
| 155 | + $directChildren = iterator_to_array( $body->childNodes ); |
|
| 156 | + |
|
| 157 | + if ( count( $directChildren ) !== 1 ) { |
|
| 158 | + throw new InvalidArgumentException( 'Expected exactly 1 tag description, got ' . count( $directChildren ) ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return $directChildren[0]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) { |
|
| 165 | + if ( $targetTag->childNodes->length > 0 ) { |
|
| 166 | + throw new InvalidArgumentException( 'Nested elements are not allowed' ); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $inputHtml |
|
| 172 | + * @param \DOMElement $targetTag |
|
| 173 | + * |
|
| 174 | + * @return AttributeMatcher[] |
|
| 175 | + */ |
|
| 176 | + private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) { |
|
| 177 | + $attributeMatchers = []; |
|
| 178 | + /** @var \DOMAttr $attribute */ |
|
| 179 | + foreach ( $targetTag->attributes as $attribute ) { |
|
| 180 | + if ( $attribute->name === 'class' ) { |
|
| 181 | + continue; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) ); |
|
| 185 | + if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) { |
|
| 186 | + $attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $attributeMatchers[] = $attributeMatcher; |
|
| 190 | + } |
|
| 191 | + return $attributeMatchers; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param \DOMElement $targetTag |
|
| 196 | + * |
|
| 197 | + * @return ClassMatcher[] |
|
| 198 | + */ |
|
| 199 | + private function createClassMatchers( \DOMElement $targetTag ) { |
|
| 200 | + $classMatchers = []; |
|
| 201 | + $classValue = $targetTag->getAttribute( 'class' ); |
|
| 202 | + foreach ( explode( ' ', $classValue ) as $expectedClass ) { |
|
| 203 | + if ( $expectedClass === '' ) { |
|
| 204 | + continue; |
|
| 205 | + } |
|
| 206 | + $classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) ); |
|
| 207 | + } |
|
| 208 | + return $classMatchers; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param \DOMElement $element |
|
| 213 | + * |
|
| 214 | + * @return string |
|
| 215 | + */ |
|
| 216 | + private function elementToString( \DOMElement $element ) { |
|
| 217 | + $newDocument = new \DOMDocument(); |
|
| 218 | + $cloned = $element->cloneNode( true ); |
|
| 219 | + $newDocument->appendChild( $newDocument->importNode( $cloned, true ) ); |
|
| 220 | + return trim( $newDocument->saveHTML() ); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | } |
@@ -8,40 +8,40 @@ |
||
| 8 | 8 | |
| 9 | 9 | class TagNameMatcher extends TagMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $tagNameMatcher; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @param Matcher|string $tagName |
|
| 18 | - * |
|
| 19 | - * @return self |
|
| 20 | - */ |
|
| 21 | - public static function withTagName( $tagName ) { |
|
| 22 | - return new static( Util::wrapValueWithIsEqual( $tagName ) ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function __construct( Matcher $tagNameMatcher ) { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->tagNameMatcher = $tagNameMatcher; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function describeTo( Description $description ) { |
|
| 31 | - $description->appendText( 'with tag name ' ) |
|
| 32 | - ->appendDescriptionOf( $this->tagNameMatcher ); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @param \DOMElement $item |
|
| 37 | - * @param Description $mismatchDescription |
|
| 38 | - * |
|
| 39 | - * @return bool |
|
| 40 | - */ |
|
| 41 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 42 | - $mismatchDescription->appendText( 'tag name ' ); |
|
| 43 | - $this->tagNameMatcher->describeMismatch( $item->tagName, $mismatchDescription ); |
|
| 44 | - return $this->tagNameMatcher->matches( $item->tagName ); |
|
| 45 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $tagNameMatcher; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @param Matcher|string $tagName |
|
| 18 | + * |
|
| 19 | + * @return self |
|
| 20 | + */ |
|
| 21 | + public static function withTagName( $tagName ) { |
|
| 22 | + return new static( Util::wrapValueWithIsEqual( $tagName ) ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function __construct( Matcher $tagNameMatcher ) { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->tagNameMatcher = $tagNameMatcher; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function describeTo( Description $description ) { |
|
| 31 | + $description->appendText( 'with tag name ' ) |
|
| 32 | + ->appendDescriptionOf( $this->tagNameMatcher ); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @param \DOMElement $item |
|
| 37 | + * @param Description $mismatchDescription |
|
| 38 | + * |
|
| 39 | + * @return bool |
|
| 40 | + */ |
|
| 41 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 42 | + $mismatchDescription->appendText( 'tag name ' ); |
|
| 43 | + $this->tagNameMatcher->describeMismatch( $item->tagName, $mismatchDescription ); |
|
| 44 | + return $this->tagNameMatcher->matches( $item->tagName ); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | } |
@@ -8,37 +8,37 @@ |
||
| 8 | 8 | |
| 9 | 9 | class TextContentsMatcher extends TagMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $matcher; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @param Matcher|string $text |
|
| 18 | - * |
|
| 19 | - * @return self |
|
| 20 | - */ |
|
| 21 | - public static function havingTextContents( $text ) { |
|
| 22 | - return new static( Util::wrapValueWithIsEqual( $text ) ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function __construct( Matcher $matcher ) { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->matcher = $matcher; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function describeTo( Description $description ) { |
|
| 31 | - $description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param \DOMElement $item |
|
| 36 | - * @param Description $mismatchDescription |
|
| 37 | - * |
|
| 38 | - * @return bool |
|
| 39 | - */ |
|
| 40 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 41 | - return $this->matcher->matches( $item->textContent ); |
|
| 42 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $matcher; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @param Matcher|string $text |
|
| 18 | + * |
|
| 19 | + * @return self |
|
| 20 | + */ |
|
| 21 | + public static function havingTextContents( $text ) { |
|
| 22 | + return new static( Util::wrapValueWithIsEqual( $text ) ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function __construct( Matcher $matcher ) { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->matcher = $matcher; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function describeTo( Description $description ) { |
|
| 31 | + $description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param \DOMElement $item |
|
| 36 | + * @param Description $mismatchDescription |
|
| 37 | + * |
|
| 38 | + * @return bool |
|
| 39 | + */ |
|
| 40 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 41 | + return $this->matcher->matches( $item->textContent ); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | } |