Skip to content

Commit 5033aac

Browse files
authored
Merge pull request #214 from GoogleChrome/fix-submitter
fix possible .submitter being null
2 parents 75df500 + 840c5e5 commit 5033aac

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

dist/dialog-polyfill.esm.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ function findFocusableElementWithin(hostElement) {
153153
/**
154154
* Determines if an element is attached to the DOM.
155155
* @param {Element} element to check
156-
* @return {Boolean} whether the element is in DOM
156+
* @return {boolean} whether the element is in DOM
157157
*/
158158
function isConnected(element) {
159159
return element.isConnected || document.body.contains(element);
160160
}
161161

162162
/**
163163
* @param {!Event} event
164+
* @return {?Element}
164165
*/
165166
function findFormSubmitter(event) {
166167
if (event.submitter) {
@@ -179,7 +180,7 @@ function findFormSubmitter(event) {
179180
submitter = root.activeElement;
180181
}
181182

182-
if (submitter.form !== form) {
183+
if (!submitter || submitter.form !== form) {
183184
return null;
184185
}
185186
return submitter;
@@ -195,7 +196,7 @@ function maybeHandleSubmit(event) {
195196
var form = /** @type {!HTMLFormElement} */ (event.target);
196197

197198
// We'd have a value if we clicked on an imagemap.
198-
var value = dialogPolyfill.useValue;
199+
var value = dialogPolyfill.imagemapUseValue;
199200
var submitter = findFormSubmitter(event);
200201
if (value === null && submitter) {
201202
value = submitter.value;
@@ -215,7 +216,8 @@ function maybeHandleSubmit(event) {
215216
}
216217
event.preventDefault();
217218

218-
if (submitter) {
219+
if (value != null) {
220+
// nb. we explicitly check against null/undefined
219221
dialog.close(value);
220222
} else {
221223
dialog.close();
@@ -751,7 +753,7 @@ dialogPolyfill.DialogManager.prototype.removeDialog = function(dpi) {
751753

752754
dialogPolyfill.dm = new dialogPolyfill.DialogManager();
753755
dialogPolyfill.formSubmitter = null;
754-
dialogPolyfill.useValue = null;
756+
dialogPolyfill.imagemapUseValue = null;
755757

756758
/**
757759
* Installs global handlers, such as click listers and native method overrides. These are needed
@@ -796,7 +798,7 @@ if (window.HTMLDialogElement === undefined) {
796798
*/
797799
document.addEventListener('click', function(ev) {
798800
dialogPolyfill.formSubmitter = null;
799-
dialogPolyfill.useValue = null;
801+
dialogPolyfill.imagemapUseValue = null;
800802
if (ev.defaultPrevented) { return; } // e.g. a submit which prevents default submission
801803

802804
var target = /** @type {Element} */ (ev.target);
@@ -810,7 +812,7 @@ if (window.HTMLDialogElement === undefined) {
810812
if (!valid) {
811813
if (!(target.localName === 'input' && target.type === 'image')) { return; }
812814
// this is a <input type="image">, which can submit forms
813-
dialogPolyfill.useValue = ev.offsetX + ',' + ev.offsetY;
815+
dialogPolyfill.imagemapUseValue = ev.offsetX + ',' + ev.offsetY;
814816
}
815817

816818
var dialog = findNearestDialog(target);

dist/dialog-polyfill.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,15 @@
159159
/**
160160
* Determines if an element is attached to the DOM.
161161
* @param {Element} element to check
162-
* @return {Boolean} whether the element is in DOM
162+
* @return {boolean} whether the element is in DOM
163163
*/
164164
function isConnected(element) {
165165
return element.isConnected || document.body.contains(element);
166166
}
167167

168168
/**
169169
* @param {!Event} event
170+
* @return {?Element}
170171
*/
171172
function findFormSubmitter(event) {
172173
if (event.submitter) {
@@ -185,7 +186,7 @@
185186
submitter = root.activeElement;
186187
}
187188

188-
if (submitter.form !== form) {
189+
if (!submitter || submitter.form !== form) {
189190
return null;
190191
}
191192
return submitter;
@@ -201,7 +202,7 @@
201202
var form = /** @type {!HTMLFormElement} */ (event.target);
202203

203204
// We'd have a value if we clicked on an imagemap.
204-
var value = dialogPolyfill.useValue;
205+
var value = dialogPolyfill.imagemapUseValue;
205206
var submitter = findFormSubmitter(event);
206207
if (value === null && submitter) {
207208
value = submitter.value;
@@ -221,7 +222,8 @@
221222
}
222223
event.preventDefault();
223224

224-
if (submitter) {
225+
if (value != null) {
226+
// nb. we explicitly check against null/undefined
225227
dialog.close(value);
226228
} else {
227229
dialog.close();
@@ -757,7 +759,7 @@
757759

758760
dialogPolyfill.dm = new dialogPolyfill.DialogManager();
759761
dialogPolyfill.formSubmitter = null;
760-
dialogPolyfill.useValue = null;
762+
dialogPolyfill.imagemapUseValue = null;
761763

762764
/**
763765
* Installs global handlers, such as click listers and native method overrides. These are needed
@@ -802,7 +804,7 @@
802804
*/
803805
document.addEventListener('click', function(ev) {
804806
dialogPolyfill.formSubmitter = null;
805-
dialogPolyfill.useValue = null;
807+
dialogPolyfill.imagemapUseValue = null;
806808
if (ev.defaultPrevented) { return; } // e.g. a submit which prevents default submission
807809

808810
var target = /** @type {Element} */ (ev.target);
@@ -816,7 +818,7 @@
816818
if (!valid) {
817819
if (!(target.localName === 'input' && target.type === 'image')) { return; }
818820
// this is a <input type="image">, which can submit forms
819-
dialogPolyfill.useValue = ev.offsetX + ',' + ev.offsetY;
821+
dialogPolyfill.imagemapUseValue = ev.offsetX + ',' + ev.offsetY;
820822
}
821823

822824
var dialog = findNearestDialog(target);

index.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,15 @@ function findFocusableElementWithin(hostElement) {
154154
/**
155155
* Determines if an element is attached to the DOM.
156156
* @param {Element} element to check
157-
* @return {Boolean} whether the element is in DOM
157+
* @return {boolean} whether the element is in DOM
158158
*/
159159
function isConnected(element) {
160160
return element.isConnected || document.body.contains(element);
161161
}
162162

163163
/**
164164
* @param {!Event} event
165+
* @return {?Element}
165166
*/
166167
function findFormSubmitter(event) {
167168
if (event.submitter) {
@@ -180,7 +181,7 @@ function findFormSubmitter(event) {
180181
submitter = root.activeElement;
181182
}
182183

183-
if (submitter.form !== form) {
184+
if (!submitter || submitter.form !== form) {
184185
return null;
185186
}
186187
return submitter;
@@ -196,7 +197,7 @@ function maybeHandleSubmit(event) {
196197
var form = /** @type {!HTMLFormElement} */ (event.target);
197198

198199
// We'd have a value if we clicked on an imagemap.
199-
var value = dialogPolyfill.useValue;
200+
var value = dialogPolyfill.imagemapUseValue;
200201
var submitter = findFormSubmitter(event);
201202
if (value === null && submitter) {
202203
value = submitter.value;
@@ -216,7 +217,8 @@ function maybeHandleSubmit(event) {
216217
}
217218
event.preventDefault();
218219

219-
if (submitter) {
220+
if (value != null) {
221+
// nb. we explicitly check against null/undefined
220222
dialog.close(value);
221223
} else {
222224
dialog.close();
@@ -754,7 +756,7 @@ dialogPolyfill.DialogManager.prototype.removeDialog = function(dpi) {
754756

755757
dialogPolyfill.dm = new dialogPolyfill.DialogManager();
756758
dialogPolyfill.formSubmitter = null;
757-
dialogPolyfill.useValue = null;
759+
dialogPolyfill.imagemapUseValue = null;
758760

759761
/**
760762
* Installs global handlers, such as click listers and native method overrides. These are needed
@@ -799,7 +801,7 @@ if (window.HTMLDialogElement === undefined) {
799801
*/
800802
document.addEventListener('click', function(ev) {
801803
dialogPolyfill.formSubmitter = null;
802-
dialogPolyfill.useValue = null;
804+
dialogPolyfill.imagemapUseValue = null;
803805
if (ev.defaultPrevented) { return; } // e.g. a submit which prevents default submission
804806

805807
var target = /** @type {Element} */ (ev.target);
@@ -813,7 +815,7 @@ if (window.HTMLDialogElement === undefined) {
813815
if (!valid) {
814816
if (!(target.localName === 'input' && target.type === 'image')) { return; }
815817
// this is a <input type="image">, which can submit forms
816-
dialogPolyfill.useValue = ev.offsetX + ',' + ev.offsetY;
818+
dialogPolyfill.imagemapUseValue = ev.offsetX + ',' + ev.offsetY;
817819
}
818820

819821
var dialog = findNearestDialog(target);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dialog-polyfill",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"description": "Polyfill for the dialog element",
55
"main": "dist/dialog-polyfill.js",
66
"module": "dist/dialog-polyfill.esm.js",

0 commit comments

Comments
 (0)