Skip to content

Commit f7a18db

Browse files
committed
defend against insanity
1 parent 8433e1a commit f7a18db

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

R/theme-elements.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,13 @@ S7::method(draw_element, element_text) <-
492492
hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
493493
margin = NULL, margin_x = FALSE, margin_y = FALSE, ...) {
494494

495-
if (is.null(label))
495+
if (is.null(label)) {
496496
return(zeroGrob())
497+
}
498+
if (is_theme_element(label)) {
499+
cli::cli_warn("{.arg label} cannot be {.obj_type_friendly {label}}.")
500+
return(zeroGrob())
501+
}
497502

498503
vj <- vjust %||% element@vjust
499504
hj <- hjust %||% element@hjust

tests/testthat/_snaps/theme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@
115115
! <ggplot2::element_text> object properties are invalid:
116116
- @colour cannot be a decimal number, but could be an integer.
117117

118+
---
119+
120+
Code
121+
element_grob(el, label = element_blank())
122+
Condition
123+
Warning:
124+
`label` cannot be a <ggplot2::element_blank> object.
125+
Output
126+
zeroGrob[NULL]
127+
118128
# Theme validation behaves as expected
119129

120130
The `aspect.ratio` theme element must be a <numeric/integer> object.

tests/testthat/test-theme.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ test_that("element_text throws appropriate conditions", {
565565
element_text(colour = sqrt(2)),
566566
error = TRUE
567567
)
568+
569+
# Some absurd case found in reverse dependency check where
570+
# labs(y = element_blank()) for some reason
571+
el <- theme_get()$text
572+
expect_snapshot(
573+
element_grob(el, label = element_blank())
574+
)
568575
})
569576

570577
test_that("Theme validation behaves as expected", {

0 commit comments

Comments
 (0)