Truncate must not count HTML comments

Created on 10 October 2017, over 6 years ago
Updated 8 July 2023, 12 months ago

Problem/Motivation

The TruncateHTML class includes commented characters when counting characters, resulting in way too short outputs if HTML comments are used. This happens easily when using Twig debug, which adds 3 to n lines of comments for theme hooks, suggestions etc. which causes outputs to differ between testing/development (using debug) and live systems (not using debug).

Proposed resolution

When trimming to a specified character count, ignore HTML comments.

πŸ› Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany ckaotik Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    I'm happy to give this a proper review, but I'd like to add some test coverage for this change. I'm happy to add the test coverage, but I'd like some assistance in making sure we are testing the right stuff. I'm fine with both traditional code comments and the

    <!--break-->

    tag being part of this single issue as long as there is proper code coverage for both.

    Our Unit tests allow us to test both trimming by characters and words. The input data for the character trimming test looks like this:

    - The string to be trimmed.
    - Number of characters to be trimmed.
    - Suffix.
    - Expected output.

    So, as an example,

    - This is the string to be trimmed. 
    - 15
    - ...
    - This is the string...

    Similarly, for the word trimming test, the input data is:

    - The string to be trimmed.
    - Number of words to be trimmed.
    - Suffix.
    - Expected output.

    It would be great for someone to provide some input data for the tests in a comment below (or add it to the patch!) that includes both twig_debug style code comments and a

    <!--break-->

    tag.

    Make sense?

    thanks,
    -mike

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Forgot to change the status...

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    Patch Failed to Apply
  • πŸ‡©πŸ‡ͺGermany Ruedische

    New patch for 2.1.x added.

  • πŸ‡ΊπŸ‡ΈUnited States markie Albuquerque, NM
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    I agree with @eelkeblok that we should spin the

    <!--break-->

    stuff off into a separate issue. I have done so: πŸ“Œ Properly handle HTML comment Active .

    Moving forward, this issue will utilize the issue fork (no more patches, please) and focus on non-break HTML comments only.

    I have updated the issue title to reflect this change.

  • @ultimike opened merge request.
  • Assigned to markie
  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Fix and tests ready for review in issue fork. All tests are passing.

    I took a slightly different approach than the patches from @ckaotik and @Ruedische - the code I added to the service methods removes all comment elements from the DOM before our trimming code gets going.

    -mike

  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Thinking about this while I was offline (I know, right?) I realized that the DOM comment remover wasn't going to remove all comment elements. I've improved it and added more realistic tests.

    Ready for review.

    -mike

  • Assigned to ultimike
  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Assigning this back to me. Also, I'll look into adding a unit test specifically for the new removeHtmlComments() method.

    -mike

  • Assigned to markie
  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    @markie,

    I've moved removeHtmlComments() as suggested and added a new unit test specifically for it.

    -mike

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States markie Albuquerque, NM

    See comments in MR.

  • Status changed to Needs review 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA
  • Status changed to Fixed 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States markie Albuquerque, NM

    reviewed and merged. Thanks for the assistance.

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

Production build 0.69.0 2024