Skip to content

Commit c1f1ba9

Browse files
committed
Merge pull request #70 from samthor/dialog-button-fixes
update form method='dialog' to support button value, fix default returnValue
2 parents 192eef1 + 34ab056 commit c1f1ba9

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

dialog-polyfill.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
function findNearestDialog(el) {
2121
while (el) {
22-
if (/dialog/i.test(el.nodeName)) {
22+
if (el.nodeName == 'DIALOG') {
2323
return /** @type {HTMLDialogElement} */ (el);
2424
}
2525
el = el.parentElement;
@@ -54,6 +54,10 @@
5454
dialog.showModal = this.showModal.bind(this);
5555
dialog.close = this.close.bind(this);
5656

57+
if (!('returnValue' in dialog)) {
58+
dialog.returnValue = '';
59+
}
60+
5761
if ('MutationObserver' in window) {
5862
var mo = new MutationObserver(this.maybeHideModal.bind(this));
5963
mo.observe(dialog, { attributes: true, attributeFilter: ['open'] });
@@ -398,7 +402,7 @@
398402
};
399403

400404
dialogPolyfill.DialogManager.prototype.handleRemove_ = function(event) {
401-
if (!/dialog/i.test(event.target.nodeName)) { return; }
405+
if (event.target.nodeName != 'DIALOG') { return; }
402406

403407
var dialog = /** @type {HTMLDialogElement} */ (event.target);
404408
if (!dialog.open) { return; }
@@ -459,12 +463,13 @@
459463
var dialog = findNearestDialog(/** @type {Element} */ (ev.target));
460464
if (!dialog) { return; }
461465

462-
// FIXME: The original event doesn't contain the INPUT element used to
463-
// submit the form (if any). Look in some possible places.
466+
// FIXME: The original event doesn't contain the element used to submit the
467+
// form (if any). Look in some possible places.
464468
var returnValue;
465469
var cands = [document.activeElement, ev.explicitOriginalTarget];
470+
var els = ['BUTTON', 'INPUT'];
466471
cands.some(function(cand) {
467-
if (cand && cand.nodeName == 'INPUT' && cand.form == ev.target) {
472+
if (cand && cand.form == ev.target && els.indexOf(cand.nodeName) != -1) {
468473
returnValue = cand.value;
469474
return true;
470475
}

suite.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ void function() {
428428
});
429429

430430
suite('form', function() {
431-
test('dialog method', function() {
431+
test('dialog method input', function() {
432432
var value = 'ExpectedValue' + Math.random();
433433

434434
var form = document.createElement('form');
@@ -452,6 +452,35 @@ void function() {
452452
assert.isFalse(dialog.open);
453453
assert.equal(dialog.returnValue, value);
454454
});
455+
test('dialog method button', function() {
456+
var value = 'ExpectedValue' + Math.random();
457+
458+
var form = document.createElement('form');
459+
form.setAttribute('method', 'dialog');
460+
dialog.appendChild(form);
461+
462+
var button = document.createElement('button');
463+
button.value = value;
464+
form.appendChild(button);
465+
466+
dialog.showModal();
467+
button.focus(); // emulate user focus action
468+
button.click();
469+
470+
assert.isFalse(dialog.open);
471+
assert.equal(dialog.returnValue, value);
472+
473+
// Clear button value, confirm textContent is not used as value.
474+
button.value = '';
475+
button.removeAttribute('value');
476+
button.textContent = value;
477+
dialog.show();
478+
button.focus(); // emulate user focus action
479+
button.click();
480+
481+
assert.equal(dialog.returnValue, button.value,
482+
'don\'t take button textContent as value');
483+
});
455484
});
456485

457486
suite('order', function() {

0 commit comments

Comments
 (0)