Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid showing unnecessary JS errors when there are elements with different origin on the page #29081

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions web_src/js/modules/dirauto.js
Original file line number Diff line number Diff line change
@@ -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' &&
Expand All @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions web_src/js/modules/tippy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tippy, {followCursor} from 'tippy.js';
import {isDocumentFragmentOrElementNode} from '../utils/dom.js';

const visibleInstances = new Set();

Expand Down Expand Up @@ -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, {
Expand All @@ -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') {
Expand Down
9 changes: 9 additions & 0 deletions web_src/js/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export function onDomReady(cb) {
}
}

export function isDocumentFragmentOrElementNode(el) {
try {
return 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;
}
}

Copy link
Member

@silverwind silverwind Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we detect the iframe document via el.ownerDocument !== document and ignore those? I don't think we want to run the mutation callbacks on any iframe content (mermaid and user-configured renderers are the only cases I know of).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 6299b57 , and added more comments

// autosize a textarea to fit content. Based on
// https://github.com/github/textarea-autosize
// ---------------------------------------------------------------------
Expand Down
Loading