Skip to content

[WebProfilerBundle] Fix event delegation on links inside toggles #59292

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

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@
}

toggle.addEventListener('click', (e) => {
const toggle = e.currentTarget;

if (e.target.closest('a, .sf-toggle') !== toggle) {
Copy link
Member

Choose a reason for hiding this comment

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

should we handle buttons inside toggles as well ?

Copy link
Contributor Author

@MatTheCat MatTheCat Dec 24, 2024

Choose a reason for hiding this comment

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

buttons and inputs could be handled the same way indeed, but it doesn't seem to be needed for now so I'm not sure 🤔

return;
}

e.preventDefault();

if ('' !== window.getSelection().toString()) {
/* Don't do anything on text selection */
return;
}

/* needed because when the toggle contains HTML contents, user can click */
/* on any of those elements instead of their parent '.sf-toggle' element */
const toggle = e.target.closest('.sf-toggle');
const element = document.querySelector(toggle.getAttribute('data-toggle-selector'));

toggle.classList.toggle('sf-toggle-on');
Expand All @@ -154,14 +157,6 @@
toggle.innerHTML = currentContent !== altContent ? altContent : originalContent;
});

/* Prevents from disallowing clicks on links inside toggles */
const toggleLinks = toggle.querySelectorAll('a');
toggleLinks.forEach((toggleLink) => {
toggleLink.addEventListener('click', (e) => {
e.stopPropagation();
});
});

toggle.setAttribute('data-processed', 'true');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,17 @@
}

addEventListener(toggles[i], 'click', function(e) {
e.preventDefault();
var toggle = e.currentTarget;

if ('' !== window.getSelection().toString()) {
/* Don't do anything on text selection */
if (e.target.closest('a, span[data-clipboard-text], .sf-toggle') !== toggle) {
return;
}

var toggle = e.target || e.srcElement;
e.preventDefault();

/* needed because when the toggle contains HTML contents, user can click */
/* on any of those elements instead of their parent '.sf-toggle' element */
while (!hasClass(toggle, 'sf-toggle')) {
toggle = toggle.parentNode;
if ('' !== window.getSelection().toString()) {
/* Don't do anything on text selection */
return;
}

var element = document.querySelector(toggle.getAttribute('data-toggle-selector'));
Expand All @@ -182,22 +180,6 @@
toggle.innerHTML = currentContent !== altContent ? altContent : originalContent;
});

/* Prevents from disallowing clicks on links inside toggles */
var toggleLinks = toggles[i].querySelectorAll('a');
for (var j = 0; j < toggleLinks.length; j++) {
addEventListener(toggleLinks[j], 'click', function(e) {
e.stopPropagation();
});
}

/* Prevents from disallowing clicks on "copy to clipboard" elements inside toggles */
var copyToClipboardElements = toggles[i].querySelectorAll('span[data-clipboard-text]');
for (var k = 0; k < copyToClipboardElements.length; k++) {
addEventListener(copyToClipboardElements[k], 'click', function(e) {
e.stopPropagation();
});
}

toggles[i].setAttribute('data-processed', 'true');
}
})();
Expand Down