Skip to content

Commit

Permalink
add a shadow-friendly contains fn
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Jan 22, 2018
1 parent b732f68 commit b27ccb9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let debug = logger('quill:events');

const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
const EMITTERS = [];
const supportsRootNode = ('getRootNode' in document);

EVENTS.forEach(function(eventName) {
document.addEventListener(eventName, (...args) => {
Expand All @@ -30,9 +31,24 @@ class Emitter extends EventEmitter {

handleDOM(event, ...args) {
const target = (event.composedPath ? event.composedPath()[0] : event.target);
const containsNode = (node, target) => {
if (!supportsRootNode || target.getRootNode() === document) {
return node.contains(target);
}

while (!node.contains(target)) {
const root = target.getRootNode();
if (!root || !root.host) {
return false;
}
target = root.host;
}

return true;
};

(this.listeners[event.type] || []).forEach(function({ node, handler }) {
if (target === node || node.contains(target)) {
if (target === node || containsNode(node, target)) {
handler(event, ...args);
}
});
Expand Down

0 comments on commit b27ccb9

Please sign in to comment.