From 99ab86f208c9c9d1b427b8a73a8792afb4324300 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Mon, 9 May 2016 19:29:39 +0200 Subject: [PATCH 1/2] escape \u2028 and \u2029 to compensate for JSON and JS difference --- app/helpers/react_on_rails_helper.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/helpers/react_on_rails_helper.rb b/app/helpers/react_on_rails_helper.rb index 5c852abd2..59dd51130 100644 --- a/app/helpers/react_on_rails_helper.rb +++ b/app/helpers/react_on_rails_helper.rb @@ -274,12 +274,24 @@ def server_rendered_react_component_html(props, react_component_name, dom_id, ReactOnRails::ServerRenderingPool.reset_pool_if_server_bundle_was_modified # Since this code is not inserted on a web page, we don't need to escape props + # + # However, as JSON (returned from `props_string(props)`) isn't JavaScript, + # but we want treat it as such, we need to compensate for the difference. + # + # \u2028 and \u2029 are valid characters in strings in JSON, but are treated + # as newline separators in JavaScript. As no newlines are allowed in + # strings in JavaScript, this causes an exception. + # + # We fix this by replacing these unicode characters with their escaped versions. + # This should be safe, as the only place they can appear is in strings anyway. + # + # Read more here: http://timelessrepo.com/json-isnt-a-javascript-subset wrapper_js = <<-JS (function() { var railsContext = #{rails_context(server_side: true).to_json}; #{initialize_redux_stores} - var props = #{props_string(props)}; + var props = #{props_string(props).gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')}; return ReactOnRails.serverRenderReactComponent({ name: '#{react_component_name}', domNodeId: '#{dom_id}', From 837ca64c98f8946e2c2548c7e2eeb622c6edf35e Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Mon, 9 May 2016 19:34:05 +0200 Subject: [PATCH 2/2] changelog for \u2028 and \u2029 bug --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1043f21ed..02602dde8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. Items under Contributors: please follow the recommendations outlined at [keepachangelog.com](http://keepachangelog.com/). Please use the existing headings and styling as a guide, and add a link for the version diff at the bottom of the file. Also, please update the `Unreleased` link to compare to the latest release version. ## [Unreleased] +##### Fixed +- Fixed errors when server rendered props contain \u2028 or \u2029 characters [#375](https://github.com/shakacode/react_on_rails/pull/375) by [mariusandra] + ##### Added - Non-digested version of assets in public folder [#413](https://github.com/shakacode/react_on_rails/pull/413) by [alleycat-at-git]