Skip to content

Commit 8dc5d05

Browse files
authored
Avoid try/catch in findEventTarget. NFC (#22984)
Rather than using try/catch to detect when `document` is missing we explicitly check for it. This is the same pattern used in the other version of `findEventTarget` on line 336. This avoids casting a wide net and hiding errors in the rest of the code in this function. Its also more consistent with the other version of `findEventTarget`.
1 parent eff9671 commit 8dc5d05

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/library_html5.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ var LibraryHTML5 = {
332332
return cString > 2 ? UTF8ToString(cString) : cString;
333333
},
334334

335+
// Find a DOM element with the given ID, or null if none is found.
335336
$findEventTarget__deps: ['$maybeCStringToJsString', '$specialHTMLTargets'],
336337
$findEventTarget: (target) => {
337338
target = maybeCStringToJsString(target);
338339
#if ENVIRONMENT_MAY_BE_WORKER || ENVIRONMENT_MAY_BE_NODE
339-
var domElement = specialHTMLTargets[target] || (typeof document != 'undefined' ? document.querySelector(target) : undefined);
340+
var domElement = specialHTMLTargets[target] || (typeof document != 'undefined' ? document.querySelector(target) : null);
340341
#else
341342
var domElement = specialHTMLTargets[target] || document.querySelector(target);
342343
#endif
@@ -375,28 +376,28 @@ var LibraryHTML5 = {
375376
#endif
376377

377378
#else
378-
// Find a DOM element with the given ID.
379+
// Find a DOM element with the given ID, or null if none is found.
379380
$findEventTarget__deps: ['$specialHTMLTargets'],
380381
$findEventTarget: (target) => {
381382
#if ASSERTIONS
382383
warnOnce('Rules for selecting event targets in HTML5 API are changing: instead of using document.getElementById() that only can refer to elements by their DOM ID, new event target selection mechanism uses the more flexible function document.querySelector() that can look up element names, classes, and complex CSS selectors. Build with -sDISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR to change to the new lookup rules. See https://github.com/emscripten-core/emscripten/pull/7977 for more details.');
383384
#endif
384-
try {
385-
// The sensible "default" target varies between events, but use window as the default
386-
// since DOM events mostly can default to that. Specific callback registrations
387-
// override their own defaults.
388-
if (!target) return window;
389-
if (typeof target == "number") target = specialHTMLTargets[target] || UTF8ToString(target);
390-
if (target === '#window') return window;
391-
else if (target === '#document') return document;
392-
else if (target === '#screen') return screen;
393-
else if (target === '#canvas') return Module['canvas'];
394-
return (typeof target == 'string') ? document.getElementById(target) : target;
395-
} catch(e) {
396-
// In Web Workers, some objects above, such as '#document' do not exist. Gracefully
397-
// return null for them.
398-
return null;
399-
}
385+
// The sensible "default" target varies between events, but use window as the default
386+
// since DOM events mostly can default to that. Specific callback registrations
387+
// override their own defaults.
388+
if (!target) return window;
389+
if (typeof target == "number") target = specialHTMLTargets[target] || UTF8ToString(target);
390+
if (target === '#window') return window;
391+
else if (target === '#document') return document;
392+
else if (target === '#screen') return screen;
393+
else if (target === '#canvas') return Module['canvas'];
394+
else if (typeof target == 'string')
395+
#if ENVIRONMENT_MAY_BE_WORKER || ENVIRONMENT_MAY_BE_NODE
396+
return (typeof document != 'undefined') ? document.getElementById(target) : null;
397+
#else
398+
return document.getElementById(target);
399+
#endif
400+
return target;
400401
},
401402

402403
// Like findEventTarget, but looks for OffscreenCanvas elements first

0 commit comments

Comments
 (0)