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