Skip to content

Commit 8e07711

Browse files
committedSep 27, 2018
Do not enable disabled elements for XHR redirects
Fixes rails#29473.
1 parent 8541394 commit 8e07711

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed
 

‎actionview/app/assets/javascripts/rails-ujs/features/disable.coffee

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ Rails.handleDisabledElement = (e) ->
88

99
# Unified function to enable an element (link, button and form)
1010
Rails.enableElement = (e) ->
11-
element = if e instanceof Event then e.target else e
11+
if e instanceof Event
12+
return if isXhrRedirect(e)
13+
element = e.target
14+
else
15+
element = e
16+
1217
if matches(element, Rails.linkDisableSelector)
1318
enableLinkElement(element)
1419
else if matches(element, Rails.buttonDisableSelector) or matches(element, Rails.formEnableSelector)
@@ -80,3 +85,7 @@ enableFormElement = (element) ->
8085
setData(element, 'ujs:enable-with', null) # clean up cache
8186
element.disabled = false
8287
setData(element, 'ujs:disabled', null)
88+
89+
isXhrRedirect = (event) ->
90+
xhr = event.detail?[0]
91+
xhr?.getResponseHeader("X-Xhr-Redirect")?

‎actionview/test/ujs/public/test/data-disable.js

+17
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,20 @@ asyncTest('button[data-remote][data-disable] re-enables when `ajax:error` event
320320
start()
321321
}, 30)
322322
})
323+
324+
asyncTest('do not enable elements for XHR redirects', 6, function() {
325+
var link = $('a[data-disable]').attr('data-remote', true).attr('href', '/echo?with_xhr_redirect=true')
326+
327+
App.checkEnabledState(link, 'Click me')
328+
329+
link
330+
.bindNative('ajax:send', function() {
331+
App.checkDisabledState(link, 'Click me')
332+
})
333+
.triggerNative('click')
334+
335+
setTimeout(function() {
336+
App.checkDisabledState(link, 'Click me')
337+
start()
338+
}, 30)
339+
})

‎actionview/test/ujs/public/test/settings.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var App = App || {}
2+
var Turbolinks = Turbolinks || {}
23

34
App.assertCallbackInvoked = function(callbackName) {
45
ok(true, callbackName + ' callback should have been invoked')
@@ -116,3 +117,6 @@ $.fn.extend({
116117
return this
117118
}
118119
})
120+
121+
Turbolinks.clearCache = function() {}
122+
Turbolinks.visit = function() {}

‎actionview/test/ujs/server.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ def echo
6464
if params[:content_type] && params[:content]
6565
render inline: params[:content], content_type: params[:content_type]
6666
elsif request.xhr?
67-
render json: JSON.generate(data)
67+
if params[:with_xhr_redirect]
68+
response.set_header("X-Xhr-Redirect", "http://example.com/")
69+
render inline: %{Turbolinks.clearCache()\nTurbolinks.visit("http://example.com/", {"action":"replace"})}
70+
else
71+
render json: JSON.generate(data)
72+
end
6873
elsif params[:iframe]
6974
payload = JSON.generate(data).gsub("<", "&lt;").gsub(">", "&gt;")
7075
html = <<-HTML

0 commit comments

Comments
 (0)
Please sign in to comment.