From 92af43f9756176a0d4f43462fea2ce51eb657b13 Mon Sep 17 00:00:00 2001 From: Dmitry Karpunin Date: Tue, 28 Oct 2014 13:32:26 +0300 Subject: [PATCH 1/3] data-method with params --- src/rails.js | 31 ++++++++++++++++++++++++++++--- test/public/test/data-method.js | 25 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/rails.js b/src/rails.js index c0f45238..d4659652 100644 --- a/src/rails.js +++ b/src/rails.js @@ -173,18 +173,43 @@ var href = rails.href(link), method = link.data('method'), target = link.attr('target'), + params = link.data('params'), csrfToken = $('meta[name=csrf-token]').attr('content'), csrfParam = $('meta[name=csrf-param]').attr('content'), form = $('
'), - metadataInput = ''; + dataInputs = ''; if (csrfParam !== undefined && csrfToken !== undefined) { - metadataInput += ''; + dataInputs += ''; + } + + if (params) { + + function buildParamsInputs(key, value) { + switch (typeof value) { + case 'array': + var result = []; + for (var i = 0, l = value.length; i < l; i++) { + result.push( buildParamsInputs(key === null ? i : key + '[' + i + ']', value[i]) ); + } + return result.join(''); + case 'object': + var result = []; + for (var i in value) { + result.push( buildParamsInputs(key === null ? i : key + '[' + i + ']', value[i]) ); + } + return result.join(''); + default: + return ''; + } + } + + dataInputs += buildParamsInputs(null, params); } if (target) { form.attr('target', target); } - form.hide().append(metadataInput).appendTo('body'); + form.hide().append(dataInputs).appendTo('body'); form.submit(); }, diff --git a/test/public/test/data-method.js b/test/public/test/data-method.js index c4426624..a40787d8 100644 --- a/test/public/test/data-method.js +++ b/test/public/test/data-method.js @@ -29,6 +29,31 @@ asyncTest('link with "data-method" set to "delete"', 3, function() { }); }); +var escapeMap = { + '<': '<', + '>': '>' +}; +function escapeReplacer (entity) { return escapeMap[entity] || entity; } +function escapeHTML(str) { return String(str).replace(/<|>/g, escapeReplacer); } + +asyncTest('link with "data-method" set to "post" and "data-params"', 4, function() { + var value1 = 0, + value2 = '\'quoted"/>&<\'value"', + value3 = {foo: {bar: {baz: value2}}}, + params = { + data1: value1, + data2: value2, + data3: value3 + }; + $('a[data-method]').attr({'data-method': 'post', 'data-params': JSON.stringify(params)}); + submit(function(data) { + equal(data.REQUEST_METHOD, 'POST'); + equal(data.params.data1, escapeHTML(value1), 'params should have key data1 with right value'); + equal(data.params.data2, escapeHTML(value2), 'params should have key data2 with right value'); + propEqual(data.params.data3, {foo: {bar: {baz: escapeHTML(value2)}}}, 'params should have key data3 with right value'); + }); +}); + asyncTest('link with "data-method" and CSRF', 1, function() { $('#qunit-fixture') .append('') From 384fac189108ecf66f68bb6242314e5a363a251e Mon Sep 17 00:00:00 2001 From: Dmitry Karpunin Date: Thu, 30 Oct 2014 13:56:07 +0300 Subject: [PATCH 2/3] Add test for a link with data-remote attribute and data-params as JSON --- test/public/test/data-remote.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js index f8c6b2a8..7722c9ed 100644 --- a/test/public/test/data-remote.js +++ b/test/public/test/data-remote.js @@ -84,6 +84,29 @@ asyncTest('clicking on a link with data-remote attribute', 5, function() { .trigger('click'); }); +asyncTest('clicking on a link with data-remote attribute and data-params as JSON', 6, function() { + var value1 = 0, + value2 = '\'quoted"/>&<\'value"', + value3 = {foo: {bar: {baz: value2}}}, + params = { + data1: value1, + data2: value2, + data3: value3 + }; + $('a[data-remote]') + .attr('data-params', JSON.stringify(params)) + .bind('ajax:success', function(e, data, status, xhr) { + App.assertCallbackInvoked('ajax:success'); + App.assertRequestPath(data, '/echo'); + equal(data.params.data1, value1, 'params should have key data1 with right value'); + equal(data.params.data2, value2, 'params should have key data2 with right value'); + propEqual(data.params.data3, {foo: {bar: {baz: value2}}}, 'params should have key data3 with right value'); + App.assertGetRequest(data); + }) + .bind('ajax:complete', function() { start() }) + .trigger('click'); +}); + asyncTest('clicking on a button with data-remote attribute', 5, function() { $('button[data-remote]') .bind('ajax:success', function(e, data, status, xhr) { From 65ff2668240bf97ec2c815cf408628083c173024 Mon Sep 17 00:00:00 2001 From: Dmitry Karpunin Date: Thu, 30 Oct 2014 14:50:40 +0300 Subject: [PATCH 3/3] Remove unnecessary json escape from test server and data-method test --- test/public/test/data-method.js | 13 +++---------- test/server.rb | 3 +-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/test/public/test/data-method.js b/test/public/test/data-method.js index a40787d8..71287b7a 100644 --- a/test/public/test/data-method.js +++ b/test/public/test/data-method.js @@ -29,13 +29,6 @@ asyncTest('link with "data-method" set to "delete"', 3, function() { }); }); -var escapeMap = { - '<': '<', - '>': '>' -}; -function escapeReplacer (entity) { return escapeMap[entity] || entity; } -function escapeHTML(str) { return String(str).replace(/<|>/g, escapeReplacer); } - asyncTest('link with "data-method" set to "post" and "data-params"', 4, function() { var value1 = 0, value2 = '\'quoted"/>&<\'value"', @@ -48,9 +41,9 @@ asyncTest('link with "data-method" set to "post" and "data-params"', 4, function $('a[data-method]').attr({'data-method': 'post', 'data-params': JSON.stringify(params)}); submit(function(data) { equal(data.REQUEST_METHOD, 'POST'); - equal(data.params.data1, escapeHTML(value1), 'params should have key data1 with right value'); - equal(data.params.data2, escapeHTML(value2), 'params should have key data2 with right value'); - propEqual(data.params.data3, {foo: {bar: {baz: escapeHTML(value2)}}}, 'params should have key data3 with right value'); + equal(data.params.data1, value1, 'params should have key data1 with right value'); + equal(data.params.data2, value2, 'params should have key data2 with right value'); + propEqual(data.params.data3, {foo: {bar: {baz: value2}}}, 'params should have key data3 with right value'); }); }); diff --git a/test/server.rb b/test/server.rb index 85f11db2..25069477 100644 --- a/test/server.rb +++ b/test/server.rb @@ -61,11 +61,10 @@ def jquery_versions content_type 'application/json' data.to_json elsif params[:iframe] - payload = data.to_json.gsub('<', '<').gsub('>', '>') <<-HTML

You shouldn't be seeing this. Go back

HTML