sebastienros/fluid

truncatewords - truncate indicator is always added, even if shorter

Closed this issue · 10 comments

If i use truncatewords in one of my OC liquid templates, the 3 dots are always visible, even if the text is much shorter.
image

This happend to me, when i used it like this truncatewords: 40, but the 3 dots are also shown on a text with 20 words or something like that.
image

Changing the indicator to a custom one also doesn't change that behavior.

Let me double check

If you check below everything seems as expected

[Theory]
[InlineData("The cat came back the very next day", 4, "The cat came back...")]
[InlineData("The cat came back the very next day", 1, "The...")]
[InlineData("The cat came back the very next day", 0, "...")]
[InlineData("The cat came back", 10, "The cat came back...")]
public void TruncateWords(string input, int size, string output)
{
var source = new StringValue(input);
var arguments = new FilterArguments()
.Add(NumberValue.Create(size));
var context = new TemplateContext();
var result = StringFilters.TruncateWords(source, arguments, context);
Assert.Equal(output, result.Result.ToStringValue());
}

Could you please break the test to ensure there's a bug

@hishamco I'm not quite sure if i'm missing something here, but your last test in line 303 doesn't make much sense to me or seems wrong.
You truncate to 10 words, the example is way shorter, still you have the dots.
That's pretty much exactly the problem i have.
The text is shorter then the truncate size, but still gets the dots or whatever string will be defined for that.

Also in my tests with the exact same values, the spaces get removed automatically.
As you can see, i also showed the problem with the example in the last line of my tests.
Backend:
image

Frontend:
image

OC Version: 1.4.0

The bug is obvious, and we need to check the behavior of when the number of words matches exactly by looking at Shopify 's tests.

Ya, I notice the bug, also I might check with Liquid the case that Seb mentioned

FYI

{{ "The cat came back the very next day" | truncatewords : 4 }} -> The cat came back...
{{ "The cat came back the very next day" | truncatewords : 8 }} -> The cat came back the very next day...
{{ "The cat came back the very next day" | truncatewords : 12 }} -> The cat came back the very next day

tested in https://liquidjs.com/playground.html

I will push a PR soon ..

@sebastienros I checked truncate the logic seems right

@hishamco Thanks a lot!