From dc4ab30456f9eeee48bb24c812b4be129552a060 Mon Sep 17 00:00:00 2001 From: terka Date: Sat, 11 Jul 2015 00:36:53 +0200 Subject: [PATCH 1/3] Fixed issue #518: Textarea and select value isn't preserved when going back/forward --- jquery.pjax.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/jquery.pjax.js b/jquery.pjax.js index f1728d45..1a863f88 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -556,6 +556,20 @@ function uniqueId() { } function cloneContents(container) { + // Preserve textarea values + var textarea = findAll(container, "textarea") + textarea.text(function(i, text){return textarea[i].value}) + + // Preserve select values + findAll(container, "select").each(function(i, elem){ + elem = $(elem); + var values = $(elem).val(); + elem.find('option[selected]').attr('selected', false); + elem.find('option').filter(function(){ + return ($.inArray(this.value, values) !== -1); + }).attr('selected', true); + }) + var cloned = container.clone() // Unmark script tags as already being eval'd so they can get executed again // when restored from cache. HAXX: Uses jQuery internal method. From f9c06149f6058a1fd440d3e885c4d225a9e58e29 Mon Sep 17 00:00:00 2001 From: "Christian Felder (masone)" Date: Wed, 23 Sep 2015 18:31:25 +0200 Subject: [PATCH 2/3] Add tests for form value preservation when traversing history Addition to PR #542 --- test/unit/pjax.js | 157 ++++++++++++++++++++++++++++++++++++++++++++ test/views/form.erb | 18 +++++ 2 files changed, 175 insertions(+) create mode 100644 test/views/form.erb diff --git a/test/unit/pjax.js b/test/unit/pjax.js index 54f5b8b8..7540a47c 100644 --- a/test/unit/pjax.js +++ b/test/unit/pjax.js @@ -1099,4 +1099,161 @@ if ($.support.pjax) { equal(frame.location.search, "") }) }) + + asyncTest("preserves input value when going back and forth", 1, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var field = frame.$("input[type=text]") + + if (count == 1) { + // Form + field.val("changed") + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + equal(field.val(), "changed", "Field value is preserved") + start() + } + }) + }) + + asyncTest("preserves textarea value when going back and forth", 1, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var field = frame.$("textarea") + + if (count == 1) { + // Form + field.val("changed") + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + equal(field.val(), "changed", "Field value is preserved") + start() + } + }) + }) + + asyncTest("preserves checkbox value when going back and forth", 1, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var field = frame.$("input[type=checkbox]") + + if (count == 1) { + // Form + field.prop("checked", true) + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + ok(field.prop("checked"), "Field value is preserved") + start() + } + }) + }) + + asyncTest("preserves checkbox value when going back and forth", 1, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var field = frame.$("input[type=radio]") + + if (count == 1) { + // Form + field.prop("checked", true) + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + ok(field.prop("checked"), "Field value is preserved") + start() + } + }) + }) + + asyncTest("preserves select value when going back and forth", 1, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var option = frame.$("select option:last") + + if (count == 1) { + // Form + option.prop("selected", true) + + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + //var option = frame.$("select option:last") + equal(option.prop("selected"), true, "Field value is preserved") + start() + } + }) + }) + + asyncTest("preserves multiple select value when going back and forth", 3, function() { + var count = 0 + var frame = this.frame + + frame.$.pjax({url: "form.html", container: "#main"}) + + frame.$("#main").on("pjax:end", function() { + count++ + var field = frame.$("select").prop("multiple", true) + var options = field.find("option") + + if (count == 1) { + // Form + options.prop("selected", true) + + frame.history.back() + } else if (count == 2) { + // Hello + frame.history.forward() + } else if (count == 3) { + // Form + options.each(function(){ + equal($(this).prop("selected"), true, "Field value is preserved") + }) + start() + } + }) + }) + } diff --git a/test/views/form.erb b/test/views/form.erb new file mode 100644 index 00000000..d955d80f --- /dev/null +++ b/test/views/form.erb @@ -0,0 +1,18 @@ +<%= title 'Form' %> + +
+ + + + + + + + +
+ + From 0f86c7377fd40b802ea0370fa46f5491d87ec33a Mon Sep 17 00:00:00 2001 From: Tereza Tomcova Date: Wed, 23 Sep 2015 19:48:12 +0200 Subject: [PATCH 3/3] Fixed preserving single select value when going back and forward --- jquery.pjax.js | 1 + 1 file changed, 1 insertion(+) diff --git a/jquery.pjax.js b/jquery.pjax.js index 1a863f88..210903b9 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -564,6 +564,7 @@ function cloneContents(container) { findAll(container, "select").each(function(i, elem){ elem = $(elem); var values = $(elem).val(); + values = $.isArray(values) ? values : [values]; elem.find('option[selected]').attr('selected', false); elem.find('option').filter(function(){ return ($.inArray(this.value, values) !== -1);