Browse Source

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 years ago
parent
commit
68c1b882fd

+ 20
- 3
src/com/dmdirc/ui/messages/Styliser.java View File

251
      * to be as far left as they possibly can be. This means that the start of
251
      * to be as far left as they possibly can be. This means that the start of
252
      * the string will include any control codes immediately preceeding the
252
      * the string will include any control codes immediately preceeding the
253
      * desired text, and the end will not include any trailing codes.
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
      * @param styled The styled String to be operated on
257
      * @param styled The styled String to be operated on
256
      * @param from The starting index in the unstyled string
258
      * @param from The starting index in the unstyled string
262
         final String unstyled = stipControlCodes(styled);
264
         final String unstyled = stipControlCodes(styled);
263
         final String startBit = unstyled.substring(0, from);
265
         final String startBit = unstyled.substring(0, from);
264
         final String middleBit = unstyled.substring(from, to);
266
         final String middleBit = unstyled.substring(from, to);
267
+        final String sanitised = stipInternalControlCodes(styled);
265
         int start = from;
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
             start++;
271
             start++;
269
         }
272
         }
270
 
273
 
271
         int end = to + start - from;
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
             end++;
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
                 + CODE_TOOLTIP, "$1");
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
      * Returns a substring of the input string such that no control codes are present
373
      * Returns a substring of the input string such that no control codes are present
357
      * in the output. If the returned value isn't the same as the input, then the
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 View File

64
                      4, 7, Styliser.CODE_COLOUR + "4,0RED"},
64
                      4, 7, Styliser.CODE_COLOUR + "4,0RED"},
65
             {"Blah" + Styliser.CODE_COLOUR + "4,0RED blah blah",
65
             {"Blah" + Styliser.CODE_COLOUR + "4,0RED blah blah",
66
                      5, 7, "ED"},
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
         return Arrays.asList(tests);
72
         return Arrays.asList(tests);

Loading…
Cancel
Save