Skip to content

Commit a96fa19

Browse files
committed
Merge remote-tracking branch 'remotes/masone/form_tests' into issue518
2 parents 0f86c73 + f9c0614 commit a96fa19

File tree

3 files changed

+187
-6
lines changed

3 files changed

+187
-6
lines changed

jquery.pjax.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@ function handleSubmit(event, container, options) {
119119
options = optionsFor(container, options)
120120

121121
var form = event.currentTarget
122+
var $form = $(form)
122123

123124
if (form.tagName.toUpperCase() !== 'FORM')
124125
throw "$.pjax.submit requires a form element"
125126

126127
var defaults = {
127-
type: form.method.toUpperCase(),
128-
url: form.action,
129-
container: $(form).attr('data-pjax'),
128+
type: ($form.attr('method') || 'GET').toUpperCase(),
129+
url: $form.attr('action'),
130+
container: $form.attr('data-pjax'),
130131
target: form
131132
}
132133

@@ -292,10 +293,15 @@ function pjax(options) {
292293
window.history.replaceState(pjax.state, container.title, container.url)
293294
}
294295

296+
// Only blur the focus if the focused element is within the container.
297+
var blurFocus = $.contains(options.container, document.activeElement)
298+
295299
// Clear out any focused controls before inserting new page contents.
296-
try {
297-
document.activeElement.blur()
298-
} catch (e) { }
300+
if (blurFocus) {
301+
try {
302+
document.activeElement.blur()
303+
} catch (e) { }
304+
}
299305

300306
if (container.title) document.title = container.title
301307

test/unit/pjax.js

+157
Original file line numberDiff line numberDiff line change
@@ -1099,4 +1099,161 @@ if ($.support.pjax) {
10991099
equal(frame.location.search, "")
11001100
})
11011101
})
1102+
1103+
asyncTest("preserves input value when going back and forth", 1, function() {
1104+
var count = 0
1105+
var frame = this.frame
1106+
1107+
frame.$.pjax({url: "form.html", container: "#main"})
1108+
1109+
frame.$("#main").on("pjax:end", function() {
1110+
count++
1111+
var field = frame.$("input[type=text]")
1112+
1113+
if (count == 1) {
1114+
// Form
1115+
field.val("changed")
1116+
frame.history.back()
1117+
} else if (count == 2) {
1118+
// Hello
1119+
frame.history.forward()
1120+
} else if (count == 3) {
1121+
// Form
1122+
equal(field.val(), "changed", "Field value is preserved")
1123+
start()
1124+
}
1125+
})
1126+
})
1127+
1128+
asyncTest("preserves textarea value when going back and forth", 1, function() {
1129+
var count = 0
1130+
var frame = this.frame
1131+
1132+
frame.$.pjax({url: "form.html", container: "#main"})
1133+
1134+
frame.$("#main").on("pjax:end", function() {
1135+
count++
1136+
var field = frame.$("textarea")
1137+
1138+
if (count == 1) {
1139+
// Form
1140+
field.val("changed")
1141+
frame.history.back()
1142+
} else if (count == 2) {
1143+
// Hello
1144+
frame.history.forward()
1145+
} else if (count == 3) {
1146+
// Form
1147+
equal(field.val(), "changed", "Field value is preserved")
1148+
start()
1149+
}
1150+
})
1151+
})
1152+
1153+
asyncTest("preserves checkbox value when going back and forth", 1, function() {
1154+
var count = 0
1155+
var frame = this.frame
1156+
1157+
frame.$.pjax({url: "form.html", container: "#main"})
1158+
1159+
frame.$("#main").on("pjax:end", function() {
1160+
count++
1161+
var field = frame.$("input[type=checkbox]")
1162+
1163+
if (count == 1) {
1164+
// Form
1165+
field.prop("checked", true)
1166+
frame.history.back()
1167+
} else if (count == 2) {
1168+
// Hello
1169+
frame.history.forward()
1170+
} else if (count == 3) {
1171+
// Form
1172+
ok(field.prop("checked"), "Field value is preserved")
1173+
start()
1174+
}
1175+
})
1176+
})
1177+
1178+
asyncTest("preserves checkbox value when going back and forth", 1, function() {
1179+
var count = 0
1180+
var frame = this.frame
1181+
1182+
frame.$.pjax({url: "form.html", container: "#main"})
1183+
1184+
frame.$("#main").on("pjax:end", function() {
1185+
count++
1186+
var field = frame.$("input[type=radio]")
1187+
1188+
if (count == 1) {
1189+
// Form
1190+
field.prop("checked", true)
1191+
frame.history.back()
1192+
} else if (count == 2) {
1193+
// Hello
1194+
frame.history.forward()
1195+
} else if (count == 3) {
1196+
// Form
1197+
ok(field.prop("checked"), "Field value is preserved")
1198+
start()
1199+
}
1200+
})
1201+
})
1202+
1203+
asyncTest("preserves select value when going back and forth", 1, function() {
1204+
var count = 0
1205+
var frame = this.frame
1206+
1207+
frame.$.pjax({url: "form.html", container: "#main"})
1208+
1209+
frame.$("#main").on("pjax:end", function() {
1210+
count++
1211+
var option = frame.$("select option:last")
1212+
1213+
if (count == 1) {
1214+
// Form
1215+
option.prop("selected", true)
1216+
1217+
frame.history.back()
1218+
} else if (count == 2) {
1219+
// Hello
1220+
frame.history.forward()
1221+
} else if (count == 3) {
1222+
// Form
1223+
//var option = frame.$("select option:last")
1224+
equal(option.prop("selected"), true, "Field value is preserved")
1225+
start()
1226+
}
1227+
})
1228+
})
1229+
1230+
asyncTest("preserves multiple select value when going back and forth", 3, function() {
1231+
var count = 0
1232+
var frame = this.frame
1233+
1234+
frame.$.pjax({url: "form.html", container: "#main"})
1235+
1236+
frame.$("#main").on("pjax:end", function() {
1237+
count++
1238+
var field = frame.$("select").prop("multiple", true)
1239+
var options = field.find("option")
1240+
1241+
if (count == 1) {
1242+
// Form
1243+
options.prop("selected", true)
1244+
1245+
frame.history.back()
1246+
} else if (count == 2) {
1247+
// Hello
1248+
frame.history.forward()
1249+
} else if (count == 3) {
1250+
// Form
1251+
options.each(function(){
1252+
equal($(this).prop("selected"), true, "Field value is preserved")
1253+
})
1254+
start()
1255+
}
1256+
})
1257+
})
1258+
11021259
}

test/views/form.erb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%= title 'Form' %>
2+
3+
<form action="env.html" method="GET">
4+
<input type="text" value="foo">
5+
<textarea>foo</textarea>
6+
7+
<input type="checkbox" value="foo">
8+
<input type="radio" value="foo">
9+
10+
<select>
11+
<option value="foo">foo</option>
12+
<option value="bar">bar</option>
13+
<option value="baz">baz</option>
14+
</select>
15+
16+
</form>
17+
18+
<script type="text/javascript">window.parent.iframeLoad(window)</script>

0 commit comments

Comments
 (0)