jorgemanrubia / truncato

A tool for truncating HTML strings efficiently
MIT License
59 stars 17 forks source link

Max length to determine the resulting string #22

Open synth opened 2 years ago

synth commented 2 years ago

We have noticed that Truncato does not truncate so the final string results in max length, but rather max length calculates on the original string. This means the final result is affected by the quality of the markup. This can be seen with two content blocks that are identical in content, but different in markup.

Truncato

str1 = "Hello, How is the weather"
str2 = "Hello, <div style=\"color: #green,background-color:#yellow;overflow:auto\">How is the weather"

[36] pry(main)> Truncato.truncate(str1, max_length: 22)
=> "Hello, How is the weat..."
[37] pry(main)> Truncato.truncate(str2, max_length: 22)
=> "Hello, <div style='color: #green,background-color:#yellow;overflow:auto'>...</div>"

For addititional reference, there is a js function that truncates and preserves markup and results in this behavior of resulting in a string of a desired content length (without markup).

truncation.js

str1 = "Hello, How is the weather"
str2 = "Hello, <div style=\"color: #green,background-color:#yellow;overflow:auto\">How is the weather"
truncate(str1, 22)
'Hello, How is the weat...'
truncate(str2, 22)
'Hello, <div style="color: #green,background-color:#yellow;overflow:auto">How is the weat...</div>'