Problem/Motivation
When the Additional Link CSS Classes, Additional MailTo CSS Classes and Additional Tel CSS Classes settings are all filled in, mailto links end up with the classes of all three link types. Tel links are not affected.
This makes it impossible to style external, mailto and tel links separately by their additional classes. For example, mailto links pick up the external link styling.
There are two causes in js/extlink.js:
- The additional tel classes are applied to
mailtoLinksinstead oftelLinks:
if (hasAdditionalTelClasses) { Drupal.extlink.applyClassAndSpan( mailtoLinks, drupalSettings.data.extlink.extAdditionalTelClasses, null, ); } - The additional classes are applied in a second
applyClassAndSpan()pass, with the additional class itself asclassName. In that passclassNamematches neithermailtoClassnortelClass, so the if/else chain inapplyClassAndSpan()falls through to the last branch and adds the Additional Link CSS Classes as well:
} else if (drupalSettings.data.extlink.extAdditionalLinkClasses) { // Is regular external link: linksToProcess[i].classList.add( drupalSettings.data.extlink.extAdditionalLinkClasses, ); }
Tel links are only spared because of the first bug: they never get the second pass.
Steps to reproduce
- Go to
/admin/config/user-interface/extlink. - Set Additional Link CSS Classes to
external-link, Additional MailTo CSS Classes tomailto-linkand Additional Tel CSS Classes totel-link, and enable Place an icon next to image links. Save. - Create a page containing
<a href="mailto:test@example.com">Mail</a>and<a href="tel:0123456789">Phone</a>. - View the page and inspect both links.
Expected: the mailto link has mailto mailto-link, and the tel link has tel tel-link.
Actual: the mailto link has mailto mailto-link external-link tel-link, and the tel link has tel tel-link.
Note: with Place an icon next to image links disabled, the bug is hidden. The icon appended in the first pass excludes the mailto/tel links from the additional classes pass. The bug also shows when the mailto/tel icons are disabled, since the links then have no icon.
Proposed resolution
- Pass
telLinksinstead ofmailtoLinkswhen applying the additional tel classes. - Only fall back to the Additional Link CSS Classes in
applyClassAndSpan()when processing external links, i.e. whenclassName === 'ext'.Drupal.extlink.attach()always passes'ext'for external links, regardless of the extlink_class setting.
An alternative would be to drop the separate "additional classes" passes altogether, since the first pass already adds the additional mailto and tel classes through the if/else chain.
Remaining tasks
- Review the merge request.
- Add test coverage for the additional CSS classes per link type.
User interface changes
None.
API changes
None.
Data model changes
None.
AI was used in the drafting of this issue summary, to help review for clarity, clean up language, and check grammar.
Issue fork extlink-3631097
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
joshuaramaekersAdded a note and changed the second step to reproduce.
Comment #4
joshuaramaekersOpened a merge request with the fix and test coverage. testExtlinkAdditionalCssClasses() now checks that each link type only receives its own additional classes, both with and without "Place an icon next to image links". It fails without the fix and passes with it.
Comment #5
smustgrave commentedNot sure I fully see the need for className === 'ext' mind explaining
Also summary and MR description are reading like AI generated if so please review https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquett...
Comment #6
smustgrave commentedThe first part is correct definitely a bug
Comment #7
joshuaramaekersAdded AI disclosure for issue summary.
Comment #8
joshuaramaekersComment #10
smustgrave commentedThanks!
Just fyi the policy has updated so just for the future the base template should be filled in with your own words and any AI info can be prepended to a dedicated section.
Merging
Thanks for contributing