Skip to content

Commit

Permalink
Extended optimizations related to equality tests to inequality tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Mar 27, 2024
1 parent c7609e3 commit 134bffb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions docs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,10 @@ Quick (s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\Quick)
[x] Source with data set #22
[x] Source with data set #23
[x] Source with data set #24
[x] Source with data set #25
[x] Source with data set #26
[x] Source with data set #27
[x] Source with data set #28
[x] Renders plain text
[x] Renders multi-line text
[x] Renders rich text
Expand Down
2 changes: 1 addition & 1 deletion src/Bundles/MediaPack/Renderer.php

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/Configurator/RendererGenerators/PHP/Quick.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,16 @@ protected static function replacePHP(&$php)
// A comparison between an attribute and a literal string. Rather than unescape the
// attribute value, we escape the literal. This applies to comparisons using XPath's
// contains() as well (translated to PHP's strpos())
'(' . $getAttribute . '===(' . $string . '))s'
'(' . $getAttribute . '([!=]==)(' . $string . '))s'
=> function ($m)
{
return '$attributes[' . $m[1] . ']===' . htmlspecialchars($m[2], ENT_COMPAT);
return '$attributes[' . $m[1] . ']' . $m[2] . htmlspecialchars($m[3], ENT_COMPAT);
},

'((' . $string . ')===' . $getAttribute . ')s'
'((' . $string . ')([!=]==)' . $getAttribute . ')s'
=> function ($m)
{
return htmlspecialchars($m[1], ENT_COMPAT) . '===$attributes[' . $m[2] . ']';
return htmlspecialchars($m[1], ENT_COMPAT) . $m[2] . '$attributes[' . $m[3] . ']';
},

'(strpos\\(' . $getAttribute . ',(' . $string . ')\\)([!=]==(?:0|false)))s'
Expand Down Expand Up @@ -466,11 +466,11 @@ protected static function replacePHP(&$php)

// An attribute value used in an arithmetic comparison or operation does not need to be
// unescaped. The same applies to empty(), isset() and conditionals
'(' . $getAttribute . '(?=(?:==|[-+*])\\d+))' => '$attributes[$1]',
'(\\b(\\d+(?:==|[-+*]))' . $getAttribute . ')' => '$1$attributes[$2]',
'(empty\\(' . $getAttribute . '\\))' => 'empty($attributes[$1])',
"(\\\$node->hasAttribute\\(('[^']+')\\))" => 'isset($attributes[$1])',
'if($node->attributes->length)' => 'if($this->hasNonNullValues($attributes))',
'(' . $getAttribute . '(?=(?:[!=]=|[-+*])\\d+))' => '$attributes[$1]',
'(\\b(\\d+(?:[!=]=|[-+*]))' . $getAttribute . ')' => '$1$attributes[$2]',
'(empty\\(' . $getAttribute . '\\))' => 'empty($attributes[$1])',
"(\\\$node->hasAttribute\\(('[^']+')\\))" => 'isset($attributes[$1])',
'if($node->attributes->length)' => 'if($this->hasNonNullValues($attributes))',

// In all other situations, unescape the attribute value before use
'(' . $getAttribute . ')' => 'htmlspecialchars_decode($attributes[$1]??\'\')'
Expand Down
16 changes: 16 additions & 0 deletions tests/Configurator/RendererGenerators/PHP/QuickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,18 @@ public static function getSourceTests()
['X' => '<xsl:if test="@x=1">x</xsl:if>'],
"if(\$attributes['x']==1){\$html.='x';}"
],
[
['X' => '<xsl:if test="@x!=1">x</xsl:if>'],
"if(\$attributes['x']!=1){\$html.='x';}"
],
[
['X' => '<xsl:if test="1=@x">x</xsl:if>'],
"if(1==\$attributes['x']){\$html.='x';}"
],
[
['X' => '<xsl:if test="1!=@x">x</xsl:if>'],
"if(1!=\$attributes['x']){\$html.='x';}"
],
[
['X' => '<hr title="{@x+200*@y}"/>'],
"\$attributes['x']+200*\$attributes['y']"
Expand Down Expand Up @@ -785,6 +793,14 @@ public static function getSourceTests()
['X' => '<xsl:if test="@*">Y</xsl:if>'],
'if($this->hasNonNullValues($attributes)){$html.=\'Y\';}'
],
[
['X' => '<xsl:if test="string(@foo) != \'\'">X</xsl:if>'],
"if(\$attributes['foo']!==''){\$html.='X';}"
],
[
['X' => '<xsl:if test="\'\'!=@foo">X</xsl:if>'],
"if(''!==\$attributes['foo']){\$html.='X';}"
],
];
}
}

0 comments on commit 134bffb

Please sign in to comment.