]> git.lyx.org Git - features.git/commitdiff
Support input of non-ASCII characters in hyperlinks
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 9 Jun 2018 09:52:55 +0000 (11:52 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 9 Sep 2018 09:00:20 +0000 (11:00 +0200)
We transform them to hex representation via
QByteArray::toPercentEncoding()

Fixes: #11165
(cherry picked from commit 01d8f418943f5f63f329658d387a7dbd9a024c91)

src/insets/InsetHyperlink.cpp
src/support/lstrings.cpp
src/support/lstrings.h
status.23x

index afa49d4e35b36d8573385cb1fddfbcf7dfceb853..aae07ce0da8867a9c4b449508ed6ffb1f5eeeff9 100644 (file)
@@ -77,6 +77,7 @@ docstring InsetHyperlink::screenLabel() const
        return temp + url;
 }
 
+
 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action()) {
@@ -125,15 +126,16 @@ void InsetHyperlink::latex(otexstream & os,
 
        // For the case there is no name given, the target is set as name.
        // Do this before !url.empty() and !name.empty() to handle characters
-       // like the "%" correctly.
+       // such as % correctly.
        if (name.empty())
                name = url;
 
        if (!url.empty()) {
-               // Replace the "\" character by its ASCII code according to the
-               // URL specifications because "\" is not allowed in URLs and by
-               // \href. Only do this when the following character is not also
-               // a "\", because "\\" is valid code
+               // Use URI/URL-style percent-encoded string (hexadecimal).
+               // We exclude some characters that must not be transformed
+               // in hrefs (% # / :) or that we need to treat manually (\).
+               url = to_percent_encoding(url, from_ascii("%#\\/:"));
+               // We handle \ manually since \\ is valid
                for (size_t i = 0, pos;
                        (pos = url.find('\\', i)) != string::npos;
                        i = pos + 2) {
@@ -145,6 +147,7 @@ void InsetHyperlink::latex(otexstream & os,
                // field because otherwise LaTeX will fail when the hyperlink is
                // within an argument of another command, e.g. in a \footnote. It
                // is important that they are escaped as "\#" and not as "\#{}".
+               // FIXME this is not necessary in outside of commands.
                for (int k = 0; k < 2; k++)
                        for (size_t i = 0, pos;
                                (pos = url.find(chars_url[k], i)) != string::npos;
index be326c0fd46dea68b7b229130b488fe9f3518fd3..4a12c34b4daec5fb2767ccec63a4ae4626859305 100644 (file)
@@ -1431,6 +1431,14 @@ std::string formatFPNumber(double x)
 }
 
 
+docstring to_percent_encoding(docstring const & in, docstring const & ex)
+{
+       QByteArray input = toqstr(in).toUtf8();
+       QByteArray excludes = toqstr(ex).toUtf8();
+       return qstring_to_ucs4(QString(input.toPercentEncoding(excludes)));
+}
+
+
 docstring bformat(docstring const & fmt, int arg1)
 {
        LATTEST(contains(fmt, from_ascii("%1$d")));
index 9bcff42afa1a2e89470d76daa96959140006a804..7d2fc6dd77d9d23a12a08c562e8dae23408cf69c 100644 (file)
@@ -353,6 +353,10 @@ int findToken(char const * const str[], std::string const & search_token);
 /// like "1000000.000000", and precision control would not be that easy either.
 std::string formatFPNumber(double);
 
+/// Returns an URI/URL-style percent-encoded copy of the string \p in.
+/// \p ex defines a string of characters that are excluded from the transformation
+docstring to_percent_encoding(docstring const & in, docstring const & ex = docstring());
+
 
 docstring bformat(docstring const & fmt, int arg1);
 docstring bformat(docstring const & fmt, long arg1);
index 65f296e2b6df4e7a9b72c9e63b4d4f4bb437e528..690313ae5df3694bd78810cf4576b0ddb63cbe9c 100644 (file)
@@ -15,7 +15,7 @@ What's new
 
 * DOCUMENT INPUT/OUTPUT
 
-
+- Add support for non-ASCII characters in hyperlinks (bug 11165).
 
 
 * MISCELLANEOUS