When passing to truncate_utf8() text that spans multiple lines, the function cuts the text on the first newline character if the $wordsafe flag is set to TRUE, no matter what $max_length is passed to it.

To test:

$text = <<<EOD
This is a text that spans multiple lines.
Line 2 goes here.
EOD;
dsm(truncate_utf8($text, 51, TRUE));
dsm(truncate_utf8($text, 51, FALSE));

Output:

  • This is a text that spans multiple lines.
  • This is a text that spans multiple lines.
    Line 2 go

Expected behavior:

  • This is a text that spans multiple lines.
    Line 2
  • This is a text that spans multiple lines.
    Line 2 go

The attached patch fixes this by adding the s (PCRE_DOTALL) modifier to the regular expression used.

CommentFileSizeAuthor
#20 unicode-truncate-1712106-19.patch1.67 KBm4olivei
#9 unicode-truncate-1712106-9-FAIL.patch904 bytesdawehner
FAILED: [[SimpleTest]]: [MySQL] 63,538 pass(es), 2 fail(s), and 0 exception(s). View
#9 unicode-truncate-1712106-9.patch1.77 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 63,708 pass(es). View
#8 drupal8-cut_on_the_first_line_wordsafe_true-1712106-8.patch913 bytesInternetDevels
PASSED: [[SimpleTest]]: [MySQL] 63,680 pass(es). View
#6 truncate_utf8-wordsafe-newlines-1712106-6.patch794 bytesBoobaa
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch truncate_utf8-wordsafe-newlines-1712106-6.patch. Unable to apply patch. See the log in the details link for more information. View
#3 truncate_utf8-wordsafe-newlines-1712106-3.patch913 bytesJacobSanford
PASSED: [[SimpleTest]]: [MySQL] 57,033 pass(es). View
#2 truncate_utf8-wordsafe-newlines-1712106-2.patch814 bytesjamix
PASSED: [[SimpleTest]]: [MySQL] 53,078 pass(es). View
truncate_utf8-wordsafe-newlines.patch794 bytesjamix
PASSED: [[SimpleTest]]: [MySQL] 39,365 pass(es). View
Members fund testing for the Drupal project. Drupal Association Learn more

Comments

Feng-Shui’s picture

Attached patched works for me, thanks.

jamix’s picture

Version: 7.x-dev » 8.x-dev
FileSize
814 bytes
PASSED: [[SimpleTest]]: [MySQL] 53,078 pass(es). View

Re-rolling against 8.x-dev.

JacobSanford’s picture

FileSize
913 bytes
PASSED: [[SimpleTest]]: [MySQL] 57,033 pass(es). View

Re-roll to reflect core changes.

gaellafond’s picture

I just found this issue. Any idea when it will be fixed in the Core?

yce’s picture

Thanks, attached patch works for me too. (7.x)

Boobaa’s picture

Issue summary: View changes
FileSize
794 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch truncate_utf8-wordsafe-newlines-1712106-6.patch. Unable to apply patch. See the log in the details link for more information. View

Attached is an updated patch against 7.26 (with offset changes only).

Status: Needs review » Needs work

The last submitted patch, 6: truncate_utf8-wordsafe-newlines-1712106-6.patch, failed testing.

InternetDevels’s picture

Status: Needs work » Needs review
FileSize
913 bytes
PASSED: [[SimpleTest]]: [MySQL] 63,680 pass(es). View

Interdiff is empty, because function truncate_utf8 moved to Unicode::truncate.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.77 KB
PASSED: [[SimpleTest]]: [MySQL] 63,708 pass(es). View
904 bytes
FAILED: [[SimpleTest]]: [MySQL] 63,538 pass(es), 2 fail(s), and 0 exception(s). View

The code looked fine, though there was clearly no test so I went ahead and used the issue summary to create one

dawehner’s picture

Status: Reviewed & tested by the community » Needs review

ups

Status: Needs review » Needs work

The last submitted patch, 9: unicode-truncate-1712106-9-FAIL.patch, failed testing.

xmacinfo’s picture

Has this been fixed for Drupal 7?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.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.

m4olivei’s picture

Status: Needs work » Reviewed & tested by the community

Just came across this. Patch still works against 8.4.x.

The last submitted patch, 9: unicode-truncate-1712106-9.patch, failed testing. View results

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: unicode-truncate-1712106-9-FAIL.patch, failed testing. View results

m4olivei’s picture

Status: Needs work » Needs review
FileSize
1.67 KB

Alright, I guess not. Here's a re-roll.

m4olivei’s picture

Status: Needs review » Reviewed & tested by the community

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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed a9685a928d to 8.6.x and fd2d6c259d to 8.5.x. Thanks!

diff --git a/core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php b/core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
index 0d09688bdd..8076f0cb30 100644
--- a/core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
@@ -376,7 +376,7 @@ public function testTruncate($text, $max_length, $expected, $wordsafe = FALSE, $
    *     - (optional) Boolean for the $add_ellipsis flag. Defaults to FALSE.
    */
   public function providerTruncate() {
-    return [
+    $tests = [
       ['frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'],
       ['frànçAIS is über-åwesome', 23, 'frànçAIS is über-åwesom'],
       ['frànçAIS is über-åwesome', 17, 'frànçAIS is über-'],
@@ -421,13 +421,25 @@ public function providerTruncate() {
       ['Help! Help! Help!', 4, 'Hel…', TRUE, TRUE],
       ['Help! Help! Help!', 3, 'He…', TRUE, TRUE],
       ['Help! Help! Help!', 2, 'H…', TRUE, TRUE],
-      ['This is a text that spans multiple lines.
-Line 2 goes here.', 51, 'This is a text that spans multiple lines.
-Line 2', TRUE],
-      ['This is a text that spans multiple lines.
-Line 2 goes here.', 51, 'This is a text that spans multiple lines.
-Line 2 go', FALSE],
     ];
+
+    // Test truncate on text with muliplte lines.
+    $multi_line = <<<EOF
+This is a text that spans multiple lines.
+Line 2 goes here.
+EOF;
+    $multi_line_wordsafe = <<<EOF
+This is a text that spans multiple lines.
+Line 2
+EOF;
+    $multi_line_non_wordsafe = <<<EOF
+This is a text that spans multiple lines.
+Line 2 go
+EOF;
+    $tests[] = [$multi_line, 51, $multi_line_wordsafe, TRUE];
+    $tests[] = [$multi_line, 51, $multi_line_non_wordsafe, FALSE];
+
+    return $tests;
   }
 
   /**

I re-wrote the test data to be inline with coding standards. I ran the test locally and proved it failed as expected.

  • alexpott committed a9685a9 on 8.6.x
    Issue #1712106 by jamix, dawehner, m4olivei, Boobaa, InternetDevels,...

  • alexpott committed fd2d6c2 on 8.5.x
    Issue #1712106 by jamix, dawehner, m4olivei, Boobaa, InternetDevels,...

  • alexpott committed e455822 on 8.6.x
    Issue #1712106: Fix spelling mistake introduced on commit
    

  • alexpott committed 9a8c545 on 8.5.x
    Issue #1712106: Fix spelling mistake introduced on commit
    
    (cherry...

Status: Fixed » Closed (fixed)

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