0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 15:28:40 +00:00

fix: Support for locales using decimal separators other than . (dot) (#372)

* Bugfix UnitConverter expects float got string (strict types enabled)

* Bugfix for latest bugfix with huge numbers

* Bugfix for german locale

* Use number_format instead of str_replace(sprintf())
This commit is contained in:
cracksalad 2023-04-30 15:30:23 +02:00 committed by GitHub
parent c05639e0c9
commit 43f49ac9a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -261,7 +261,7 @@ class HTMLPurifier_UnitConverter
*/ */
private function round($n, $sigfigs) private function round($n, $sigfigs)
{ {
$new_log = (int)floor(log(abs($n), 10)); // Number of digits left of decimal - 1 $new_log = (int)floor(log(abs((float)$n), 10)); // Number of digits left of decimal - 1
$rp = $sigfigs - $new_log - 1; // Number of decimal places needed $rp = $sigfigs - $new_log - 1; // Number of decimal places needed
$neg = $n < 0 ? '-' : ''; // Negative sign $neg = $n < 0 ? '-' : ''; // Negative sign
if ($this->bcmath) { if ($this->bcmath) {
@ -276,7 +276,7 @@ class HTMLPurifier_UnitConverter
} }
return $n; return $n;
} else { } else {
return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1); return $this->scale(round((float)$n, $sigfigs - $new_log - 1), $rp + 1);
} }
} }
@ -300,7 +300,7 @@ class HTMLPurifier_UnitConverter
// Now we return it, truncating the zero that was rounded off. // Now we return it, truncating the zero that was rounded off.
return substr($precise, 0, -1) . str_repeat('0', -$scale + 1); return substr($precise, 0, -1) . str_repeat('0', -$scale + 1);
} }
return sprintf('%.' . $scale . 'f', (float)$r); return number_format((float)$r, $scale, '.', '');
} }
} }

View File

@ -102,6 +102,13 @@ class HTMLPurifier_UnitConverterTest extends HTMLPurifier_Harness
$this->assertConversion('11.112pt', '0.15433in'); $this->assertConversion('11.112pt', '0.15433in');
} }
public function testDecimalSeparatorComma()
{
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
$this->assertConversion('11.11px', '0.294cm');
setlocale(LC_ALL, '');
}
public function testRoundingBigNumber() public function testRoundingBigNumber()
{ {
$this->assertConversion('444400000000000000000000in', '42660000000000000000000000px'); $this->assertConversion('444400000000000000000000in', '42660000000000000000000000px');