diff options
| author | Xi Yan | 2018-06-06 15:44:50 +0000 |
|---|---|---|
| committer | Eric Williams | 2018-06-07 14:43:47 +0000 |
| commit | ebf9d7eedcf2e07f6c256751abf964f7e8cbd29e (patch) | |
| tree | e36b32c28df004ace2d971e4ca005241e51cce55 | |
| parent | 820c46cbb7018b48aa8791ca34c8025246bda964 (diff) | |
| download | eclipse.platform.swt-ebf9d7eedcf2e07f6c256751abf964f7e8cbd29e.tar.gz eclipse.platform.swt-ebf9d7eedcf2e07f6c256751abf964f7e8cbd29e.tar.xz eclipse.platform.swt-ebf9d7eedcf2e07f6c256751abf964f7e8cbd29e.zip | |
Bug 396175 - Link widget needs an escape character for < and >
Added escape character for angle bracket in link widget.
Change-Id: I44675a5a18c4319421c9b37af16d4d345ec8b55d
Signed-off-by: Xi Yan <xixiyan@redhat.com>
4 files changed, 69 insertions, 12 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Link.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Link.java index 3e98df4c5f..4a128c4cc3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Link.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Link.java @@ -730,7 +730,9 @@ void setOrientation () { * In the rare case of identical hyperlinks within the same string, the * HREF attribute can be used to distinguish between them. The string may * include the mnemonic character and line delimiters. The only delimiter - * the HREF attribute supports is the quotation mark ("). + * the HREF attribute supports is the quotation mark ("). Text containing + * angle-bracket characters < or > may be escaped using \\, however + * this operation is a hint and varies from platform to platform. * </p> * <p> * Mnemonics are indicated by an '&' that causes the next diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Link.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Link.java index 716caf8987..30455f3216 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Link.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Link.java @@ -570,12 +570,21 @@ String parse (String string) { ids = new String [length / 4]; mnemonics = new int [length / 4 + 1]; StringBuilder result = new StringBuilder (); - char [] buffer = new char [length]; - string.getChars (0, string.length (), buffer, 0); + StringBuilder buffer = new StringBuilder(string); int index = 0, state = 0, linkIndex = 0; int start = 0, tagStart = 0, linkStart = 0, endtagStart = 0, refStart = 0; while (index < length) { - char c = Character.toLowerCase (buffer [index]); + char c = Character.toLowerCase (buffer.charAt(index)); + + // escape < or > with \\< or \\> + if (c == '\\' && index + 1 < length) { + char c2 = Character.toLowerCase(buffer.charAt(index + 1)); + if (c2 == '<' || c2 == '>') { + buffer.deleteCharAt(index); + length--; + } + } + switch (state) { case 0: if (c == '<') { @@ -614,12 +623,12 @@ String parse (String string) { break; case 6: if (c == '>') { - mnemonics [linkIndex] = parseMnemonics (buffer, start, tagStart, result); + mnemonics [linkIndex] = parseMnemonics (buffer.toString().toCharArray(), start, tagStart, result); int offset = result.length (); - parseMnemonics (buffer, linkStart, endtagStart, result); + parseMnemonics (buffer.toString().toCharArray(), linkStart, endtagStart, result); offsets [linkIndex] = new Point (offset, result.length () - 1); if (ids [linkIndex] == null) { - ids [linkIndex] = new String (buffer, linkStart, endtagStart - linkStart); + ids [linkIndex] = new String (buffer.toString().toCharArray(), linkStart, endtagStart - linkStart); } linkIndex++; start = tagStart = linkStart = endtagStart = refStart = index + 1; @@ -650,7 +659,7 @@ String parse (String string) { break; case 12: if (c == '"') { - ids[linkIndex] = new String (buffer, refStart, index - refStart); + ids[linkIndex] = new String (buffer.toString().toCharArray(), refStart, index - refStart); state = 2; } break; @@ -674,8 +683,8 @@ String parse (String string) { index++; } if (start < length) { - int tmp = parseMnemonics (buffer, start, tagStart, result); - int mnemonic = parseMnemonics (buffer, Math.max (tagStart, linkStart), length, result); + int tmp = parseMnemonics (buffer.toString().toCharArray(), start, tagStart, result); + int mnemonic = parseMnemonics (buffer.toString().toCharArray(), Math.max (tagStart, linkStart), length, result); if (mnemonic == -1) mnemonic = tmp; mnemonics [linkIndex] = mnemonic; } else { @@ -778,7 +787,9 @@ void setOrientation (boolean create) { * In the rare case of identical hyperlinks within the same string, the * HREF attribute can be used to distinguish between them. The string may * include the mnemonic character and line delimiters. The only delimiter - * the HREF attribute supports is the quotation mark ("). + * the HREF attribute supports is the quotation mark ("). Text containing + * angle-bracket characters < or > may be escaped using \\, however + * this operation is a hint and varies from platform to platform. * </p> * <p> * Mnemonics are indicated by an '&' that causes the next diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Link.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Link.java index d41f9440a5..646a40c39c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Link.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Link.java @@ -707,7 +707,9 @@ public void setLinkForeground (Color color) { * In the rare case of identical hyperlinks within the same string, the * HREF attribute can be used to distinguish between them. The string may * include the mnemonic character and line delimiters. The only delimiter - * the HREF attribute supports is the quotation mark ("). + * the HREF attribute supports is the quotation mark ("). Text containing + * angle-bracket characters < or > may be escaped using \\, however + * this operation is a hint and varies from platform to platform. * </p> * <p> * Mnemonics are indicated by an '&' that causes the next diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug396175_LinkAngularBracket.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug396175_LinkAngularBracket.java new file mode 100644 index 0000000000..f28373b9dd --- /dev/null +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug396175_LinkAngularBracket.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.tests.gtk.snippets; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Link; +import org.eclipse.swt.widgets.Shell; + +public class Bug396175_LinkAngularBracket { + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + + Link fakeLink = new Link(shell, SWT.BORDER); + fakeLink.setText("This is a \\<a href=\"fake\">FAKE\\<LINK\\</a\\>"); + + Link realLink = new Link(shell, SWT.BORDER); + realLink.setText("This is a <a href=\"real\">REAL\\<LINK</a>"); + + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + display.dispose(); + } + +} |
