Skip to content

Commit

Permalink
Remove unsuported character by ggtext in Labels (#482)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Felixmil authored Aug 18, 2023
1 parent eec205f commit d206f85
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion R/label.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions R/utilities-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 14 additions & 5 deletions tests/testthat/test-font-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>
plotConfiguration = PlotConfiguration$new(
title = "**This title should be bold**",
subtitle = "*This subtitle should be italic*",
caption = "This caption has style<br>
<span style = 'font-size:10pt;'>because it uses <span style = 'color:#0072B2;'><b>HTML</b></span> instead of
<span style = 'color:#D55E00;'><i>Markdown syntax</i></span></span>")
)
)
)
})

0 comments on commit d206f85

Please sign in to comment.