-
-
Save acreeger/1572249 to your computer and use it in GitHub Desktop.
Grails truncate tag with abbr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String ELLIPSIS = '...' | |
def truncate = { attrs, body -> | |
def maxLength = attrs.maxlength | |
if (maxLength == null || !maxLength.isInteger() || maxLength.toInteger() <= 0) { | |
throw new Exception("The attribute 'maxlength' must an integer greater than 3. Provided value: $maxLength") | |
} else { | |
maxLength = maxLength.toInteger() | |
} | |
if (maxLength <= ELLIPSIS.size()) { | |
throw new Exception("The attribute 'maxlength' must be greater than 3. Provided value: $maxLength") | |
} | |
if (body().length() > maxLength) { | |
out << /<abbr title="${body().encodeAsHTML()}">${body()[0..maxLength - (ELLIPSIS.size() + 1)].encodeAsHTML()}$ELLIPSIS<\/abbr>/ | |
} else { | |
out << body().encodeAsHTML() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def "truncate works for text longer than max length"() { | |
setup: | |
def fullText = ("D" * 2) + "&" + ("D" * 12) | |
def maxLength = "10" | |
when: | |
tagLib.truncate(maxlength:maxLength, {fullText}) | |
then: | |
tagLib.out.toString() == createdTruncatedAbbr(fullText,maxLength) | |
} | |
def "truncate works for text shorter than or equal to max length"() { | |
setup: | |
def fullText = ("D" * 2) + "&" + ("D" * 4) //length 7 | |
when: | |
tagLib.truncate(maxlength:maxLength.toString(), {fullText}) | |
then: | |
tagLib.out.toString() == fullText.encodeAsHTML() | |
where: | |
maxLength << [7,8,9,10,11,12] | |
} | |
def "truncate errors when maxlength is less than or equal the length of the ellipsis"() { | |
def fullText = "D" * 7 | |
when: | |
tagLib.truncate(maxlength:maxLength, {fullText}) | |
then: | |
def e = thrown(Exception) | |
e.message == "The attribute 'maxlength' must be greater than 3. Provided value: $maxLength" | |
where: | |
maxLength << ["2","3"] | |
} | |
def "truncate throws an error when maxlength is less than or equal to zero or not an integer"() { | |
def fullText = "D" * 7 | |
when: | |
tagLib.truncate(maxlength:maxLength, {fullText}) | |
then: | |
def e = thrown(Exception) | |
e.message == "The attribute 'maxlength' must an integer greater than 3. Provided value: $maxLength" | |
where: | |
maxLength << ["0","-5","blah"] | |
} | |
def createdTruncatedAbbr(fullText, maxlength) { | |
if (maxlength in String) { | |
maxlength = maxlength.toInteger() | |
} | |
def tag = /<abbr title="${fullText.encodeAsHTML()}">${fullText[0..maxlength - '...'.size() - 1].encodeAsHTML()}...<\/abbr>/ | |
return tag | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment