makeSafeForPresentation static method
Implementation
static String makeSafeForPresentation(String unclean,
int maxCharactersToConsider, double ellipsizeDip, int flags) {
bool onlyKeepFirstLine = ((flags & SAFE_STRING_FLAG_FIRST_LINE) != 0);
bool forceSingleLine = ((flags & SAFE_STRING_FLAG_SINGLE_LINE) != 0);
bool trim = ((flags & SAFE_STRING_FLAG_TRIM) != 0);
String shortString;
if (maxCharactersToConsider > 0) {
shortString = unclean.substring(
0, Math.min(unclean.length, maxCharactersToConsider));
} else {
shortString = unclean;
}
// Treat string as HTML. This
// - converts HTML symbols: e.g. ß -> ß
// - applies some HTML tags: e.g. <br> -> \n
// - removes invalid characters such as \b
// - removes html styling, such as <b>
// - applies html formatting: e.g. a<p>b</p>c -> a\n\nb\n\nc
// - replaces some html tags by "object replacement" markers: <img> -> \ufffc
// - Removes leading white space
// - Removes all trailing white space beside a single space
// - Collapses double white space
_StringWithRemovedChars gettingCleaned =
_StringWithRemovedChars((shortString));
int firstNonWhiteSpace = -1;
int firstTrailingWhiteSpace = -1;
// Remove new lines (if requested) and control characters.
int uncleanLength = gettingCleaned.length();
for (int offset = 0; offset < uncleanLength;) {
int codePoint = gettingCleaned.codePointAt(offset);
// int type = String.fromCharCode(codePoint);
int codePointLen = String.fromCharCode(codePoint).length;
bool isNewline = _isNewline(codePoint);
if (onlyKeepFirstLine && isNewline) {
gettingCleaned.removeAllCharAfter(offset);
break;
} else if (forceSingleLine && isNewline) {
gettingCleaned.removeRange(offset, offset + codePointLen);
}
// else if (type == Character.CONTROL && !isNewline) {
// gettingCleaned.removeRange(offset, offset + codePointLen);
// }
else if (trim && !_isWhiteSpace(codePoint)) {
// This is only executed if the code point is not removed
if (firstNonWhiteSpace == -1) {
firstNonWhiteSpace = offset;
}
firstTrailingWhiteSpace = offset + codePointLen;
}
offset += codePointLen;
}
if (trim) {
// Remove leading and trailing white space
if (firstNonWhiteSpace == -1) {
// No non whitespace found, remove all
gettingCleaned.removeAllCharAfter(0);
} else {
if (firstNonWhiteSpace > 0) {
gettingCleaned.removeAllCharBefore(firstNonWhiteSpace);
}
if (firstTrailingWhiteSpace < uncleanLength) {
gettingCleaned.removeAllCharAfter(firstTrailingWhiteSpace);
}
}
}
return gettingCleaned.toString();
}