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. 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.</p>
I am getting a teaser trimmed:
Dr.
I think the problem is . , 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 ' '=> 1 here, but no works.
| Comment | File | Size | Author |
|---|
Issue fork drupal-2835615
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
edysmpComment #3
edysmpComment #4
darrenwh commentedI 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
Comment #14
smustgrave commentedConfirmed this is still relevant in D9.5
Will need an issue summary update when a solution is agreed upon.
Comment #15
smustgrave commentedThis 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.
Comment #16
smustgrave commentedSo 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
Comment #17
smustgrave commentedComment #20
smustgrave commentedComment #27
smustgrave commentedClosed #1482178: text_summary does not break on word boundaries if the first sentence is longer than the length of the summary being produced as duplicate moving over credit.
Comment #28
rosk0I 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_trimmedformatter is that it breaks works when trimming body field with HTML. We have no 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.
Comment #30
2dareis2do commentedI 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
Not seen 。before
adding '.' => 0, works for me. e.g.
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
Comment #34
smustgrave commentedComment #36
smustgrave commentedComment #37
smustgrave commentedComment #38
dcam commentedI'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?
Comment #39
smustgrave commentedUpdated the steps, let me know if it still doesn't show.
Comment #40
dcam commentedThank 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),
, because CKEditor inserts those after a sentence's full stop. I became concerned about two things: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. 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 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.It resulted in this trimmed display value (with a 300 character trim length) :
So my question is: why was
' '=> 6,added to the breakpoints array instead of'. '=> 6,? That could help ensure that the breakpoint is added after a sentence. I tested it on my local.Comment #41
smustgrave commentedI applied the feedback as it makes sense and test seems to still pass.
Comment #42
dcam commentedWhat 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.
Comment #43
smustgrave commentedAlso good call made the test update
Comment #44
dcam commentedAll right, thank you for considering my feedback. I have nothing further to comment on. It looks good to me.
Comment #45
quietone commentedSetting work for re-enabling cspell. I updated credit while here as well.
Comment #46
quietone commentedI 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.
Comment #47
dcam commentedThat reorganization of the test looks good. The changes make sense for ensuring cspell is checking things that ought to be checked.
Comment #49
godotislateI 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.
Comment #50
smustgrave commentedThing the title is accurate enough as it does appear abbreviations like Dr. are triggering this.
Tweaked the summary some
Comment #51
godotislateComment #52
quietone commentedThe 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.
Comment #53
godotislateComment #56
godotislateCommitted and pushed bffaae1 to main and 000c505 to 11.x. Thanks!
Consulting other committers on whether to backport to 11.4.
Comment #57
smustgrave commentedwas about to it but let me know!
Comment #58
godotislateConsulted 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.