Problem/Motivation

Our clients often add external links without a protocol (like www.example.com instead of https://www.example.com) leading to it being considered a link to the same domain resulting in a 404 (like https://website.com/www.example.com). It would be nice to give them a reminder and option to click to add a protocol to the link.

Proposed resolution

Provide a matcher to detect links that start with "www" and are missing a protocol.

Feel free to close if you don't think this is appropriate for linkit to provide. It does not currently provide any tests for it.

CommentFileSizeAuthor
#2 3423212-1.patch1.28 KBseutje

Issue fork linkit-3423212

Command icon 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

seutje created an issue. See original summary.

seutje’s picture

Issue summary: View changes
Status: Active » Needs work
StatusFileSize
new1.28 KB

Attached 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.

mark_fullmer’s picture

This 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!

ricksta’s picture

Status: Needs work » Reviewed & tested by the community

The patch works fine for 6.1.x. I tested in the CKEditor and as a link field in Basic page content type.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 3423212-1.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mark_fullmer’s picture

Status: Needs work » Active

I'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 http or contains ://, do not suggest anything.
2. If, on the other hand, after prepending https:// to the string, the result validates as a URL, suggest adding https://

This proposal is captured in the merge request.

It would also be good to add test coverage...

mark_fullmer’s picture

Status: Active » Needs review

Test 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:

      '/node/1' => FALSE,
      '<front>' => FALSE,
      '/sites/default/files/file.pdf' => FALSE,
      'https://drupal.org' => FALSE,
      'http://drupal.org' => FALSE,
      'ftp://drupal.org' => FALSE,
      'www.drupal.org' => TRUE,
      'drupal.org' => TRUE,
      'drupal.org/node/1' => TRUE,
      'drupal.org/node/1?query=1' => TRUE,
      'drupal.org/node/1#anchor' => TRUE,

(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.)

seutje’s picture

I 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.

mark_fullmer’s picture

The current approach does however fail with unicode TLDs like Brahmic, Cyrillic, Arabic, Hebrew, Chinese, Greek, etc.

Yes, 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 @todo inline to document the limitation?

seutje’s picture

Status: Needs review » Reviewed & tested by the community

Oh 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.

  • mark_fullmer committed d09ab3ff on 6.1.x
    Issue #3423212 by seutje: Provide matcher for external URLs missing...
mark_fullmer’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.