Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix finding inputs with IDs containing spaces #3852

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gadenbuie
Copy link
Member

@gadenbuie gadenbuie commented Jul 6, 2023

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)

Switches between a `#${id}` and `id="${id}"` selector depending on whether the ID contains spaces.

Throws a console warning if an ID is found to contain spaces so that users can track down the offending input or tabset and fix it.
@gadenbuie gadenbuie requested review from wch and cpsievert July 6, 2023 21:42
@gadenbuie gadenbuie changed the title fix(2799): Fix finding inputs with IDs containing spaces fix: Fix finding inputs with IDs containing spaces Jul 7, 2023
Copy link
Collaborator

@cpsievert cpsievert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait on merging this until after #3928 is merged.

Also, after merging, let's ping @nstrayer about potentially surfacing the console.warn() in the UI

Lastly, it'd be good to have some (explicit) test coverage on this change

@gadenbuie gadenbuie added this to the 1.9 milestone Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Widgets' inputIds behave incorrectly with spaces
2 participants