From 2226b72d1cfe15696819e677e5f2e5f99cffefbf Mon Sep 17 00:00:00 2001 From: d-buchmann Date: Sun, 6 Jul 2025 00:20:29 +0200 Subject: [PATCH] Update AbstractParameter.php (#959) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update AbstractParameter.php Make lazy null conditionals explicit. Try to handle LaTeX special chars gracefully. Fixes #958 * Only escape the percentage sign, so that you can still use latex for units * Only escape previously unescaped percentage signs * simplify regex * Render the percentage sign correctly in units in the frontend. --------- Co-authored-by: Jan Böhmer --- .../controllers/pages/latex_preview_controller.js | 5 ++++- .../pages/parameters_autocomplete_controller.js | 4 +++- src/Entity/Parameters/AbstractParameter.php | 15 +++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/assets/controllers/pages/latex_preview_controller.js b/assets/controllers/pages/latex_preview_controller.js index 6113393a..7f1e611c 100644 --- a/assets/controllers/pages/latex_preview_controller.js +++ b/assets/controllers/pages/latex_preview_controller.js @@ -33,7 +33,10 @@ export default class extends Controller { { let value = ""; if (this.unitValue) { - value = "\\mathrm{" + this.inputTarget.value + "}"; + //Escape percentage signs + value = this.inputTarget.value.replace(/%/g, '\\%'); + + value = "\\mathrm{" + value + "}"; } else { value = this.inputTarget.value; } diff --git a/assets/controllers/pages/parameters_autocomplete_controller.js b/assets/controllers/pages/parameters_autocomplete_controller.js index cd82875a..e187aa42 100644 --- a/assets/controllers/pages/parameters_autocomplete_controller.js +++ b/assets/controllers/pages/parameters_autocomplete_controller.js @@ -85,7 +85,9 @@ export default class extends Controller tmp += '' + katex.renderToString(data.symbol) + '' } if (data.unit) { - tmp += '' + katex.renderToString('[' + data.unit + ']') + '' + let unit = data.unit.replace(/%/g, '\\%'); + unit = "\\mathrm{" + unit + "}"; + tmp += '' + katex.renderToString('[' + unit + ']') + '' } diff --git a/src/Entity/Parameters/AbstractParameter.php b/src/Entity/Parameters/AbstractParameter.php index edcedc3e..39f333da 100644 --- a/src/Entity/Parameters/AbstractParameter.php +++ b/src/Entity/Parameters/AbstractParameter.php @@ -217,7 +217,7 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu $str = ''; $bracket_opened = false; - if ($this->value_typical) { + if ($this->value_typical !== null) { $str .= $this->getValueTypicalWithUnit($latex_formatted); if ($this->value_min || $this->value_max) { $bracket_opened = true; @@ -225,11 +225,11 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu } } - if ($this->value_max && $this->value_min) { + if ($this->value_max !== null && $this->value_min !== null) { $str .= $this->getValueMinWithUnit($latex_formatted).' ... '.$this->getValueMaxWithUnit($latex_formatted); - } elseif ($this->value_max) { + } elseif ($this->value_max !== null) { $str .= 'max. '.$this->getValueMaxWithUnit($latex_formatted); - } elseif ($this->value_min) { + } elseif ($this->value_min !== null) { $str .= 'min. '.$this->getValueMinWithUnit($latex_formatted); } @@ -449,7 +449,10 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu if (!$with_latex) { $unit = $this->unit; } else { - $unit = '$\mathrm{'.$this->unit.'}$'; + //Escape the percentage sign for convenience (as latex uses it as comment and it is often used in units) + $escaped = preg_replace('/\\\\?%/', "\\\\%", $this->unit); + + $unit = '$\mathrm{'.$escaped.'}$'; } return $str.' '.$unit; @@ -457,7 +460,7 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu return $str; } - + /** * Returns the class of the element that is allowed to be associated with this attachment. * @return string