Skip to content

Commit 3bd0573

Browse files
committed
make EmscriptenRunner compatible w/ a canvas in shadow DOM
1 parent b14b0aa commit 3bd0573

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

loader.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,25 @@ window.Module = null;
12351235
var self = this;
12361236
this._canvas = canvas;
12371237
this._hooks = { start: [], reset: [] };
1238+
1239+
// If passed in canvas is in a shadow root, Emscripten's later findEventTarget('#canvas')
1240+
// won't find it via document queries — patch both to fall back to shadow root.
1241+
if (canvas.getRootNode && canvas.getRootNode() !== document) {
1242+
var _root = canvas.getRootNode();
1243+
var _origGetById = document.getElementById.bind(document);
1244+
var _origQS = document.querySelector.bind(document);
1245+
document.getElementById = function(id) {
1246+
return _origGetById(id) || _root.querySelector('#' + id);
1247+
};
1248+
document.querySelector = function(sel) {
1249+
return _origQS(sel) || _root.querySelector(sel);
1250+
};
1251+
this._restoreDocQueries = function() {
1252+
document.getElementById = _origGetById;
1253+
document.querySelector = _origQS;
1254+
};
1255+
}
1256+
12381257
// This is somewhat wrong, because our Emscripten-based emulators
12391258
// are currently compiled to start immediately when their js file
12401259
// is loaded.
@@ -1273,6 +1292,7 @@ window.Module = null;
12731292
};
12741293

12751294
EmscriptenRunner.prototype.stop = function () {
1295+
if (this._restoreDocQueries) this._restoreDocQueries();
12761296
};
12771297

12781298
var mute_protection = function() {

0 commit comments

Comments
 (0)