Changeset 13162 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java
r13157 r13162 19 19 import java.util.Collections; 20 20 import java.util.List; 21 import java.util.regex.Matcher; 21 22 import java.util.regex.Pattern; 22 23 … … 79 80 private static final Pattern SENTENCE_MARKS_EASTERN = Pattern.compile("(\\u3002)([\\p{L}\\p{Punct}])"); 80 81 82 private static final Pattern HTTP_LINK = Pattern.compile("(https?://[^\\s\\(\\)<>]+)"); 83 private static final Pattern HTML_LINK = Pattern.compile("<a href=\"[^\"]+\">([^<]+)</a>"); 84 private static final Pattern SLASH = Pattern.compile("([^/])/([^/])"); 85 81 86 private final NoteData noteData; 82 87 … … 231 236 private void fixPanelSize(MapView mv, String text) { 232 237 int maxWidth = mv.getWidth() * 2/3; 238 int maxHeight = mv.getHeight() * 2/3; 233 239 JEditorPane pane = displayedPanel.getEditorPane(); 234 if (pane.getPreferredSize().width > maxWidth && Config.getPref().getBoolean("note.text.break-on-sentence-mark", false)) { 240 Dimension d = pane.getPreferredSize(); 241 if ((d.width > maxWidth || d.height > maxHeight) && Config.getPref().getBoolean("note.text.break-on-sentence-mark", false)) { 235 242 // To make sure long notes are displayed correctly 236 243 displayedPanel.setText(insertLineBreaks(text)); 237 244 } 238 245 // If still too large, enforce maximum size 239 Dimensiond = pane.getPreferredSize();240 if (d.width > maxWidth ) {246 d = pane.getPreferredSize(); 247 if (d.width > maxWidth || d.height > maxHeight) { 241 248 View v = (View) pane.getClientProperty(BasicHTML.propertyKey); 242 249 if (v == null) { … … 290 297 htmlText = htmlText.replace("
", "<br>"); 291 298 // convert URLs to proper HTML links 292 htmlText = htmlText.replaceAll("(https?://[^\\s\\(\\)<>]+)", "<a href=\"$1\">$1</a>");299 htmlText = replaceLinks(htmlText); 293 300 sb.append(htmlText); 294 301 } … … 297 304 String result = sb.toString(); 298 305 Logging.debug(result); 306 return result; 307 } 308 309 static String replaceLinks(String htmlText) { 310 String result = HTTP_LINK.matcher(htmlText).replaceAll("<a href=\"$1\">$1</a>"); 311 Matcher m1 = HTML_LINK.matcher(result); 312 if (m1.find()) { 313 int last = 0; 314 StringBuffer sb = new StringBuffer(); // Switch to StringBuilder when switching to Java 9 315 do { 316 sb.append(result, last, m1.start()); 317 last = m1.end(); 318 String link = m1.group(0); 319 Matcher m2 = SLASH.matcher(link).region(link.indexOf('>'), link.lastIndexOf('<')); 320 while (m2.find()) { 321 m2.appendReplacement(sb, "$1/\u200b$2"); //zero width space to wrap long URLs (see #10864, #15550) 322 } 323 m2.appendTail(sb); 324 } while (m1.find()); 325 result = sb.append(result, last, result.length()).toString(); 326 } 299 327 return result; 300 328 } -
trunk/test/unit/org/openstreetmap/josm/gui/layer/NoteLayerTest.java
r13122 r13162 56 56 // CHECKSTYLE.ON: LineLength 57 57 } 58 59 /** 60 * Unit test of {@link NoteLayer#replaceLinks}. 61 */ 62 @Test 63 public void testReplaceLinks() { 64 // empty string 65 assertEquals("", NoteLayer.replaceLinks("")); 66 // no link 67 assertEquals("no http link", NoteLayer.replaceLinks("no http link")); 68 // just one link 69 assertEquals("<a href=\"https://www.example.com/test\">https://www.example.com/\u200btest</a>", 70 NoteLayer.replaceLinks("https://www.example.com/test")); 71 // CHECKSTYLE.OFF: LineLength 72 // text with several links (with and without slash) 73 assertEquals("foo <a href=\"https://foo.example.com/test\">https://foo.example.com/\u200btest</a> bar <a href=\"https://bar.example.com\">https://bar.example.com</a> baz", 74 NoteLayer.replaceLinks("foo https://foo.example.com/test bar https://bar.example.com baz")); 75 // CHECKSTYLE.ON: LineLength 76 } 58 77 }
Note:
See TracChangeset
for help on using the changeset viewer.