Verified Commit 051a1aca authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3619623 Empty FilterUrl maximum link text length setting trims to ellipsis only

By: pminf
By: chaitanyadessai
By: smustgrave
By: dcam
(cherry picked from commit 6ed53eb7)
parent f7ceda82
Loading
Loading
Loading
Loading
Loading
+5 −1
Changes for core/modules/filter/src/Plugin/Filter/FilterUrl.php: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -326,9 +326,13 @@ protected function unescapeComments(array $match): string {
   *   The shortened URL by a 'filter_url_length' setting cutoff.
   */
  protected function trimUrl(string $url): string {
    $max_length = $this->settings['filter_url_length'] ?? NULL;
    if (!isset($max_length) || $max_length === '') {
      return $url;
    }
    return Unicode::truncate(
      string: $url,
      max_length: $this->settings['filter_url_length'],
      max_length: $max_length,
      add_ellipsis: TRUE,
    );
  }
+44
Changes for core/modules/filter/tests/src/Kernel/FilterKernelTest.php: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -980,6 +980,50 @@ public function testUrlFilter(): void {
      ],
    ];
    $this->assertFilteredString($filter, $tests);

    // With no maximum link text length configured, the full URL should be
    // preserved as the anchor text instead of being truncated to an
    // ellipsis.
    $filter->setConfiguration([
      'settings' => [
        'filter_url_length' => NULL,
      ],
    ]);
    $tests = [
      'www.untrimmed.com/d/ff.ext?a=1&b=2#a1' => [
        '<a href="http://www.untrimmed.com/d/ff.ext?a=1&amp;b=2#a1">www.untrimmed.com/d/ff.ext?a=1&amp;b=2#a1</a>' => TRUE,
      ],
    ];
    $this->assertFilteredString($filter, $tests);

    // An empty string, as submitted by the settings form when the field is
    // left blank, is treated the same as an unset length.
    $filter->setConfiguration([
      'settings' => [
        'filter_url_length' => '',
      ],
    ]);
    $tests = [
      'www.untrimmed.com/d/ff.ext?a=1&b=2#a1' => [
        '<a href="http://www.untrimmed.com/d/ff.ext?a=1&amp;b=2#a1">www.untrimmed.com/d/ff.ext?a=1&amp;b=2#a1</a>' => TRUE,
      ],
    ];
    $this->assertFilteredString($filter, $tests);

    // A maximum link text length of 0 is distinct from an unset length: it
    // is passed through to Unicode::truncate(), which trims the link text,
    // and the ellipsis itself, down to nothing.
    $filter->setConfiguration([
      'settings' => [
        'filter_url_length' => 0,
      ],
    ]);
    $tests = [
      'www.trimmed-to-nothing.com' => [
        '<a href="http://www.trimmed-to-nothing.com"></a>' => TRUE,
      ],
    ];
    $this->assertFilteredString($filter, $tests);
  }

  /**