Skip to content

Commit f290c24

Browse files
authored
Avoid showing unnecessary JS errors when there are elements with different origin on the page (#29081)
Try to fix #29080
1 parent b6bf804 commit f290c24

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

web_src/js/modules/dirauto.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
34
function 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;

web_src/js/modules/tippy.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import tippy, {followCursor} from 'tippy.js';
2+
import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
23

34
const 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-
141140
export 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') {

web_src/js/utils/dom.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)