The text_summary() function has an optional parameter to pass in the text format of the text whose summary is being calculated. If this is passed in, the function will do all sorts of special processing depending on which (core) filters happen to be in the text format.

This entire functionality makes no sense and should be removed.

The reason is that when text_summary() is called, check_markup() has already been run on the text that is passed in to it. (See the code in text_field_formatter_view() for evidence of this.) Thus, it does not matter what filters were used to generate it, because they've already run, so in the end we are just working with regular HTML regardless.

In particular, going through the text_summary() code:

  1.   // We check for the presence of the PHP evaluator filter in the current
      // format. If the body contains PHP code, we do not split it up to prevent
      // parse errors.
      if (isset($format)) {
        $filters = filter_list_format($format);
        if (isset($filters['php_code']) && $filters['php_code']->status && strpos($text, '<?') !== FALSE) {
          return $text;
        }
      }
    

    This makes no sense. Any PHP code will have already been evaluated when check_markup() was called. If for some reason there is a '<?' left over, it does not indicate anything meaningful anymore and should not trigger any specific kind of behavior.

  2.   // If no complete paragraph then treat line breaks as paragraphs.
      $line_breaks = array('<br />' => 6, '<br>' => 4);
      // Newline only indicates a line break if line break converter
      // filter is present.
      if (isset($filters['filter_autop'])) {
        $line_breaks["\n"] = 1;
      }
    

    Also makes no sense. If the line break filter is present, newlines will have already been converted to <br />. If it isn't, we don't want to treat them specially at all. (See also #1235062: text_summary() ignores filter status which shows that this code is being run even when the line break filter isn't present, which can break things even more.)

  3.   // If the htmlcorrector filter is present, apply it to the generated summary.
      if (isset($filters['filter_htmlcorrector'])) {
        $summary = _filter_htmlcorrector($summary);
      }
    

    This one almost makes sense, but not really. We do want to run the HTML corrector, but it shouldn't matter whether or not the text format used to generate the content had it. If text_summary() break things that would otherwise have been valid HTML, it is responsible for fixing what it broke. Example: If I type "<p>This is a test</p>" into a textarea, regardless of whether or not that textarea had the HTML corrector run on it to guarantee that the output is valid, the fact that I have provided it with valid HTML means it should stay valid. If the generated summary comes out as invalid HTML such as "<p>This is a" with no closing tag, that is a bug, and text_summary() should never let that happen.

    So the conclusion is that text_summary() should run the HTML corrector if the text it winds up with contains any HTML at all. One might be concerned about the performance impact of that, but:

Patch coming up in a second.

Comments

David_Rothstein’s picture

Status: Active » Needs review
StatusFileSize
new14.07 KB
new5.68 KB

The first patch contains new tests only, which should fail. (They are sort of testing #1235062: text_summary() ignores filter status more than this issue directly, but the two issues are very closely related.)

The second patch contains the complete set of changes, and the tests should hopefully pass.

effulgentsia’s picture

Issue tags: +Needs backport to D7

+1. Patch looks good to me, but should have at least one other reviewer before RTBC.

Changing the function signature is not backportable to D7, but some of the other cleanup here is, and IMO, should be, so tagging accordingly.

sun’s picture

Component: field system » text.module
sun’s picture

Title: text_summary() should not check for specific filters, because it makes no sense to do so and can break things » text_summary() checks for specific filters, but it should be agnostic to filters
Issue tags: -Needs backport to D7

Happy to discuss possible options here.

My personal conclusion was to replace it entirely with a DOMDocument-based implementation; i.e., diving into the actual HTML markup and counting the actual text nodes contained within.

The only tidbit that would still be format/filter-specific is the check for the PHP code filter — cutting/trimming PHP code would result in a fatal error.

That said, #1235062: text_summary() ignores filter status can be fixed and backported, but whatever we're going to do here cannot be backported.

David_Rothstein’s picture

It looks like you're not aware of #221257: text_summary() should output valid HTML and Unicode text, and not count markup characters as part of the text length ?

I actually linked that issue to this one in #221257-179: text_summary() should output valid HTML and Unicode text :) Does it affect anything here though? (It's been a while since I looked at this issue.)

The only tidbit that would still be format/filter-specific is the check for the PHP code filter — cutting/trimming PHP code would result in a fatal error.

I don't see why, since as described above check_markup() has already run and executed any PHP code before this. I checked the current D8 code and it still seems to be the same. text_summary() is called via this code:

  $output = _text_sanitize($this->instance, $langcode, $item, 'value');
  $output = text_summary($output, $this->instance['settings']['text_processing'] ? $item['format'] : NULL, $this->getSetting('trim_length'));

And _text_sanitize() is basically a simple wrapper around check_markup().

The last submitted patch, 1: text-summary-1347920-1.patch, failed testing.

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
David Hernández’s picture

Assigned: Unassigned » David Hernández

I'm working on this

David Hernández’s picture

Assigned: David Hernández » Unassigned

This patch is too old. Is this issue still valid?

jhedstrom’s picture

The php filter stuff is gone, but from what I can tell, points 2 and 3 in the issue summary are still present in the text_summary() function.

thijsvdanker’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new6.51 KB

I am using the text_format as an api function to make a summary of some html text, and then it makes sense to have the format as an argument of the function.

Point 3 still stands: the summary function can mess up perfectly valid html, so it should always fix this (regardless of format settings).

This patch always normalizes the html at the end of the summary and I've updated the test to reflect this.

Status: Needs review » Needs work

The last submitted patch, 12: text_summary_checks-1347920-12.patch, failed testing.

thijsvdanker’s picture

Status: Needs work » Needs review
StatusFileSize
new7.22 KB

The failing test tests if a formatter is applied to a field.
$this->assertEqual(strlen(html_entity_decode($rendered_field)), 3);

It expected a string with <p>8randomchars</p>/n to be trimmed to 3 charactars (<p)
As the patch fixes this html, it returns <p></p>
I've changed the test to test for 7 characters (<p></p>).

mgifford’s picture

StatusFileSize
new7.22 KB

Re-uploading last patch for the bots.

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.

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

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

The code in question has gone through several changes since this patch. Can you verify it's still an issue on 9.5?

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Closing as outdated since there has not been a follow up in 7 months.