Skip to content

Commit

Permalink
Fix rendering of embedded HTML widgets with htmlwidgets 1.6.0 or later
Browse files Browse the repository at this point in the history
In general, fix rendering of HTML tags with duplicate attributes.
  • Loading branch information
glin committed Dec 24, 2022
1 parent cf500a1 commit 0934174
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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/)
Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 <option> elements.
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ test_that("asReactTag", {
# Attributes should be preserved
expect_equal(asReactTag(div(factor("xy"))), div("xy"))
expect_equal(asReactTag(div(div(as.Date("2019-01-03")))), div(div("2019-01-03")))

# Duplicate attributes should be included and collapsed (e.g., for likelihood of
# duplicate class attributes in HTML widgets with htmlwidgets >= 1.6.0)
expect_equal(asReactTag(div(class = "a", class = "b")), div(className = "a b"))
expect_equal(
asReactTag(span(class = "a", test = "t", style = list(color = "red"), class = "bb", style = list(color = "blue"))),
span(test = "t", style = list(list(color = "red"), list(color = "blue")), className = "a bb")
)
})

test_that("asReactTag preserves HTML dependencies", {
Expand Down

0 comments on commit 0934174

Please sign in to comment.