Closed (fixed)
Project:
Linkit
Version:
6.1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
22 Feb 2024 at 13:40 UTC
Updated:
21 Mar 2024 at 23:59 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
seutje commentedAttached patch provides simple matcher called "External" that does a str_starts_with($string, 'www') check and suggests adding "https://"
It does not currently provide any tests for it, though.
Comment #3
mark_fullmerThis idea seems very reasonable as an enhancement for content editors who aren't experts in HTML/HTTP, and doesn't seem like it would add much complexity to the functionality. I'll review further!
Comment #4
ricksta commentedThe patch works fine for 6.1.x. I tested in the CKEditor and as a link field in Basic page content type.
Comment #6
mark_fullmerI'm wondering whether the current implementation, which will only suggest external URLs if the string starts with "www," could be broadened to cover more scenarios. (What if someone types "example.com"?)
What about the following logic?
1. If the string starts with
httpor contains://, do not suggest anything.2. If, on the other hand, after prepending
https://to the string, the result validates as a URL, suggest addinghttps://This proposal is captured in the merge request.
It would also be good to add test coverage...
Comment #8
mark_fullmerTest coverage added! From the test coverage, here's the list of strings that will/will not be eligible for this matcher, using the updated logic proposed in the previous comment:
(Note that tests are currently failing on HEAD with Drupal 10.2.3, due to #3350477: Linkit for Link Field: "rel" and "target" settings are removed for internal links; that is unrelated to this proposed code change.)
Comment #9
seutje commentedI first looked into using filter_var, but the main reason I used a simple str_starts_with is because I couldn't get filter_var to ignore the protocol, but I guess slapping the protocol in front and then matching is a much better approach.
The current approach does however fail with unicode TLDs like Brahmic, Cyrillic, Arabic, Hebrew, Chinese, Greek, etc. (see https://onlinephp.io/c/7d21f extracted from https://en.wikipedia.org/wiki/Internationalized_domain_name), but I guess those URLs are unlikely to start with "www" anyway.
Comment #10
mark_fullmerYes, I'd read about that shortcoming of PHP's filter_var, too. I might suggest we leave that as a potential enhancement, rather than work on logic to handle unicode TLDs here. We could add a
@todoinline to document the limitation?Comment #11
seutje commentedOh yeah, I didn't mean it as a blocker, I was just trying to justify my arguably poor implementation 😅
This is already a great improvement and I don't see anything that would warrant blocking it.
Comment #13
mark_fullmer