Strip tags from formatted text before trimming

Created on 10 September 2024, 6 months ago

Problem/Motivation

The default 'Summary or trimmed' formatter specifically includes HTML tags in the character count, which is not very intuitive. The trim count should exclude HTML tags, which Smart Trim module โ†’ does.

This is explicitly tested for in https://git.drupalcode.org/project/drupal/-/blob/HEAD/core/modules/text/...

Steps to reproduce

Install Drupal using the Standard install
Modify the article teaser display to trim at 100 characters
Create an article with the following markup as the body: <p><a href="https://wwww.drupal.org/"><em><span><strong>The maximum number of characters used in the trimmed version of a post. Drupal will use this setting to determine at which offset long posts should be trimmed Note that this setting will only affect new or updated content and will not affect existing teasers.</strong></span></em></a></p>
See that the resulting teaser displays as The maximum number of characters used in the because the preceding HTML tags are counted in the 100 chars.

Proposed resolution

Strip tags before trimming.

Remaining tasks

  1. Determine the approach for this, including whether it should be a setting, since it would affect existing sites if we just change the behaviour
  2. MR with tests
  3. Review
  4. Commit

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

TBC

โœจ Feature request
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Textย  โ†’

Last updated 27 days ago

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia pameeela

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pameeela
  • First commit to issue fork.
  • Pipeline finished with Failed
    6 months ago
    Total: 218s
    #280168
  • Pipeline finished with Failed
    6 months ago
    Total: 175s
    #280176
  • Pipeline finished with Failed
    6 months ago
    Total: 482s
    #280183
  • Status changed to Needs work 6 months ago
  • Pipeline finished with Failed
    6 months ago
    Total: 507s
    #281959
  • Pipeline finished with Failed
    6 months ago
    Total: 472s
    #282096
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sriharsha.uppuluri Hyderabad

    sriharsha.uppuluri โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Success
    6 months ago
    Total: 494s
    #286026
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sriharsha.uppuluri Hyderabad

    Fixed the test case failure issue

  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Appears to be missing test cases and upgrade path.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Grevil

    Just ran into this while using metatag + glossify.

    Glossify provides filters, which automaticall create tooltips / links for words, for which a node or a taxonomy term exists. These links use the linked node body, as the tooltip, which results in a VERY long "title" HTML Tag:

    Now, if I would like to use the token [node:summary] inside a metatag or anywhere else, the summary won't be displayed correctly as you can see here:

    Now this stems from the same issue, this issue is about. But the current MR doesn't solve it deeply enough. The real root cause of this is the

    text_summary()
    

    method from the core "text" module.

    In my opinion we should strip the tags there. The method doc says the following:

    Generates a trimmed, formatted version of a text field value

    I don't think stripping HTML tags would change the documented functionality of this method.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    Thanks, I can also confirm this issue and this is definitely a bug, not a feature request. The expected behaviour is to strip the affected text.

    For example, using the Metatag module (which uses [node:summary] by default for node meta descriptions!), this bug will break the metatag generation if the shortened text has a long link in the beginning. The text will be stripped right before the link and that's it.

Production build 0.71.5 2024