Skip to content

Commit 0315dc3

Browse files
authored
Fix theme inheritance when not inheriting from blank (#3663)
* fix #3662 * add unit tests * update documentation, improve debugging output
1 parent 70a094e commit 0315dc3

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

R/theme.r

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ add_theme <- function(t1, t2, t2name) {
525525
#' @param element The name of the theme element to calculate
526526
#' @param theme A theme object (like [theme_grey()])
527527
#' @param verbose If TRUE, print out which elements this one inherits from
528+
#' @param skip_blank If TRUE, elements of type `element_blank` in the
529+
#' inheritance hierarchy will be ignored.
528530
#' @keywords internal
529531
#' @export
530532
#' @examples
@@ -540,15 +542,20 @@ add_theme <- function(t1, t2, t2name) {
540542
#' t$axis.text.x
541543
#' t$axis.text
542544
#' t$text
543-
calc_element <- function(element, theme, verbose = FALSE) {
545+
calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE) {
544546
if (verbose) message(element, " --> ", appendLF = FALSE)
545547

546548
el_out <- theme[[element]]
547549

548-
# If result is element_blank, don't inherit anything from parents
550+
# If result is element_blank, we skip it if `skip_blank` is `TRUE`,
551+
# and otherwise we don't inherit anything from parents
549552
if (inherits(el_out, "element_blank")) {
550-
if (verbose) message("element_blank (no inheritance)")
551-
return(el_out)
553+
if (isTRUE(skip_blank)) {
554+
el_out <- NULL
555+
} else {
556+
if (verbose) message("element_blank (no inheritance)")
557+
return(el_out)
558+
}
552559
}
553560

554561
# Obtain the element tree and check that the element is in it
@@ -592,7 +599,16 @@ calc_element <- function(element, theme, verbose = FALSE) {
592599

593600
# Calculate the parent objects' inheritance
594601
if (verbose) message(paste(pnames, collapse = ", "))
595-
parents <- lapply(pnames, calc_element, theme, verbose)
602+
parents <- lapply(
603+
pnames,
604+
calc_element,
605+
theme,
606+
verbose = verbose,
607+
# once we've started skipping blanks, we continue doing so until the end of the
608+
# recursion; we initiate skipping blanks if we encounter an element that
609+
# doesn't inherit blank.
610+
skip_blank = skip_blank || (!is.null(el_out) && !isTRUE(el_out$inherit.blank))
611+
)
596612

597613
# Combine the properties of this element with all parents
598614
Reduce(combine_elements, parents, el_out)

man/calc_element.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/figs/deps.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
- vdiffr-svg-engine: 1.0
2-
- vdiffr: 0.3.1
2+
- vdiffr: 0.3.0
33
- freetypeharfbuzz: 0.2.5

tests/figs/themes/axes-styling.svg

Lines changed: 4 additions & 4 deletions
Loading

tests/testthat/test-theme.r

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ test_that("calculating theme element inheritance works", {
154154
inherit.blank = TRUE # this is true because we're requesting a complete theme
155155
), class = c("element_dummyrect", "element_rect", "element"))
156156
)
157+
158+
# Check that blank elements are skipped in inheritance tree if and only if elements
159+
# don't inherit from blank.
160+
t <- theme_gray() +
161+
theme(
162+
strip.text = element_blank(),
163+
strip.text.x = element_text() # inherit.blank = FALSE is default
164+
)
165+
e1 <- calc_element("strip.text.x", t)
166+
e2 <- calc_element("text", t)
167+
e2$inherit.blank <- FALSE # b/c inherit.blank = TRUE for complete themes
168+
expect_identical(e1, e2)
169+
170+
theme <- theme_gray() +
171+
theme(strip.text = element_blank(), strip.text.x = element_text(inherit.blank = TRUE))
172+
e1 <- ggplot2:::calc_element("strip.text.x", theme)
173+
e2 <- ggplot2:::calc_element("strip.text", theme)
174+
expect_identical(e1, e2)
157175
})
158176

159177
test_that("complete and non-complete themes interact correctly with each other", {

0 commit comments

Comments
 (0)