diff options
author | Raymond Zhou <razh@users.noreply.github.com> | 2018-03-19 02:42:23 -0400 |
---|---|---|
committer | razh <somecrazyidiot@gmail.com> | 2018-03-19 03:24:56 -0400 |
commit | 48e44edfd0a8a7a29aa8fad39638ac0ee5243f42 (patch) | |
tree | 1ae9709f5dd11b7fa10f9bf12dfe950dc8fa6e39 /actionview/test/ujs | |
parent | d91e6f1213146e327b47f2a65776c7f4f347a2d6 (diff) | |
download | rails-48e44edfd0a8a7a29aa8fad39638ac0ee5243f42.tar.gz rails-48e44edfd0a8a7a29aa8fad39638ac0ee5243f42.tar.bz2 rails-48e44edfd0a8a7a29aa8fad39638ac0ee5243f42.zip |
Pass HTML responses as plain-text in rails-ujs
Running HTML responses through `DOMParser#parseFromString` results in
complete `HTMLDocument` instances with unnecessary surrounding tags.
For example:
new DOMParser().parseFromString('<p>hello</p>', 'text/html')
Will output:
<html>
<head></head>
<body>
<p>hello</p>
</body>
</html>
This is passed to the `ajax:success` handler as `event.detail[0]`
(`data`), but cannot be used directly without first traversing the
document.
To resolve this, only XML content is passed through `parseFromString`,
while HTML content is treated as plain-text.
This matches the behavior of jquery-ujs, which relied on jQuery's
response-type inference.
Diffstat (limited to 'actionview/test/ujs')
-rw-r--r-- | actionview/test/ujs/public/test/call-remote.js | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/actionview/test/ujs/public/test/call-remote.js b/actionview/test/ujs/public/test/call-remote.js index 5932195363..8a88471982 100644 --- a/actionview/test/ujs/public/test/call-remote.js +++ b/actionview/test/ujs/public/test/call-remote.js @@ -128,6 +128,17 @@ asyncTest('execution of JS code does not modify current DOM', 1, function() { }) }) +asyncTest('HTML content should be plain-text', 1, function() { + buildForm({ method: 'post', 'data-type': 'html' }) + + $('form').append('<input type="text" name="content_type" value="text/html">') + $('form').append('<input type="text" name="content" value="<p>hello</p>">') + + submit(function(e, data, status, xhr) { + ok(data === '<p>hello</ps>', 'returned data should be a plain-text string') + }) +}) + asyncTest('XML document should be parsed', 1, function() { buildForm({ method: 'post', 'data-type': 'html' }) |