Bug #82776
closedChaining f:format.crop and f:format.stripHtml does cut to early
0%
Description
If i chain a <f:format.crop ...><f:format.stripHtml>{text}</f:format.stripHtml></f:format.crop> the number of cropped (visible) chars sometimes is to low.
The problem with this is, that the stripHtml removes tags, but does not remove spaces or newlines. And the following crop does count the spaces, tabs and newlines if they are in front of the text.
For instance:
<div> <p> Some text
Output of stripHtml (space replaced with *):
\n **\n ****Some text
Depending on newline type this results in in 8 or 10 chars less than given in maxChars. I could the issue if i chain another f:spaceless viewhelper, but thats not optimal.
I dont know if it would be better if f:format.stripHtml or f:format.crop should trim the text.
Updated by Claus Due over 6 years ago
- Status changed from New to Closed
- Priority changed from Should have to Won't have this time
I'm going to take the liberty of closing this issue as "works as intended" and "won't solve", for the following reasons:
- Fluid is not (read: it can be, but not without extreme modification of each VH) aware of which nested ViewHelpers you used. It wouldn't be possible, as in your example, for f:format.crop to detect whether f:format.stripTags or any other HTML-stripping, potentially-whitespace-leaving VH was used.
- The whitespace, when it comes from a variable or VH, is not parsed by Fluid and would need to be transformed by a ViewHelper either through internal functionality of the VH, or by chaining; and chaining is the preferred way.
- It is not desirable to begin adding "trim" boolean arguments or other to each ViewHelper that may be used to receive such leading/trailing whitespace chunks. This is exactly what f:spaceless was created for (although, it will also adjust the HTML tags you used).
- Using f:spaceless is currently the preferred way to trim unwanted whitespace, and it can be used around a single variable.
Tip: you can express this as `{variableWithHtml -> f:format.stripTags() -> f:spaceless() -> f:format.crop(maxCharacters: 123)}`. While it may not be optimal (in the sense of automatically happening so you don't have to decide on it), it is an explicit way to instruct Fluid that in this case, your whitespace must be removed, and works for any combination of ViewHelpers which is why it is preferred instead of, for example, adding "trim" arguments everywhere and adding calls to `trim()`. The intention with Fluid is exactly that you can combine these formatters to apply more than one of the formattings, but you do need to tell Fluid exactly what to do.