diff --git a/NEWS.md b/NEWS.md index 20a95cdf..70d2bb5c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # reactable 0.4.1.9000 (Unreleased) +## Minor improvements and bug fixes + +* Fixed rendering of embedded HTML widgets when `htmlwidgets` 1.6.0 or later is installed. + In general, fixed rendering of any Shiny HTML tags with duplicate attributes. (@tomsing1, [#306](https://github.com/glin/reactable/issues/304)) + # reactable 0.4.1 [Documentation - reactable 0.4.1](https://v0-4-1--reactable-docs.netlify.app/) diff --git a/R/utils.R b/R/utils.R index 678f26f4..f19fa240 100644 --- a/R/utils.R +++ b/R/utils.R @@ -131,6 +131,7 @@ asReactTag <- function(x) { # Filter null elements for proper hydration x$children <- filterNulls(x$children) x$children <- lapply(x$children, asReactTag) + x <- normalizeTagAttributes(x) x$attribs <- asReactAttributes(x$attribs, x$name) x } @@ -172,6 +173,18 @@ unnestTagList <- function(x) { htmltools::attachDependencies(tags, htmlDeps) } +# Normalize tags that may have duplicate attributes, such as duplicate class attributes +# in HTML widgets with htmlwidgets >= 1.6.0. +normalizeTagAttributes <- function(tag) { + attribNames <- names(tag$attribs) + for (dupAttrib in attribNames[duplicated(attribNames)]) { + combinedValue <- htmltools::tagGetAttribute(tag, dupAttrib) + tag$attribs[attribNames == dupAttrib] <- NULL + tag$attribs[[dupAttrib]] <- combinedValue + } + tag +} + # Transform HTML attributes to React DOM attributes. # Not all attributes are supported at the moment - notable exceptions are # some event handler attributes and `selected` attributes for