Ticket #21597: 21597.patch

File 21597.patch, 2.9 KB (added by taylor.smock, 3 years ago)

Unwrap long url when getting text from HTML document

  • src/org/openstreetmap/josm/gui/widgets/JosmHTMLEditorKit.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/gui/widgets/JosmHTMLEditorKit.java b/src/org/openstreetmap/josm/gui/widgets/JosmHTMLEditorKit.java
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.widgets;
    33
     4import javax.swing.text.BadLocationException;
     5import javax.swing.text.Document;
    46import javax.swing.text.ViewFactory;
     7import javax.swing.text.html.HTMLDocument;
    58import javax.swing.text.html.HTMLEditorKit;
    69import javax.swing.text.html.StyleSheet;
    710
     11import org.openstreetmap.josm.tools.TextUtils;
     12
    813/**
    914 * A subclass of {@link HTMLEditorKit} that fixes an uncommon design choice that shares the set stylesheet between all instances.
    1015 * This class stores a single stylesheet per instance, as it should have be done by Sun in the first place.
     
    5156    public ViewFactory getViewFactory() {
    5257        return FACTORY;
    5358    }
     59
     60    @Override
     61    public Document createDefaultDocument() {
     62        StyleSheet styles = getStyleSheet();
     63        StyleSheet ss = new StyleSheet();
     64
     65        ss.addStyleSheet(styles);
     66
     67        HTMLDocument doc = new UrlHTMLDocument(ss);
     68        doc.setParser(getParser());
     69        doc.setAsynchronousLoadPriority(4);
     70        doc.setTokenThreshold(100);
     71        return doc;
     72    }
     73
     74    private static class UrlHTMLDocument extends HTMLDocument {
     75        UrlHTMLDocument(StyleSheet ss) {
     76            super(ss);
     77        }
     78
     79        @Override
     80        public String getText(int offset, int length) throws BadLocationException {
     81            String original = super.getText(offset, length);
     82            return TextUtils.unwrapLongUrl(original);
     83        }
     84    }
    5485}
  • src/org/openstreetmap/josm/tools/TextUtils.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/tools/TextUtils.java b/src/org/openstreetmap/josm/tools/TextUtils.java
    a b  
    2020    public static String wrapLongUrl(String url) {
    2121        return url.replace("/", "/\u200b").replace("&", "&\u200b");
    2222    }
     23
     24    /**
     25     * Remove zero width space character (U+8203) after each slash/ampersand to wrap long URLs.
     26     * @param url URL
     27     * @return unwrapped URL
     28     * @since xxx
     29     */
     30    public static String unwrapLongUrl(String url) {
     31        return url.replace("/\u200b", "/").replace("&\u200b", "&");
     32    }
    2333}