From b3a7d409301be54c64d572450c9bbe14f69336cb Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 18 Oct 2026 12:49:03 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20(locale):=20Lc=20escapa=20?= =?UTF-8?q?argumentos=20s=C3=B3lo=20si=20hace=20falta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `using()` copiaba siempre cada argumento de texto al escaparlo. Ahora sólo lo hace si contiene algún carácter que Maud escapa; si no, se pasa prestado. --- src/locale/lc.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/locale/lc.rs b/src/locale/lc.rs index 94ee99fb..59e3fa46 100644 --- a/src/locale/lc.rs +++ b/src/locale/lc.rs @@ -47,7 +47,7 @@ impl LcArg { // Valor para Fluent. Con `escape`, el texto se escapa para insertarse en marcado HTML. fn to_fluent(&self, escape: bool) -> FluentValue<'_> { match self { - LcArg::Text(s) if escape => s.as_ref().render().into_string().into(), + LcArg::Text(s) if escape && needs_escape(s) => s.as_ref().render().into_string().into(), LcArg::Text(s) => s.as_ref().into(), LcArg::Number(n) => (*n).into(), LcArg::Markup(s) => s.as_str().into(), @@ -55,6 +55,12 @@ impl LcArg { } } +// Indica si el texto contiene algún carácter que Maud escapa. Sin ninguno, el texto escapado es +// idéntico al original y se interpola prestado, sin reservar memoria para una copia. +fn needs_escape(text: &str) -> bool { + text.bytes().any(|b| matches!(b, b'&' | b'<' | b'>' | b'"')) +} + // **< Lc >***************************************************************************************** /// Crea instancias para traducir *textos localizados*. @@ -412,3 +418,19 @@ impl Lc { key } } + +#[cfg(test)] +mod tests { + use super::*; + + // `needs_escape()` must flag every character that Maud escapes; otherwise `using()` would + // interpolate it unescaped. Compares every character up to U+02FF with Maud's own escaping. + #[test] + fn needs_escape_matches_maud() { + for c in (0..=0x2ffu32).filter_map(char::from_u32) { + let text = c.to_string(); + let escaped = text.as_str().render().into_string(); + assert_eq!(needs_escape(&text), escaped != text, "mismatch for {c:?}"); + } + } +}