File tree Expand file tree Collapse file tree 3 files changed +19
-9
lines changed Expand file tree Collapse file tree 3 files changed +19
-9
lines changed Original file line number Diff line number Diff line change 1- // for performance considerations, it only uses performant syntax
1+ import { isDocumentFragmentOrElementNode } from '../utils/dom.js' ;
22
3+ // for performance considerations, it only uses performant syntax
34function attachDirAuto ( el ) {
45 if ( el . type !== 'hidden' &&
56 el . type !== 'checkbox' &&
@@ -18,7 +19,7 @@ export function initDirAuto() {
1819 const len = mutation . addedNodes . length ;
1920 for ( let i = 0 ; i < len ; i ++ ) {
2021 const addedNode = mutation . addedNodes [ i ] ;
21- if ( addedNode . nodeType !== Node . ELEMENT_NODE && addedNode . nodeType !== Node . DOCUMENT_FRAGMENT_NODE ) continue ;
22+ if ( ! isDocumentFragmentOrElementNode ( addedNode ) ) continue ;
2223 if ( addedNode . nodeName === 'INPUT' || addedNode . nodeName === 'TEXTAREA' ) attachDirAuto ( addedNode ) ;
2324 const children = addedNode . querySelectorAll ( 'input, textarea' ) ;
2425 const len = children . length ;
Original file line number Diff line number Diff line change 11import tippy , { followCursor } from 'tippy.js' ;
2+ import { isDocumentFragmentOrElementNode } from '../utils/dom.js' ;
23
34const visibleInstances = new Set ( ) ;
45
@@ -136,8 +137,6 @@ function attachChildrenLazyTooltip(target) {
136137 }
137138}
138139
139- const elementNodeTypes = new Set ( [ Node . ELEMENT_NODE , Node . DOCUMENT_FRAGMENT_NODE ] ) ;
140-
141140export function initGlobalTooltips ( ) {
142141 // use MutationObserver to detect new "data-tooltip-content" elements added to the DOM, or attributes changed
143142 const observerConnect = ( observer ) => observer . observe ( document , {
@@ -152,11 +151,10 @@ export function initGlobalTooltips() {
152151 if ( mutation . type === 'childList' ) {
153152 // mainly for Vue components and AJAX rendered elements
154153 for ( const el of mutation . addedNodes ) {
155- if ( elementNodeTypes . has ( el . nodeType ) ) {
156- attachChildrenLazyTooltip ( el ) ;
157- if ( el . hasAttribute ( 'data-tooltip-content' ) ) {
158- attachLazyTooltip ( el ) ;
159- }
154+ if ( ! isDocumentFragmentOrElementNode ( el ) ) continue ;
155+ attachChildrenLazyTooltip ( el ) ;
156+ if ( el . hasAttribute ( 'data-tooltip-content' ) ) {
157+ attachLazyTooltip ( el ) ;
160158 }
161159 }
162160 } else if ( mutation . type === 'attributes' ) {
Original file line number Diff line number Diff line change @@ -59,6 +59,17 @@ export function onDomReady(cb) {
5959 }
6060}
6161
62+ // checks whether an element is owned by the current document, and whether it is a document fragment or element node
63+ // if it is, it means it is a "normal" element managed by us, which can be modified safely.
64+ export function isDocumentFragmentOrElementNode ( el ) {
65+ try {
66+ return el . ownerDocument === document && el . nodeType === Node . ELEMENT_NODE || el . nodeType === Node . DOCUMENT_FRAGMENT_NODE ;
67+ } catch {
68+ // in case the el is not in the same origin, then the access to nodeType would fail
69+ return false ;
70+ }
71+ }
72+
6273// autosize a textarea to fit content. Based on
6374// https://github.com/github/textarea-autosize
6475// ---------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments