From d206f8519891df0e3717c91aa4e796903812e3d0 Mon Sep 17 00:00:00 2001
From: Felix MIL <34234913+Felixmil@users.noreply.github.com>
Date: Fri, 18 Aug 2023 09:48:09 +0200
Subject: [PATCH] Remove unsuported character by ggtext in Labels (#482)
* Remove unsuported character by ggtext in Labels
+ add utilitary function
+ add unit test
* Prepend function name with .
* Add NEWS entry
+ add for informative function description
---
NEWS.md | 1 +
R/label.R | 4 +++-
R/utilities-label.R | 20 ++++++++++++++++++++
tests/testthat/test-font-label.R | 19 ++++++++++++++-----
4 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/NEWS.md b/NEWS.md
index 8f59550b..aa3684e4 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -6,6 +6,7 @@
- New `xValuesLimits` and `yValuesLimits` argument in `PlotConfiguration` to filter the **data** used to generate the plot. (see `ggplot2::scale_continuous_x`).
- Groups names are now wraped on several lines if their number of characters is longer than 60.
- Plots and plotGrids labels (titles, subtitles, caption and axis labels) are now automatically fitting plot's width and wraped on several lines if too long.
+- Plots labels texts are now sanitized from any unsupported characters.
# tlf 1.5.0
diff --git a/R/label.R b/R/label.R
index e78cef07..c93a6d75 100644
--- a/R/label.R
+++ b/R/label.R
@@ -30,7 +30,9 @@ Label <- R6::R6Class(
validateIsIncluded(fontFace, FontFaces, nullAllowed = TRUE)
validateIsIncluded(align, Alignments, nullAllowed = TRUE)
- self$text <- text
+
+ self$text <- .sanitizeLabel(text)
+
self$font <- font %||% Font$new()
# If font properties are explicitely written, they will overwrite the properties of input Font
eval(.parseVariableToObject("self$font", c("size", "color", "fontFace", "fontFamily", "angle", "align", "maxWidth"), keepIfNull = TRUE))
diff --git a/R/utilities-label.R b/R/utilities-label.R
index 4899cd3a..0c586b66 100644
--- a/R/utilities-label.R
+++ b/R/utilities-label.R
@@ -94,3 +94,23 @@ getLabelWithUnit <- function(label, unit = NULL) {
return(paste0(label, " [", unit, "]"))
}
}
+
+#' Sanitize Label Text
+#' @description
+#' ggtext does not allow certain characters that can be converted to html
+#' tags but that are not supported. This function removes this forbidden
+#' characters.
+#'
+#' @param text a character string
+#'
+#' @return a sanitized character string
+#'
+#' @examples
+#' .sanitizeLabel("`code`")
+.sanitizeLabel <- function(text){
+ forbiddenCharacters <- c("`")
+ if (isOfType(text, "character", nullAllowed = FALSE)) {
+ text <- stringr::str_remove_all(text, paste(forbiddenCharacters, sep = "|"))
+ }
+ return(text)
+}
diff --git a/tests/testthat/test-font-label.R b/tests/testthat/test-font-label.R
index b4d9ac8e..9cbd944d 100644
--- a/tests/testthat/test-font-label.R
+++ b/tests/testthat/test-font-label.R
@@ -90,17 +90,26 @@ test_that("Long texts are properly handled in labels",{
})
+test_that("Tags that are not supported by ggtext are removed", {
+ expect_no_error(
+ initializePlot(
+ plotConfiguration = PlotConfiguration$new(
+ title = "this title should work even though it has `backticks`")
+ )
+ )
+})
+
test_that("Markdown syntax is not supported because Label and Font overwrite its with the FontFace argument",{
# This test should break when the issue is fixed.
vdiffr::expect_doppelganger(
"Plot with Markdown",
fig = initializePlot(
- plotConfiguration = PlotConfiguration$new(
- title = "**This title should be bold**",
- subtitle = "*This subtitle should be italic*",
- caption = "This caption has style
+ plotConfiguration = PlotConfiguration$new(
+ title = "**This title should be bold**",
+ subtitle = "*This subtitle should be italic*",
+ caption = "This caption has style
because it uses HTML instead of
Markdown syntax")
- )
+ )
)
})