Просмотр исходного кода

Fix getStyledText breaking when half-inside tooltips

Change-Id: Ib62376c5e493d06da8ffaccfb81b3e424a79a81b
Reviewed-on: http://gerrit.dmdirc.com/1486
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.5b1
Chris Smith 14 лет назад
Родитель
Сommit
68c1b882fd

+ 20
- 3
src/com/dmdirc/ui/messages/Styliser.java Просмотреть файл

@@ -251,6 +251,8 @@ public class Styliser implements ConfigChangeListener {
251 251
      * to be as far left as they possibly can be. This means that the start of
252 252
      * the string will include any control codes immediately preceeding the
253 253
      * desired text, and the end will not include any trailing codes.
254
+     * <p>
255
+     * This method will NOT include "internal" control codes in the output.
254 256
      *
255 257
      * @param styled The styled String to be operated on
256 258
      * @param from The starting index in the unstyled string
@@ -262,19 +264,20 @@ public class Styliser implements ConfigChangeListener {
262 264
         final String unstyled = stipControlCodes(styled);
263 265
         final String startBit = unstyled.substring(0, from);
264 266
         final String middleBit = unstyled.substring(from, to);
267
+        final String sanitised = stipInternalControlCodes(styled);
265 268
         int start = from;
266 269
 
267
-        while (!stipControlCodes(styled.substring(0, start)).equals(startBit)) {
270
+        while (!stipControlCodes(sanitised.substring(0, start)).equals(startBit)) {
268 271
             start++;
269 272
         }
270 273
 
271 274
         int end = to + start - from;
272 275
 
273
-        while (!stipControlCodes(styled.substring(start, end)).equals(middleBit)) {
276
+        while (!stipControlCodes(sanitised.substring(start, end)).equals(middleBit)) {
274 277
             end++;
275 278
         }
276 279
 
277
-        return styled.substring(start, end);
280
+        return sanitised.substring(start, end);
278 281
     }
279 282
 
280 283
     /**
@@ -352,6 +355,20 @@ public class Styliser implements ConfigChangeListener {
352 355
                 + CODE_TOOLTIP, "$1");
353 356
     }
354 357
 
358
+    /**
359
+     * St(r)ips all recognised internal control codes from the input string.
360
+     *
361
+     * @param input the String to be stripped
362
+     * @return a copy of the input with control codes removed
363
+     * @since 0.6.5
364
+     */
365
+    public static String stipInternalControlCodes(final String input) {
366
+        return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
367
+                + CODE_SMILIE + CODE_STOP + CODE_UNDERLINE + "]", "")
368
+                .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)"
369
+                + CODE_TOOLTIP, "$1");
370
+    }
371
+
355 372
     /**
356 373
      * Returns a substring of the input string such that no control codes are present
357 374
      * in the output. If the returned value isn't the same as the input, then the

+ 3
- 0
test/com/dmdirc/ui/messages/StyliserIndicesTest.java Просмотреть файл

@@ -64,6 +64,9 @@ public class StyliserIndicesTest {
64 64
                      4, 7, Styliser.CODE_COLOUR + "4,0RED"},
65 65
             {"Blah" + Styliser.CODE_COLOUR + "4,0RED blah blah",
66 66
                      5, 7, "ED"},
67
+            {Styliser.CODE_TOOLTIP + "Annoying internal tooltip text"
68
+                     + Styliser.CODE_TOOLTIP + "Blah" + Styliser.CODE_TOOLTIP + "Blah",
69
+                     2, 6, "ahBl"},
67 70
         };
68 71
 
69 72
         return Arrays.asList(tests);

Загрузка…
Отмена
Сохранить