From f2f5508144c419a85490a3e326830f8accaea04c Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 15 Dec 2018 19:21:14 -0800 Subject: [PATCH] perf: only run getSelection() check if we have to (#817) --- src/routes/_utils/delegate.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/routes/_utils/delegate.js b/src/routes/_utils/delegate.js index 892bab1..7b2cc91 100644 --- a/src/routes/_utils/delegate.js +++ b/src/routes/_utils/delegate.js @@ -1,7 +1,5 @@ // Delegate certain events to the global document for perf purposes. -import { mark, stop } from './marks' - const callbacks = {} if (process.browser && process.env.NODE_ENV !== 'production') { @@ -14,14 +12,6 @@ function onEvent (e) { // we're not interested in any non-click or non-Enter events return } - if (type === 'click') { - let selection = window.getSelection() - let selectionStr = selection && selection.toString() - if (selectionStr && selectionStr.length && target.contains(selection.anchorNode)) { - return // ignore if the user is selecting text inside the clickable area - } - } - mark('delegate onEvent') let key let element = target while (element) { @@ -31,9 +21,15 @@ function onEvent (e) { element = element.parentElement } if (key && callbacks[key]) { + if (type === 'click') { + let selection = window.getSelection() + let selectionStr = selection && selection.toString() + if (selectionStr && selectionStr.length && target.contains(selection.anchorNode)) { + return // ignore if the user is selecting text inside the clickable area + } + } callbacks[key](e) } - stop('delegate onEvent') } export function registerClickDelegates (component, delegates) {