From fe338a540a2578ed7c8bd83a83aa12888eaa6b2b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 9 Mar 2018 21:39:10 +0100 Subject: [PATCH] make unicode detector work on labels --- .../utils/IrregularUnicodeDetector.java | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java index 5599358c9..8e097af16 100644 --- a/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java +++ b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java @@ -86,16 +86,14 @@ public class IrregularUnicodeDetector { builder.append('@'); } if (jid.getDomain() != null) { - int i = jid.getDomain().lastIndexOf('.'); - if (i != -1) { - String second = jid.getDomain().substring(0, i); - String top = jid.getDomain().substring(i, jid.getDomain().length()); - SpannableString secondSpannableString = new SpannableString(second); - colorize(secondSpannableString, patternTuple.domain, color); - builder.append(secondSpannableString); - builder.append(top); - } else { - builder.append(jid.getDomain()); + String[] labels = jid.getDomain().split("\\."); + for (int i = 0; i < labels.length; ++i) { + SpannableString spannableString = new SpannableString(labels[i]); + colorize(spannableString, patternTuple.domain.get(i), color); + if (i != 0) { + builder.append('.'); + } + builder.append(spannableString); } } if (builder.length() != 0 && jid.getResource() != null) { @@ -217,9 +215,9 @@ public class IrregularUnicodeDetector { private static class PatternTuple { private final Pattern local; - private final Pattern domain; + private final List domain; - private PatternTuple(Pattern local, Pattern domain) { + private PatternTuple(Pattern local, List domain) { this.local = local; this.domain = domain; } @@ -232,19 +230,13 @@ public class IrregularUnicodeDetector { localPattern = null; } String domain = jid.getDomain(); - final Pattern domainPattern; + final List domainPatterns = new ArrayList<>(); if (domain != null) { - int i = domain.lastIndexOf('.'); - if (i != -1) { - String secondLevel = domain.substring(0, i); - domainPattern = create(findIrregularCodePoints(secondLevel)); - } else { - domainPattern = null; + for (String label : domain.split("\\.")) { + domainPatterns.add(create(findIrregularCodePoints(label))); } - } else { - domainPattern = null; } - return new PatternTuple(localPattern, domainPattern); + return new PatternTuple(localPattern, domainPatterns); } } }