Problem/Motivation
On the node preview screen, linked images redirect to an undefined page.
As we can see in the attached screenshot, the direct link with "Click here" is working but the link with image tag get redirecting to the node preview undefined page.
/node/preview/86c2fbcf-11e4-4a56-a4c8-93ab8f382baa/undefined
Steps to reproduce
For demonstration, I will be using a custom block set to full html format. The block has nothing to do with the issue, it is just used to quickly place the testing code onto a page.
- Add the following code to a custom block, configured to display on node pages:
<h1>Normal Link</h1> <a href="/">Test</a> <h1>Image Link</h1> <a href="/"><img src="/path/to/an/actual/image.png"></a> <h1>Child Element Link</h1> <a href="/" style="padding:20px"><span>Test 2</span></a> - Edit a node (that is not the home page)
- Click preview button
- Click the "normal link", when the modal pops up, click "leave". Observe you are correctly redirected.
- Use your browser back button to return to preview mode.
- Click the "image link", when the modal pops up, click "leave". Observe you are NOT correctly redirected.
- Use your browser back button to return to preview mode.
- Click the "child element link", specifically the padded area around the text. When the modal pops up, click "leave". Observe you are correctly redirected.
- Use your browser back button to return to preview mode.
- Click the "child element link", specifically text itself. When the modal pops up, click "leave". Observe you are NOT correctly redirected.
Proposed resolution
The issue is that the click can come from the link element or a child of it. The JS behavior is attached only to the link. When child element is clicked, event.target.href is undefined. Instead refer to event bubbling and switch to event.currentTarget.href which will always refer to the element that the behavior is attached to.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #42 | 3308432-42.patch | 504 bytes | keshavv |
Issue fork drupal-3308432
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:
Comments
Comment #2
cilefen commentedThis would require steps to reproduce to be taken on as a bug. Can you please add to the issue summary, the steps to set up this bug, starting with a standard Drupal installation?
Comment #4
cilefen commentedComment #5
cilefen commentedComment #6
devkinetic commentedI just ran into this, and the fix is pretty simple. In my case the issue was the event was being triggered by clicking on a
<span>tag within an<a>tag. For example:Imagine that the link has some padding around the span with css. If you click on the padded area around the span, node preview redirects work just fine, but if you clicked exactly on the area of the span, the span itself would end up being the event target. Currently, the JS redirects to
event.target.hrefwhich in the case of the span click, would be undefined.The fix for me was to instead use
event.currentTarget.hrefwhich will always be the link. I tested with an img tag link as well to verify the fix for the initially described issue.Comment #8
devkinetic commentedI looked into updating the tests to cover this and while I can add the new link type, it seems that the
UIHelperTrait::clickLink()method would also need to be extended to provide the option to click the element within the link vs the link itself.Comment #9
devkinetic commentedComment #10
bramdriesenI tried to understand how and why this is happening but could not figure it out. I think steps to reproduce would help and would also allow this to be marked as a bug report.
Comment #11
bnjmnmRe #8
No need to make
clickLink()click non-links, you can target the element directly and click it.Comment #12
devkinetic commentedComment #13
devkinetic commentedComment #14
bramdriesenUploading a tests only patch. The MR already demonstrates to pass all tests, but without the change the patch should fail.
Comment #15
bramdriesenIt being an actual patch will probably help 😅
Comment #17
bramdriesenLGTM in that case 😉 not a PHPUnit expert, so there might be some nits/best practices that I'm missing.
Comment #18
bramdriesenActually, I have one question.Why would an anchor link open the pop-up that you are leaving the page? There is no reason for that to be happening.The second tests passes, which is default behaviour so that's good.
And the last test with the span failed as expected.
Ignore that. I missed the
notinpageTextNotContains🙈Comment #19
bramdriesenComment #21
bramdriesenWill hide the patch so it won't re-run
Comment #22
quietone commentedTriaging the RTBC issue queue.
I read the issue summary and the comments. I have not tried to reproduce the problem.
I look at the patch and see that two new tests, testPreviewAnchorLinks and testPreviewChildElementLinks are added. Looking at the test results only one test is failing, testPreviewChildElementLinks. That make me wonder why the other test was added. That could use an explanation.
Also, since these are functional tests can it be done in a single test?
Setting to NW for the above questions.
Comment #23
devkinetic commentedThe main issue is that the javascript didn't target the bubbled event data correctly, so some types of links would fail. That was fixed, and the question came to should there be a test to cover this situation?
When I looked at the existing tests, there was one test with two asserts for normal and anchor links. Adding in this new type of link would then make that single test have 3 asserts now, but also involve navigating backwards to the previous page after the second assert.
It was more straightforward to just split each link into it's own test.
Comment #28
devkinetic commentedUpdated the issue fork and rebuilt MR based on 11.x.
Comment #29
devkinetic commentedHere is also a patch against 10.3.x for anyone who needs it.
Comment #30
smustgrave commentedHiding patches for clarity.
#22 still seems to be relevant
Instead of breaking out could we just add to the existing test, especially since they are javascript tests and require a full bootstrap.
Issue summary appears to be missing proposed solution from the standard issue template can that be added please
Comment #31
devkinetic commentedComment #32
devkinetic commentedIS updated and combined into single test.
Comment #33
devkinetic commentedComment #34
smustgrave commentedFeedback appears to be addressed.
Comment #35
quietone commentedI read the IS, comments and MR. There are no unanswered questions. My question about the tests in #22 was answered but then later the tests were combined. That seems OK in this case.
Leaving at RTBC.
Comment #39
nod_Committed and pushed bd6abf7c4d to 11.x and d890962445 to 10.4.x. Thanks!
Comment #40
nod_(edit) update credits
Comment #42
keshavv commentedAfter implementing this commit, clicking on simple links during preview mode results in an undefined redirect.
Steps to Reproduce:
1. Edit any node.
2. Click the Preview button.
3. Within the preview, click on any basic link or admin links.
4. You are redirected to: /node/preview/UUID/undefined
Comment #43
bramdriesen@keshavv this ticket is closed for almost a year. It's recommended to create a new ticket and reference to this issue. Also note that the patch workflow is deprecated, you should use merge requests.
Comment #44
norman.lolFollow-up: #3541343: "Leave preview" points to undefined instead of node's edit page