Skip to content
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

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

Merged
merged 4 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/HTMLPurifier/UnitConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private function div($s1, $s2, $scale)
*/
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
$neg = $n < 0 ? '-' : ''; // Negative sign
if ($this->bcmath) {
Expand All @@ -276,7 +276,7 @@ private function round($n, $sigfigs)
}
return $n;
} else {
return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
return $this->scale(round((float)$n, $sigfigs - $new_log - 1), $rp + 1);
}
}

Expand All @@ -300,7 +300,7 @@ private function scale($r, $scale)
// Now we return it, truncating the zero that was rounded off.
return substr($precise, 0, -1) . str_repeat('0', -$scale + 1);
}
return sprintf('%.' . $scale . 'f', (float)$r);
return number_format((float)$r, $scale, '.', '');
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/HTMLPurifier/UnitConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public function testRoundingUserPrecision()
$this->assertConversion('111.12pt', '1.5433in');
$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()
{
Expand Down