arendjr / text-clipper

Fast and correct clip functions for HTML and plain text
MIT License
133 stars 13 forks source link

Clipping HTML has unexpected results #7

Closed cloudratha closed 5 years ago

cloudratha commented 5 years ago

Been writing a few tests and getting unexpected results with the following:

  1. When using a length that is less or equal to the indicator length:

    clip('<p>one <a href="#">two - three <br>four</a> five</p>', 0, {
    html: true,
    breakWords: true,
    indicator: '...'
    });
    // output - <p>one <a href=\"#\">two - three <br>four</a> five<...

    The expected output I imagine would be <p>...</p>

  2. When the last character in the node is a space, but has remaining tags:

    clip('<p>one <a href="#">two - three <br>four</a> five</p>', 6, {
    html: true,
    breakWords: true,
    indicator: '...'
    });
    // output - <p>one </p>

    The expected output - <p>one...</p>

Please let me know if this is a bug or if it's actually expected behaviour.

arendjr commented 5 years ago

Great observations and thanks for reporting! I've fixed both.

Please note the expected output for the first example is actually the empty string, as there is no indicator at a line-break, and the start of a paragraph does count like that.

arendjr commented 5 years ago

Fixed in 1.2.4.