Add a more complex truncate method #1413

merged 2 commits into from Sep 23, 2012


None yet

2 participants


This method allows specification of a given length of plain text embedded in HTML of unlimited length when truncating. This offers a more complex option than the truncate method which allows specification of total length (number of character) of either plain text or html+plaintext.

It also corrects some minor problems in the string.truncate method.

@pasamio pasamio and 1 other commented on an outdated diff Jul 28, 2012
@@ -35,6 +35,11 @@
public static function truncate($text, $length = 0, $noSplit = true, $allowHtml = true)
+ // Assume a lone open tag is invalid HTML.
+ if ($length == 1 && substr($text, 0, 1) == '<')
pasamio Jul 28, 2012

If $length == 1 then surely you can just do $text == '<' without the substr?

elinw Jul 29, 2012

Basically if you are truncating to one character when you have a string longer than one character you have some problems no matter what because we requre a ... to indicate the truncation, but in this case we are just saying look you have html here and you only want one character, we're just going to call that truncated totally and be done. The real problem is if you just truncate to a <. You could make an argument that if someone starts of with an and they truncate to one character (which becomes ...) that we should include all of the html. We could do it but it seems extremely edge case.

elinw Jul 29, 2012

Actually ...
If you look at the test at line 364 of the test changes you will see that in truncateComplex if there is only html, such as an image tag, we will return that even with a length of 1 whereas in truncate normal you should get either nothing in the mode that does not allow html which should then be turned to ... or ... in the mode that does allow html because the $length doesn't give you room to close the tag (you really must have at least 5 characters to do something with valid html and that's pushing it since I don't know that there is a one letter self closing tag <? /> ). So what was happening was when truncate was given this scenario it was doing the truncation (at line 55/60) and then seeing there was no > in the truncated string so it was returning the < as a character. I'm proposing here that we make the assumption the < in the first position of a 1 character string is an opening tag and handle it as such.

I was really not intending to do a rewrite of truncate itself but I found there are some issues with short lengths. Also we're going to have to look at its assumptions again in light of some of the new tags from html5.


Length is the maximum length allowed and then we want to know if the first character of the string (of unknown length) to be truncated to one character is an open tag because if it is that causes some difficulty.

@elinw elinw closed this Aug 9, 2012
@elinw elinw reopened this Aug 10, 2012

Actually technically the ellipsis is one character even though we are creating it with 3.

@pasamio pasamio merged commit b3cc2f5 into joomla:staging Sep 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment