-
Notifications
You must be signed in to change notification settings - Fork 8.1k
ext/dom: Return numeric results from XPath callbacks as numbers #22105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
afflerbach
wants to merge
1
commit into
php:master
Choose a base branch
from
afflerbach:xpath-callback-return-numbers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
ext/dom/tests/modern/xml/return_numeric_strings_from_xpath.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --TEST-- | ||
| Returning numeric string values from Dom\XPath callback functions for large integers | ||
| --EXTENSIONS-- | ||
| dom | ||
| --SKIPIF-- | ||
| <?php if (PHP_INT_SIZE !== 8) die("skip 64-bit only"); ?> | ||
| --FILE-- | ||
| <?php | ||
| define('MAX_SAFE_INTEGER', 2**53-1); | ||
|
|
||
| $document = Dom\XMLDocument::createFromString('<root/>'); | ||
| $xpath = new Dom\XPath($document); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-int', fn(int $i): int => $i); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-float', fn(float $f): float => $f); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-max-safe-int', fn() => MAX_SAFE_INTEGER); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-negative-max-safe-int', fn() => -MAX_SAFE_INTEGER); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-large-int', fn() => MAX_SAFE_INTEGER+2); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-negative-large-int', fn() => -MAX_SAFE_INTEGER-2); | ||
| $xpath->registerNamespace('x', 'urn:x'); | ||
|
|
||
| var_dump($xpath->evaluate('x:get-float('.PHP_INT_MAX.')')); | ||
| var_dump($xpath->evaluate('x:get-float('.PHP_INT_MIN.')')); | ||
| var_dump($xpath->evaluate('x:get-float('.PHP_FLOAT_MAX.')')); | ||
| var_dump($xpath->evaluate('x:get-float('.PHP_FLOAT_MIN.')')); | ||
| var_dump($xpath->evaluate('x:get-float('.PHP_FLOAT_EPSILON.')')); | ||
|
|
||
| var_dump(MAX_SAFE_INTEGER); | ||
| var_dump(floatval(MAX_SAFE_INTEGER)); | ||
| var_dump($xpath->evaluate("x:get-int(".(MAX_SAFE_INTEGER).")")); | ||
| var_dump($xpath->evaluate("x:get-int(".(-MAX_SAFE_INTEGER).")")); | ||
| var_dump($xpath->evaluate("x:get-max-safe-int()")); | ||
| var_dump($xpath->evaluate("x:get-negative-max-safe-int()")); | ||
|
|
||
| var_dump(MAX_SAFE_INTEGER+2); | ||
| // loses precision while type casting | ||
| var_dump(floatval(MAX_SAFE_INTEGER+2)); | ||
| // loses precision while parameter passing int -> XPath number | ||
| var_dump($xpath->evaluate("x:get-int(".(MAX_SAFE_INTEGER+2).")")); | ||
| var_dump($xpath->evaluate("x:get-int(".(-MAX_SAFE_INTEGER-2).")")); | ||
| // returns string values for integers larger than 2^53-1 to maintain precision | ||
| var_dump($xpath->evaluate("x:get-large-int()")); | ||
| var_dump($xpath->evaluate("x:get-negative-large-int()")); | ||
| ?> | ||
| --EXPECT-- | ||
| float(9.223372036854778E+18) | ||
| float(-9.223372036854778E+18) | ||
| float(1.7976931348623E+308) | ||
| float(2.2250738585072E-308) | ||
| float(2.2204460492503003E-16) | ||
| int(9007199254740991) | ||
| float(9007199254740991) | ||
| float(9007199254740991) | ||
| float(-9007199254740991) | ||
| float(9007199254740991) | ||
| float(-9007199254740991) | ||
| int(9007199254740993) | ||
| float(9007199254740992) | ||
| string(16) "9007199254740992" | ||
| string(17) "-9007199254740992" | ||
| string(16) "9007199254740993" | ||
| string(17) "-9007199254740993" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| --TEST-- | ||
| Returning scalar values from Dom\XPath callback functions | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
| $document = Dom\XMLDocument::createFromString('<root/>'); | ||
| $xpath = new Dom\XPath($document); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-int', fn(int $i): int => $i); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-float', fn(float $f): float => $f); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-string', fn(string $s): string => $s); | ||
| $xpath->registerPhpFunctionNs('urn:x', 'get-bool', fn(bool $b): bool => $b); | ||
| $xpath->registerNamespace('x', 'urn:x'); | ||
|
|
||
| var_dump($xpath->evaluate('x:get-string("test")')); | ||
| var_dump($xpath->evaluate('x:get-bool(1)')); | ||
| var_dump($xpath->evaluate('x:get-bool(0)')); | ||
|
|
||
| var_dump($xpath->evaluate('x:get-int(41)')); | ||
| var_dump($xpath->evaluate('x:get-int(41) + 1')); | ||
| var_dump($xpath->evaluate('x:get-int(41)') + 1); | ||
| var_dump($xpath->evaluate('x:get-float(4.2)')); | ||
| var_dump($xpath->evaluate('2 * x:get-float(4.2)')); | ||
| var_dump(2 * $xpath->evaluate('x:get-float(4.2)')); | ||
|
|
||
| var_dump($xpath->evaluate('x:get-int(0)')); | ||
| var_dump($xpath->evaluate('x:get-int(-0)')); | ||
| var_dump($xpath->evaluate('x:get-float(0)')); | ||
| var_dump($xpath->evaluate('x:get-float(-0)')); | ||
|
|
||
| var_dump($xpath->evaluate('x:get-float(number("invalid"))')); | ||
| var_dump($xpath->evaluate('x:get-float(1 div 0)')); | ||
| ?> | ||
| --EXPECT-- | ||
| string(4) "test" | ||
| bool(true) | ||
| bool(false) | ||
| float(41) | ||
| float(42) | ||
| float(42) | ||
| float(4.2) | ||
| float(8.4) | ||
| float(8.4) | ||
| float(0) | ||
| float(0) | ||
| float(0) | ||
| float(-0) | ||
| float(NAN) | ||
| float(INF) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
ext/xsl/tests/xsltprocessor_registerPHPFunctionNS-numeric-results.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --TEST-- | ||
| Returning numeric values from XSLTProcessor callbacks | ||
| --EXTENSIONS-- | ||
| xsl | ||
| --FILE-- | ||
| <?php | ||
| $document = Dom\XMLDocument::createFromString('<root><a/><b/><c/><d/></root>'); | ||
| $xslt =<<<END | ||
| <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | ||
| version="1.0" xmlns:my="urn:my.ns"> | ||
| <xsl:output method="text" omit-xml-declaration="yes"/> | ||
| <xsl:template match="/root">count: <xsl:value-of select="my:count(*)"/></xsl:template> | ||
| </xsl:stylesheet> | ||
| END; | ||
| $stylesheet = Dom\XMLDocument::createFromString($xslt); | ||
| $xsltProcessor = new XSLTProcessor; | ||
| $xsltProcessor->importStylesheet($stylesheet); | ||
| $xsltProcessor->registerPHPFunctionNS('urn:my.ns', 'count', fn(array $arg1) => count($arg1)); | ||
| var_dump($xsltProcessor->transformToXML($document)); | ||
| ?> | ||
| --EXPECT-- | ||
| string(8) "count: 4" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a BC break thus need a good UPGRADING entry.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will break for numbers >
2**52on 64-bit systems. Previously, it was represented as a string and could be coerced back to a phpintwithout precision loss. Doubles can't reliably store integers >2**52so you would get precision loss.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what tests are for ;)