Skip to content

Commit

Permalink
XPathHelper: ignore malformed expression in minify()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jan 22, 2020
1 parent aec2d9f commit ec72f4f
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 39 deletions.
3 changes: 0 additions & 3 deletions docs/testdox.txt
Expand Up @@ -1062,8 +1062,6 @@ XPath Helper (s9e\TextFormatter\Tests\Configurator\Helpers\XPathHelper)
[x] minify() tests with data set #25
[x] minify() tests with data set #26
[x] minify() tests with data set #27
[x] minify() tests with data set #28
[x] minify() tests with data set #29
[x] Parse equality expr with data set #0
[x] Parse equality expr with data set #1
[x] Parse equality expr with data set #2
Expand Down Expand Up @@ -1900,7 +1898,6 @@ Minify Inline CSS (s9e\TextFormatter\Tests\Configurator\TemplateNormalizations\M
[x] Works with data set #8

Minify XPath Expressions (s9e\TextFormatter\Tests\Configurator\TemplateNormalizations\MinifyXPathExpressions)
[x] Throws an exception if a string isn't properly closed
[x] Works with data set #0
[x] Works with data set #1
[x] Works with data set #2
Expand Down
20 changes: 3 additions & 17 deletions src/Configurator/Helpers/XPathHelper.php
Expand Up @@ -119,12 +119,6 @@ public static function minify($expr)
return $expr;
}

preg_match_all('("[^"]*+"|\'[^\']*+\'|[\'"](*:X))', $expr, $m);
if (!empty($m['MARK']))
{
throw new RuntimeException("Cannot parse XPath expression '" . $expr . "'");
}

// Temporarily encode the content of literal strings
$expr = self::encodeStrings(trim($expr));

Expand Down Expand Up @@ -174,19 +168,11 @@ protected static function removeRedundantParentheses(string $expr): string
{
if ($token === '(')
{
$left[$depth] = $k;
++$depth;
$left[$depth++] = $k;
}
elseif ($token === ')')
elseif ($token === ')' && --$depth >= 0 && $tokens[$k + 1] === ')' && $left[$depth - 1] === $left[$depth] - 1)
{
if (--$depth < 0)
{
throw new RuntimeException("Cannot parse XPath expression '" . self::decodeStrings($expr) . "'");
}
if ($tokens[$k + 1] === ')' && $left[$depth - 1] === $left[$depth] - 1)
{
unset($tokens[$k], $tokens[$left[$depth]]);
}
unset($tokens[$k], $tokens[$left[$depth]]);
}
}

Expand Down
8 changes: 0 additions & 8 deletions tests/Configurator/Helpers/XPathHelperTest.php
Expand Up @@ -158,10 +158,6 @@ public function getMinifyTests()
' foo or _bar ',
'foo or_bar'
],
[
'foo = "bar',
new RuntimeException("Cannot parse XPath expression 'foo = \"bar'")
],
[
'100 * (315 + 30) div 560',
'100*(315+30)div560'
Expand Down Expand Up @@ -226,10 +222,6 @@ public function getMinifyTests()
'true() and true()',
'true()andtrue()'
],
[
'"1")',
new RuntimeException('Cannot parse XPath expression \'"1")\'')
],
[
'(1+(1+1))',
'1+(1+1)'
Expand Down
Expand Up @@ -7,17 +7,6 @@
*/
class MinifyXPathExpressionsTest extends AbstractTest
{
/**
* @testdox Throws an exception if a string isn't properly closed
*/
public function testInvalidXPath()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage("Cannot parse XPath expression 'foo = \"bar'");

$this->test('<xsl:if test="foo = &quot;bar">!</xsl:if>', null);
}

public function getData()
{
return [
Expand Down

0 comments on commit ec72f4f

Please sign in to comment.