Avoid NumberFormatException in Html.getHtmlColor()

This commit is contained in:
seven332 committed 2019-01-05 12:38:43 +08:00
1 parent 9dd39412d4
commit a0b786b2a6
1 file changed
+12 -7
+12 -7
View File
@@ -713,11 +713,13 @@ class HtmlToSpannedConverter implements ContentHandler {
where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
int c = getHtmlColor(f.mColor);
if (c != -1) {
try {
int c = getHtmlColor(f.mColor);
text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (IllegalArgumentException e) {
// Ignore
}
}
}
@@ -906,16 +908,19 @@ class HtmlToSpannedConverter implements ContentHandler {
*/
@ColorInt
public static int getHtmlColor(@NonNull String colorString) {
if (colorString.charAt(0) == '#') {
if ((colorString.length() == 7 || colorString.length() == 9) && colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
long color;
try {
color = Long.parseLong(colorString.substring(1), 16);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Unknown color: " + colorString);
}
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color: " + colorString);
}
return (int)color;
return (int) color;
} else if (colorString.startsWith("rgb(") && colorString.endsWith(")")) {
String str = colorString.substring(4, colorString.length() - 1);
String[] colors = str.split("[\\s]*,[\\s]*");