From 4f5bab823726a4bca212ac42a8e4fdebf3d3aa2a Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Fri, 8 May 2020 20:57:22 +0200 Subject: [PATCH] Exported MS Edge polyfill --- src/TextAnnotator.jsx | 3 ++- src/utils/MSEdgePolyfills.js | 30 +++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/TextAnnotator.jsx b/src/TextAnnotator.jsx index ae7f32b..8a5550d 100644 --- a/src/TextAnnotator.jsx +++ b/src/TextAnnotator.jsx @@ -4,8 +4,9 @@ import Highlighter from './highlighter/Highlighter'; import SelectionHandler from './selection/SelectionHandler'; import RelationsLayer from './relations/RelationsLayer'; import RelationEditor from './relations/editor/RelationEditor'; +import { addPolyfills } from './utils/MSEdgePolyfills'; -import './utils/MSEdgePolyfills'; +addPolyfills(); // For Microsoft Edge /** * Pulls the strings between the annotation highlight layer diff --git a/src/utils/MSEdgePolyfills.js b/src/utils/MSEdgePolyfills.js index d349b88..4d7d508 100644 --- a/src/utils/MSEdgePolyfills.js +++ b/src/utils/MSEdgePolyfills.js @@ -1,16 +1,20 @@ -if (!Element.prototype.matches) { - Element.prototype.matches = Element.prototype.msMatchesSelector || - Element.prototype.webkitMatchesSelector; -} +export const addPolyfills = () => { -if (!Element.prototype.closest) { - Element.prototype.closest = function(s) { - let el = this; + if (!Element.prototype.matches) { + Element.prototype.matches = Element.prototype.msMatchesSelector || + Element.prototype.webkitMatchesSelector; + } + + if (!Element.prototype.closest) { + Element.prototype.closest = function(s) { + let el = this; + + do { + if (Element.prototype.matches.call(el, s)) return el; + el = el.parentElement || el.parentNode; + } while (el !== null && el.nodeType === 1); + return null; + }; + } - do { - if (Element.prototype.matches.call(el, s)) return el; - el = el.parentElement || el.parentNode; - } while (el !== null && el.nodeType === 1); - return null; - }; }