Skip to content

Commit

Permalink
Merge pull request GoogleChrome#70 from samthor/dialog-button-fixes
Browse files Browse the repository at this point in the history
update form method='dialog' to support button value, fix default returnValue
  • Loading branch information
samthor committed Aug 15, 2015
2 parents 192eef1 34ab056 commit c1f1ba9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
15 changes: 10 additions & 5 deletions dialog-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 19,7 @@
*/
function findNearestDialog(el) {
while (el) {
if (/dialog/i.test(el.nodeName)) {
if (el.nodeName == 'DIALOG') {
return /** @type {HTMLDialogElement} */ (el);
}
el = el.parentElement;
Expand Down Expand Up @@ -54,6 54,10 @@
dialog.showModal = this.showModal.bind(this);
dialog.close = this.close.bind(this);

if (!('returnValue' in dialog)) {
dialog.returnValue = '';
}

if ('MutationObserver' in window) {
var mo = new MutationObserver(this.maybeHideModal.bind(this));
mo.observe(dialog, { attributes: true, attributeFilter: ['open'] });
Expand Down Expand Up @@ -398,7 402,7 @@
};

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

var dialog = /** @type {HTMLDialogElement} */ (event.target);
if (!dialog.open) { return; }
Expand Down Expand Up @@ -459,12 463,13 @@
var dialog = findNearestDialog(/** @type {Element} */ (ev.target));
if (!dialog) { return; }

// FIXME: The original event doesn't contain the INPUT element used to
// submit the form (if any). Look in some possible places.
// FIXME: The original event doesn't contain the element used to submit the
// form (if any). Look in some possible places.
var returnValue;
var cands = [document.activeElement, ev.explicitOriginalTarget];
var els = ['BUTTON', 'INPUT'];
cands.some(function(cand) {
if (cand && cand.nodeName == 'INPUT' && cand.form == ev.target) {
if (cand && cand.form == ev.target && els.indexOf(cand.nodeName) != -1) {
returnValue = cand.value;
return true;
}
Expand Down
31 changes: 30 additions & 1 deletion suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 428,7 @@ void function() {
});

suite('form', function() {
test('dialog method', function() {
test('dialog method input', function() {
var value = 'ExpectedValue' Math.random();

var form = document.createElement('form');
Expand All @@ -452,6 452,35 @@ void function() {
assert.isFalse(dialog.open);
assert.equal(dialog.returnValue, value);
});
test('dialog method button', function() {
var value = 'ExpectedValue' Math.random();

var form = document.createElement('form');
form.setAttribute('method', 'dialog');
dialog.appendChild(form);

var button = document.createElement('button');
button.value = value;
form.appendChild(button);

dialog.showModal();
button.focus(); // emulate user focus action
button.click();

assert.isFalse(dialog.open);
assert.equal(dialog.returnValue, value);

// Clear button value, confirm textContent is not used as value.
button.value = '';
button.removeAttribute('value');
button.textContent = value;
dialog.show();
button.focus(); // emulate user focus action
button.click();

assert.equal(dialog.returnValue, button.value,
'don\'t take button textContent as value');
});
});

suite('order', function() {
Expand Down

0 comments on commit c1f1ba9

Please sign in to comment.