Problem/Motivation
When clicking an anchor link within CKEditor5, the browser attempts to navigate to the anchor link. If the link points to an anchor that exists on the edit page (e.g. #main-content), the browser scrolls up/down to that anchor’s position on the page.
This is not the expected behavior. When I click on a non-anchor link (be it an internal or external link), a tooltip pops up which displays the URL (which is linked), an Edit button, and an Unlink button. I can then choose to navigate to the link’s URL by clicking on the URL. This is the behavior I expect to see when clicking on an anchor link within CKEditor5. While this tooltip also appears when the link is an anchor link, the browser navigates to the anchor in addition to opening the tooltip.
See attached screen cast for a demonstration of the behavior I’m describing on a fresh Drupal 10.1 site, but I have seen this happen on a 9.5 site as well. I attempted to reproduce this on this CKEditor5 demo page and was unsuccessful, which is why I’m reporting it here.
Steps to reproduce
- Edit a page that requires you to scroll down a bit before you get to a WYSIWYG editor.
- In a CKEditor5 editor, select some text and link it to
#main-content. - Click on the linked text.
- Observe that the browser scrolls up to the
#main-contentanchor near the top of the node edit form.
Proposed resolution
Update the scope of the selector passed to the fragment link event listener in core/misc/form.es6.js to exclude links which are inside a WYSIWYG editor.
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | GTM_Settings.png | 55.51 KB | hitesh.koli |
| #22 | 3339741-after-patch.mov | 2.86 MB | nayana_mvr |
| #22 | 3339741-before-patch.mov | 1.04 MB | nayana_mvr |
| #21 | helper-text-anchor-works-normally.mov | 2.31 MB | agarzola |
| #17 | aftr_MR.mov | 2.06 MB | sonam.chaturvedi |
Issue fork drupal-3339741
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3339741-clicking-on-an
changes, plain diff MR !3486 /
changes, plain diff MR !3484
- 10.1.x
changes, plain diff MR !3485
Comments
Comment #2
agarzola commentedComment #3
agarzola commentedComment #4
wim leersWow, superb find! Looks like there's no issue for this yet upstream: https://github.com/ckeditor/ckeditor5/issues?q=is%3Aissue+link+anchor+is.... Would you mind creating it? 🙏
Comment #5
agarzola commentedI have not been able to reproduce the issue in any of their demos, actually. You can try it for yourself: https://ckeditor.com/ckeditor-5/demo/feature-rich/
That is the reason I have not reported it upstream and instead reported it here. Happy to open a ticket, though, if we’re able to reproduce it in a non-Drupal setting.
Comment #6
agarzola commentedRemoving the
[upstream]badge until we can confirm that this is reproducible upstream.Comment #7
agarzola commentedI found the culprit. It actually does not have to do with the CKEditor module at all, but rather form.js. This file (found at
core/misc/form.js) adds a click event listener to all anchor links on the page which will trigger a fragment interaction event, causing the browser to navigate to the anchor target. It targets all anchor links indiscriminately, without regard to where it might be located:I believe the solution here is to amend the selector passed in the second argument to
$().on()so that links found inside a WYSIWYG editor are excluded from the query. I will be submitting merge requests for Drupal 9.5.x-dev and 10.1.x-dev shortly.Since this doesn’t actually have anything to do with the CKEditor core module, I changed the Component field on this issue to “javascript”, though perhaps there is a more appropriate selection?
Comment #9
agarzola commentedComment #10
agarzola commentedI could use some guidance on the proper way to submit MRs against multiple branches. Should I create a child issue for the 10.1.x-dev branch and create an MR from there?
Comment #11
agarzola commentedKind friends showed me the way. I’m closing my MR, changing the target version on this issue, and submitting a new MR that targets 10.1.x.
Comment #16
agarzola commentedComment #17
sonam.chaturvedi commentedVerified and tested MR!3486 on 10.1.x-dev. Patch applied cleanly and works fine.
Test Results
1.Now user is not navigated to an anchor when a user clicks on a fragment link within a WYSIWYG editor.
2.Only tooltip is shown with the URL and now user can choose whether navigate to the link’s URL by clicking on the URL in tooltip.
Please find attached before and after MR video.
Comment #18
agarzola commentedThank you for the review @sonam.chaturvedi.
Comment #19
wim leersOh wow — so it's a regression on the Drupal core side simply because CKEditor 4 used
<iframe>s, and CKEditor 5 doesn't! 🤯What a magnificently obscure bug, and epic work shepherding this in the right direction, @agarzola! 👏
Unfortunately, I think the solution is a bit too broad, even though it is probably harmless on 99% of sites. I left a suggestion that will allow us to make it harmless on 100% of sites.
Comment #20
agarzola commentedComment #21
agarzola commentedI did some digging in an attempt to find alternative ways to limit the selector’s scope, but I found that what is proposed in the MR is not only adequate, but the correct solution.
My original concern (and I believe it’s @wimleers’s concern as well) was that perhaps other text which may potentially include one or more legitimate anchor links might get included inside of
.form-textarea-wrapper(as a sibling of thetextareaelement and WYSIWYG editor). I can only think of one such element that might be included and that is helper text, so I did a test locally and it turns out that helper text is not injected into the.form-textarea-wrappercontainer element, so it is not at risk of getting left out ofform.js’s purview with the query selector in my merge request. I have attached a new video showing this in action. The video shows my fix preventing the anchor link within the CKEditor UI from working (as intended). The field has helper text that includes an anchor link. Clicking on the anchor link correctly navigates to the anchor, as intended.Respectfully, I am marking this back as Needs review and marking the conversation as resolved, as I believe that the solution in the MR is adequate. Please let me know if this is not proper procedure to get more eyes on this.
Thanks!
Comment #22
nayana_mvr commentedVerified the MR!3486 and tested it on Drupal version 10.1.x. After applying the patch, 2 scenarios were tested :-
1. When the anchor is inside CKEditor5 - On clicking the anchor link, a tooltip pops up which displays the URL (which is linked), an Edit button, and an Unlink button. On Clicking the link, the edit page will open in a new browser navigating to the anchor’s position on the page.
2. When the anchor is outside CKEditor5( eg., help text as mentioned in #21) - On clicking the anchor link, it will navigate/ scroll to the anchor’s position on the page. Which means the patch doesn’t affect anchor links outside CKEditor5.
So I think the patch works fine and I have added the before and after screen recordings for reference. Need RTBC+1
Comment #23
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
So seems like it may not be an upstream issue.
MR does fix the issue but will need a test case.
Comment #24
richarddavies commentedI just upgraded to CKEditor 5 and I've been troubleshooting a very similar situation where clicking on some links in the editor caused the browser to instantly navigate to the link. Although my root cause was different than what is described in this original issue, I'm leaving a comment here in case it helps anyone else.
What was different in my case was that my links didn't contain an anchor and it only happened when editing certain pages on my site. It was really difficult to pinpoint exactly what conditions were causing this behavior, but I finally determined that the issue was caused by our Google Tag Manager code which tracks clicks on outbound links. We had tried to exclude GTM on our admin/editor pages, but had overlooked some so GTM was unintentionally loading when editing certain types of pages. After reconfiguring GTM to exclude those pages I can now click on the links without navigating to them.
Comment #25
agarzola commentedComment #26
markdorisonComment #27
agarzola commentedThis is still active, as I'm working getting my test to pass.
Comment #29
hitesh.koli#24 works for me.
Comment #30
hitesh.koliGTM setting for tracking only anonymous users.
Google tag > Conditions > User Role > Select `anon`
Comment #31
acbramley commentedTriaged as part of BSI.
Looks like Nightwatch tests were added, but the MR needs a reroll. I'll attempt to change the MR target and rebase.