fix: Fix finding inputs with IDs containing spaces
Fixes #2799
The key issue in #2799 is that the input message handler uses "#" + message[i].id to compose the selector used to find the appropriate input. While it'd be reasonable to establish that input IDs should not contain spaces, we don't have any upstream checks and they clearly work in most cases.
This PR updates the update input message handler and the tab insert/remove logic to switch between a #${id} and [id="${id}"] selector depending on whether the ID contains spaces. The attribute selector is a tad slower, which is why we first check for spaces and use the #based selector if we can.
If we do detect spaces, we throw a console warning so that users can track down the offending input or tabset and fix it.
[shiny] Invalid ID, should not include spaces. {id: 'slider with space'}
I've only updated message handling logic where we were using the "#" + id pattern, in which case switching the selector is safe. I also think this is a safe approach considering that in cases where IDs without spaces are strictly required we should throw from the input constructor or warn from the update helper function; in both cases users should alert us to broken behavior.
Reprex App
Here's a very small app that demonstrates the change in behavior using updateSliderInput().
library(shiny)
ui <- fluidPage(
titlePanel("Issue 2799"),
p(a(href = "https://github.com/rstudio/shiny/issues/2799", "rstudio/shiny#2799")),
sliderInput("slider with space", "Slider with space in ID", 0, 10, 2),
actionButton("update", "Update inputs")
)
server <- function(input, output, session) {
observeEvent(input$update, {
updateSliderInput(session, "slider with space", value = 5)
})
}
shinyApp(ui, server)