From 8e07711d65a10be697020cc775e966bc2df87b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20B=C3=B3na?= Date: Thu, 14 Dec 2017 10:14:24 +0100 Subject: Do not enable disabled elements for XHR redirects Fixes #29473. --- .../javascripts/rails-ujs/features/disable.coffee | 11 ++++++++++- actionview/test/ujs/public/test/data-disable.js | 17 +++++++++++++++++ actionview/test/ujs/public/test/settings.js | 4 ++++ actionview/test/ujs/server.rb | 7 ++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/actionview/app/assets/javascripts/rails-ujs/features/disable.coffee b/actionview/app/assets/javascripts/rails-ujs/features/disable.coffee index 90aa3bdf0e..a8c692ee62 100644 --- a/actionview/app/assets/javascripts/rails-ujs/features/disable.coffee +++ b/actionview/app/assets/javascripts/rails-ujs/features/disable.coffee @@ -8,7 +8,12 @@ Rails.handleDisabledElement = (e) -> # Unified function to enable an element (link, button and form) Rails.enableElement = (e) -> - element = if e instanceof Event then e.target else e + if e instanceof Event + return if isXhrRedirect(e) + element = e.target + else + element = e + if matches(element, Rails.linkDisableSelector) enableLinkElement(element) else if matches(element, Rails.buttonDisableSelector) or matches(element, Rails.formEnableSelector) @@ -80,3 +85,7 @@ enableFormElement = (element) -> setData(element, 'ujs:enable-with', null) # clean up cache element.disabled = false setData(element, 'ujs:disabled', null) + +isXhrRedirect = (event) -> + xhr = event.detail?[0] + xhr?.getResponseHeader("X-Xhr-Redirect")? diff --git a/actionview/test/ujs/public/test/data-disable.js b/actionview/test/ujs/public/test/data-disable.js index e9919764b6..88dc801b2f 100644 --- a/actionview/test/ujs/public/test/data-disable.js +++ b/actionview/test/ujs/public/test/data-disable.js @@ -320,3 +320,20 @@ asyncTest('button[data-remote][data-disable] re-enables when `ajax:error` event start() }, 30) }) + +asyncTest('do not enable elements for XHR redirects', 6, function() { + var link = $('a[data-disable]').attr('data-remote', true).attr('href', '/echo?with_xhr_redirect=true') + + App.checkEnabledState(link, 'Click me') + + link + .bindNative('ajax:send', function() { + App.checkDisabledState(link, 'Click me') + }) + .triggerNative('click') + + setTimeout(function() { + App.checkDisabledState(link, 'Click me') + start() + }, 30) +}) diff --git a/actionview/test/ujs/public/test/settings.js b/actionview/test/ujs/public/test/settings.js index b1ce3b8c64..05677f2595 100644 --- a/actionview/test/ujs/public/test/settings.js +++ b/actionview/test/ujs/public/test/settings.js @@ -1,4 +1,5 @@ var App = App || {} +var Turbolinks = Turbolinks || {} App.assertCallbackInvoked = function(callbackName) { ok(true, callbackName + ' callback should have been invoked') @@ -116,3 +117,6 @@ $.fn.extend({ return this } }) + +Turbolinks.clearCache = function() {} +Turbolinks.visit = function() {} diff --git a/actionview/test/ujs/server.rb b/actionview/test/ujs/server.rb index 48e9bcb65f..56f436c8b8 100644 --- a/actionview/test/ujs/server.rb +++ b/actionview/test/ujs/server.rb @@ -64,7 +64,12 @@ class TestsController < ActionController::Base if params[:content_type] && params[:content] render inline: params[:content], content_type: params[:content_type] elsif request.xhr? - render json: JSON.generate(data) + if params[:with_xhr_redirect] + response.set_header("X-Xhr-Redirect", "http://example.com/") + render inline: %{Turbolinks.clearCache()\nTurbolinks.visit("http://example.com/", {"action":"replace"})} + else + render json: JSON.generate(data) + end elsif params[:iframe] payload = JSON.generate(data).gsub("<", "<").gsub(">", ">") html = <<-HTML -- cgit v1.2.3