aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-02-08 10:42:18 +0800
committerGitHub <noreply@github.com>2024-02-08 02:42:18 +0000
commitf290c24d286b996ac6b512f49a30aa5aef1a2dbe (patch)
tree076f4a29a8fa4f6ac31b91b196b807fa33d87fc6
parentb6bf8041d8e8ee845728687b1f358f1d482afff2 (diff)
downloadgitea-f290c24d286b996ac6b512f49a30aa5aef1a2dbe.tar.gz
gitea-f290c24d286b996ac6b512f49a30aa5aef1a2dbe.zip
Avoid showing unnecessary JS errors when there are elements with different origin on the page (#29081)
Try to fix #29080
-rw-r--r--web_src/js/modules/dirauto.js5
-rw-r--r--web_src/js/modules/tippy.js12
-rw-r--r--web_src/js/utils/dom.js11
3 files changed, 19 insertions, 9 deletions
diff --git a/web_src/js/modules/dirauto.js b/web_src/js/modules/dirauto.js
index c917bf8cff..cd90f8155b 100644
--- a/web_src/js/modules/dirauto.js
+++ b/web_src/js/modules/dirauto.js
@@ -1,5 +1,6 @@
-// for performance considerations, it only uses performant syntax
+import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
+// for performance considerations, it only uses performant syntax
function attachDirAuto(el) {
if (el.type !== 'hidden' &&
el.type !== 'checkbox' &&
@@ -18,7 +19,7 @@ export function initDirAuto() {
const len = mutation.addedNodes.length;
for (let i = 0; i < len; i++) {
const addedNode = mutation.addedNodes[i];
- if (addedNode.nodeType !== Node.ELEMENT_NODE && addedNode.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) continue;
+ if (!isDocumentFragmentOrElementNode(addedNode)) continue;
if (addedNode.nodeName === 'INPUT' || addedNode.nodeName === 'TEXTAREA') attachDirAuto(addedNode);
const children = addedNode.querySelectorAll('input, textarea');
const len = children.length;
diff --git a/web_src/js/modules/tippy.js b/web_src/js/modules/tippy.js
index 7f8423e319..27f371fd88 100644
--- a/web_src/js/modules/tippy.js
+++ b/web_src/js/modules/tippy.js
@@ -1,4 +1,5 @@
import tippy, {followCursor} from 'tippy.js';
+import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
const visibleInstances = new Set();
@@ -136,8 +137,6 @@ function attachChildrenLazyTooltip(target) {
}
}
-const elementNodeTypes = new Set([Node.ELEMENT_NODE, Node.DOCUMENT_FRAGMENT_NODE]);
-
export function initGlobalTooltips() {
// use MutationObserver to detect new "data-tooltip-content" elements added to the DOM, or attributes changed
const observerConnect = (observer) => observer.observe(document, {
@@ -152,11 +151,10 @@ export function initGlobalTooltips() {
if (mutation.type === 'childList') {
// mainly for Vue components and AJAX rendered elements
for (const el of mutation.addedNodes) {
- if (elementNodeTypes.has(el.nodeType)) {
- attachChildrenLazyTooltip(el);
- if (el.hasAttribute('data-tooltip-content')) {
- attachLazyTooltip(el);
- }
+ if (!isDocumentFragmentOrElementNode(el)) continue;
+ attachChildrenLazyTooltip(el);
+ if (el.hasAttribute('data-tooltip-content')) {
+ attachLazyTooltip(el);
}
}
} else if (mutation.type === 'attributes') {
diff --git a/web_src/js/utils/dom.js b/web_src/js/utils/dom.js
index 64a6a5affc..4dc55a518a 100644
--- a/web_src/js/utils/dom.js
+++ b/web_src/js/utils/dom.js
@@ -59,6 +59,17 @@ export function onDomReady(cb) {
}
}
+// checks whether an element is owned by the current document, and whether it is a document fragment or element node
+// if it is, it means it is a "normal" element managed by us, which can be modified safely.
+export function isDocumentFragmentOrElementNode(el) {
+ try {
+ return el.ownerDocument === document && el.nodeType === Node.ELEMENT_NODE || el.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
+ } catch {
+ // in case the el is not in the same origin, then the access to nodeType would fail
+ return false;
+ }
+}
+
// autosize a textarea to fit content. Based on
// https://github.com/github/textarea-autosize
// ---------------------------------------------------------------------