File tree 3 files changed +19
-9
lines changed
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' ;
2
2
3
+ // for performance considerations, it only uses performant syntax
3
4
function attachDirAuto ( el ) {
4
5
if ( el . type !== 'hidden' &&
5
6
el . type !== 'checkbox' &&
@@ -18,7 +19,7 @@ export function initDirAuto() {
18
19
const len = mutation . addedNodes . length ;
19
20
for ( let i = 0 ; i < len ; i ++ ) {
20
21
const addedNode = mutation . addedNodes [ i ] ;
21
- if ( addedNode . nodeType !== Node . ELEMENT_NODE && addedNode . nodeType !== Node . DOCUMENT_FRAGMENT_NODE ) continue ;
22
+ if ( ! isDocumentFragmentOrElementNode ( addedNode ) ) continue ;
22
23
if ( addedNode . nodeName === 'INPUT' || addedNode . nodeName === 'TEXTAREA' ) attachDirAuto ( addedNode ) ;
23
24
const children = addedNode . querySelectorAll ( 'input, textarea' ) ;
24
25
const len = children . length ;
Original file line number Diff line number Diff line change 1
1
import tippy , { followCursor } from 'tippy.js' ;
2
+ import { isDocumentFragmentOrElementNode } from '../utils/dom.js' ;
2
3
3
4
const visibleInstances = new Set ( ) ;
4
5
@@ -136,8 +137,6 @@ function attachChildrenLazyTooltip(target) {
136
137
}
137
138
}
138
139
139
- const elementNodeTypes = new Set ( [ Node . ELEMENT_NODE , Node . DOCUMENT_FRAGMENT_NODE ] ) ;
140
-
141
140
export function initGlobalTooltips ( ) {
142
141
// use MutationObserver to detect new "data-tooltip-content" elements added to the DOM, or attributes changed
143
142
const observerConnect = ( observer ) => observer . observe ( document , {
@@ -152,11 +151,10 @@ export function initGlobalTooltips() {
152
151
if ( mutation . type === 'childList' ) {
153
152
// mainly for Vue components and AJAX rendered elements
154
153
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 ) ;
160
158
}
161
159
}
162
160
} else if ( mutation . type === 'attributes' ) {
Original file line number Diff line number Diff line change @@ -59,6 +59,17 @@ export function onDomReady(cb) {
59
59
}
60
60
}
61
61
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
+
62
73
// autosize a textarea to fit content. Based on
63
74
// https://github.com/github/textarea-autosize
64
75
// ---------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments