Skip to content

Commit

Permalink
Merge pull request #104 from ramnathv/joe/bugfix/script-end-tag
Browse files Browse the repository at this point in the history
Fix a bug with </script> appearing in widget data
  • Loading branch information
ramnathv committed May 21, 2015
2 parents f6e158a + 4b0171f commit 86994f5
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion R/htmlwidgets.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,15 @@ widget_dependencies <- function(name, package){
# Generates a <script type="application/json"> tag with the JSON-encoded data,
# to be picked up by htmlwidgets.js for static rendering.
widget_data <- function(x, id, ...){
# It's illegal for </script> to appear inside of a script tag, even if it's
# inside a quoted string. Fortunately we know that in JSON, the only place
# the '<' character can appear is inside a quoted string, where a Unicode
# escape has the same effect, without confusing the browser's parser. The
# repro for the bug this gsub fixes is to have the string "</script>" appear
# anywhere in the data/metadata of a widget--you will get a syntax error
# instead of a properly rendered widget.
tags$script(type="application/json", `data-for` = id,
HTML(toJSON(createPayload(x)))
HTML(gsub("<", "\\\\u003c", toJSON(createPayload(x))))
)
}

Expand Down

0 comments on commit 86994f5

Please sign in to comment.