Problem/Motivation

Using TextSummary::generate() on text that has periods followed by   can result in summary text that is cut off at other periods (followed by white space) before the trim length is met. These can be undesirable especially if the period is meant to be used as an abbreviation (such as "Dr.") instead of the end of a sentence.

While abbreviations vs sentences can not be detected, periods followed by   should also be considered candidates for trim break points.

Steps to reproduce

Create a content type with a text formatted field (since content types don't ship with main anymore)
On the display set it to trimmed at 300 character
Create a node of that type using the example

Dr. Cras ultricies ligula sed magna dictum porta. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla porttitor accumsan tincidunt. Cras ultricies ligula sed magna dictum porta. Nulla quis lorem ut libero malesuada feugiat. Proin eget tortor risus. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Proin eget tortor risus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Vivamus suscipit tortor eget felis porttitor volutpat. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Curabitur aliquet quam id dui posuere blandit. Curabitur aliquet quam id dui posuere blandit. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Pellentesque in ipsum id orci porta dapibus.

Notice the display cuts off at Dr.

Proposed resolution

Add ' '=> 6, to the $break_points array

Remaining tasks

Figure out a solution
Write tests
Review

User interface changes

NA

API changes

NA

Data model changes

NA

Release notes snippet

NA

Original Post

I have a text in the body field trimmed to 600 characters in the tesaer view:
<p>Dr. Cras ultricies ligula sed magna dictum porta.&nbsp;Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.&nbsp;Pellentesque in ipsum id orci porta dapibus.&nbsp;Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.&nbsp;Nulla porttitor accumsan tincidunt.&nbsp;Cras ultricies ligula sed magna dictum porta.&nbsp;Nulla quis lorem ut libero malesuada feugiat.&nbsp;Proin eget tortor risus.&nbsp;Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.&nbsp;Proin eget tortor risus.&nbsp;Praesent sapien massa, convallis a pellentesque nec, egestas non nisi.&nbsp;Vivamus suscipit tortor eget felis porttitor volutpat. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Curabitur aliquet quam id dui posuere blandit. Curabitur aliquet quam id dui posuere blandit. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Pellentesque in ipsum id orci porta dapibus.</p>

I am getting a teaser trimmed:
Dr.

I think the problem is .&nbsp;, if I removed this, the trimmed is fine, there is a drupal solution for not removed all the html code space in my site?

I try fixed adding '&nbsp;'=> 1 here, but no works.

Issue fork drupal-2835615

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

edysmp created an issue. See original summary.

edysmp’s picture

Issue summary: View changes
edysmp’s picture

Issue summary: View changes
darrenwh’s picture

I can replicate this issue, the system is trimming up to the period(.) if this is moved further into the sentence the trimming happens after the period

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Version: 9.4.x-dev » 9.5.x-dev
Issue tags: +Bug Smash Initiative, +Needs issue summary update

Confirmed this is still relevant in D9.5

Will need an issue summary update when a solution is agreed upon.

smustgrave’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

This doesn't seem to be related to the html character nbsp but that it's one paragraph and the code uses the period in doctor as a breakpoint.

Not sure best way to avoid certain periods, like Dr, Mr, Mrs, but apply at end of sentences.

smustgrave’s picture

So one contrib module that may be worth mentioning is https://www.drupal.org/project/smart_trim

Also playing around with the function there’s a line
$rpos = strpos($reversed, strrev($point));
if I replace as
$rpos = strpos($summary, strrev($point)); OR
$rpos = strpos($reversed, $point);
it works then too but I’m sure using reversed as for a reason

smustgrave’s picture

Status: Active » Needs review
StatusFileSize
new2.89 KB
new3.59 KB

The last submitted patch, 17: 2835615-17-tests-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 17: 2835615-17.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new2.64 KB
new4.09 KB

Status: Needs review » Needs work

The last submitted patch, 20: 2835615-20.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave credited chx.

rosk0’s picture

I don't think it's fair to call #1482178: text_summary does not break on word boundaries if the first sentence is longer than the length of the summary being produced duplicate of this one. To me it feels like they are talking about related, but different things.

The problem we see with text_trimmed formatter is that it breaks works when trimming body field with HTML. We have no &nbsp; there mostly.

Tested the patch from #20 and it haven't changed anything for us - we still see broken words at the end of the field output where the text was trimmed.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

2dareis2do’s picture

I have a similar issue where when importing content. if space does not follow then period then it will be be not be trimmed as described. This took a little while for me to try and debug but as you can see from the screenshots appear like a bug.

e.g. // If the first paragraph is too long, split at the end of a sentence.

So in text.module you have

  // If the first paragraph is too long, split at the end of a sentence.
  $break_points[] = ['. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1];

Not seen 。before

adding '.' => 0, works for me. e.g.

  $break_points[] = ['. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '.' => 0, '؟ ' => 1];

For my purposes I can change to following, but the dr. problem would still exist.

e.g.

Please see screen shots attached.

Add separate issue for this https://www.drupal.org/project/drupal/issues/3424444

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

smustgrave’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Needs work
Issue tags: +Needs steps to reproduce

I'm sorry, but I couldn't reproduce the issue before cloning the MR. The text was trimmed at ~600 characters, not at the "Dr." abbreviation. I'm not sure what the issue is, but notably the steps to reproduce the problem are out of date since a text-with-summary field has to be set up by a tester. Could we have some updated steps to reproduce, please?

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs steps to reproduce

Updated the steps, let me know if it still doesn't show.

dcam’s picture

Title: Trimmed Formatter no work with html code space. » TextTrimmedFormatter may trim at abbreviations
Issue summary: View changes
Status: Needs review » Needs work

Thank you. That did help. After following the updated steps I was able to reproduce the issue. I'm not exactly certain where I went wrong, but I know that previously I left the trimming at the default 600 characters.

I performed manual testing with the MR applied. The intent behind the change is that the formatter should trim at non-breaking spaces (NBSP), &nbsp;, because CKEditor inserts those after a sentence's full stop. I became concerned about two things:

  1. Are there other scenarios in which CKEditor inserts an NBSP?
  2. What happens if a content editor manually inserts an NBSP?

I searched, but couldn't find an answer to the first question about whether CKEditor inserts them in other scenarios. But I tested the second question myself. I had to edit the sample text from the issue summary slightly due to the positioning of full stops near the 300 character breakpoint, but after a little work I was able to produce text containing a manually inserted NBSP that resulted in trimming not at a sentence boundary. The sample text was this:
Dr. Cras ultricies ligula sed magna dictum porta.&nbsp;Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.&nbsp;Pellentesque in ipsum id orci porta dapibus.&nbsp;Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.&nbsp;Nulla porttitor.&nbsp;Cras ultricies&nbsp;ligula sed magna dictum porta.&nbsp;Nulla quis lorem ut libero malesuada feugiat.&nbsp;Proin eget tortor risus.&nbsp;Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.&nbsp;Proin eget tortor risus.&nbsp;Praesent sapien massa, convallis a pellentesque nec, egestas non nisi.&nbsp;Vivamus suscipit tortor eget felis porttitor volutpat. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Curabitur aliquet quam id dui posuere blandit. Curabitur aliquet quam id dui posuere blandit. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Pellentesque in ipsum id orci porta dapibus.

It resulted in this trimmed display value (with a 300 character trim length) :

Dr. Cras ultricies ligula sed magna dictum porta. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla porttitor. Cras ultricies

So my question is: why was '&nbsp;'=> 6, added to the breakpoints array instead of '.&nbsp;'=> 6,? That could help ensure that the breakpoint is added after a sentence. I tested it on my local.

smustgrave’s picture

Status: Needs work » Needs review

I applied the feedback as it makes sense and test seems to still pass.

dcam’s picture

What do you think about changing the text in the test to the examples I gave in #40? That way we ensure there's no regression to that fix.

smustgrave’s picture

Also good call made the test update

dcam’s picture

Status: Needs review » Reviewed & tested by the community

All right, thank you for considering my feedback. I have nothing further to comment on. It looks good to me.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Setting work for re-enabling cspell. I updated credit while here as well.

quietone’s picture

Status: Needs work » Needs review

I decided to move things around a bit in the test so that only one pair of cspell disable/enable is needed.

The failing test is a core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5Test.php, which I am assuming is unrelated and a random one. I will restart the tests.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

That reorganization of the test looks good. The changes make sense for ensuring cspell is checking things that ought to be checked.

godotislate made their first commit to this issue’s fork.

godotislate’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

I think the issue title and IS might be inaccurate, or at least not up to date? Looks like the MR addresses situations where a period is followed by a non-breaking space, but it's still entirely possible that an abbreviated word like "Dr. " might be where text is trimmed.

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update

Thing the title is accurate enough as it does appear abbreviations like Dr. are triggering this.

Tweaked the summary some

godotislate’s picture

Title: TextTrimmedFormatter may trim at abbreviations » TextTrimmedFormatter not trimming at periods followed by non-breaking space
quietone’s picture

The only question I see here is if #1482178: text_summary does not break on word boundaries if the first sentence is longer than the length of the summary being produced is a duplicate. As I read that issue it is not a duplicate of this one. If that is the case, then that issue needs to be re-opened and credit adjusted here.

godotislate’s picture

Issue summary: View changes

  • godotislate committed 000c5058 on 11.x
    fix: #2835615 TextTrimmedFormatter not trimming at periods followed by...

  • godotislate committed bffaae17 on main
    fix: #2835615 TextTrimmedFormatter not trimming at periods followed by...
godotislate’s picture

Version: main » 11.4.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed bffaae1 to main and 000c505 to 11.x. Thanks!

Consulting other committers on whether to backport to 11.4.

smustgrave’s picture

was about to it but let me know!

godotislate’s picture

Version: 11.4.x-dev » 11.x-dev
Status: Patch (to be ported) » Fixed

Consulted with @longwave and we're thinking not to backport. It's an edge-case bug that's been around awhile, and it's possible some sites are relying on the current behavior.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.