Skip to content

Commit

Permalink
Merge 9a68d30 into 89e15f7
Browse files Browse the repository at this point in the history
  • Loading branch information
thiemowmde committed Jul 5, 2017
2 parents 89e15f7 + 9a68d30 commit 2d2e04f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Empty file added phpcs.xml
Empty file.
20 changes: 8 additions & 12 deletions src/HtmlMatcher.php
Expand Up @@ -14,8 +14,6 @@ class HtmlMatcher extends DiagnosingMatcher
*/
const XML_UNKNOWN_TAG_ERROR_CODE = 801;

const SCRIPT_BODY_REPLACEMENT = 'Contents were removed by HtmlMatcher';

/**
* @var Matcher
*/
Expand Down Expand Up @@ -49,7 +47,7 @@ protected function matchesWithDiagnosticDescription($html, Description $mismatch
$internalErrors = libxml_use_internal_errors(true);
$document = new \DOMDocument();

$html = $this->stripScriptsContents($html);
$html = $this->escapeScriptTagContents($html);

if (!@$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
$mismatchDescription->appendText('there was some parsing error');
Expand Down Expand Up @@ -96,16 +94,14 @@ private function isUnknownTagError(\LibXMLError $error)

/**
* @param string $html
* @return string
*
* @return string HTML
*/
private function stripScriptsContents($html)
private function escapeScriptTagContents($html)
{
preg_match_all("#(<script.*>).*</script>#sU", $html, $scripts);
foreach ($scripts[0] as $index => $script) {
$openTag = $scripts[1][$index];
$replacement = $openTag . self::SCRIPT_BODY_REPLACEMENT . '</script>';
$html = str_replace($script, $replacement, $html);
}
return $html;
return preg_replace_callback('#(<script.*>)(.*)(</script>)#isU', function ($matches)
{
return $matches[1] . str_replace('</', '<\/', $matches[2]) . $matches[3];
}, $html);
}
}
2 changes: 1 addition & 1 deletion tests/HtmlMatcherTest.php
Expand Up @@ -85,7 +85,7 @@ public function addsSpecificTextInsideTheSciptTagsInsteadOfItsContents()

assertThat($html, is(htmlPiece(havingChild(
both(withTagName('script'))
->andAlso(havingTextContents(HtmlMatcher::SCRIPT_BODY_REPLACEMENT))))));
->andAlso(havingTextContents("\n\t<span><\\/span>\n"))))));
}

/**
Expand Down

0 comments on commit 2d2e04f

Please sign in to comment.