Skip to content

Commit 1939efc

Browse files
TheMangovnikgnu-andrew
authored andcommitted
8213781: web page background renders blue in JEditorPane
4895924: Strings in format #rgb not handled by Color.decode() (affects CSS / Swing) Reviewed-by: andrew Backport-of: f3c10c8
1 parent c5cef8c commit 1939efc

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

src/java.desktop/share/classes/javax/swing/text/html/CSS.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,19 @@ static final Color hexToColor(String value) {
13621362
} else {
13631363
digits = value;
13641364
}
1365+
// Some webpage passes 3 digit color code as in #fff which is
1366+
// decoded as #000FFF resulting in blue background.
1367+
// As per https://www.w3.org/TR/CSS1/#color-units,
1368+
// The three-digit RGB notation (#rgb) is converted into six-digit form
1369+
// (#rrggbb) by replicating digits, not by adding zeros.
1370+
// This makes sure that white (#ffffff) can be specified with the short notation
1371+
// (#fff) and removes any dependencies on the color depth of the display.
1372+
if (digits.length() == 3) {
1373+
final String r = digits.substring(0, 1);
1374+
final String g = digits.substring(1, 2);
1375+
final String b = digits.substring(2, 3);
1376+
digits = String.format("%s%s%s%s%s%s", r, r, g, g, b, b);
1377+
}
13651378
String hstr = "0x" + digits;
13661379
Color c;
13671380
try {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @key headful
27+
* @bug 8213781
28+
* @summary Verify webpage background color renders correctly in JEditorPane
29+
*/
30+
31+
import java.awt.Toolkit;
32+
import java.awt.event.WindowAdapter;
33+
import java.awt.event.WindowEvent;
34+
import java.io.IOException;
35+
import java.net.MalformedURLException;
36+
import javax.swing.JDialog;
37+
import javax.swing.JEditorPane;
38+
import javax.swing.JFrame;
39+
import javax.swing.JScrollPane;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.event.HyperlinkEvent;
42+
import javax.swing.event.HyperlinkListener;
43+
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
44+
import javax.swing.text.html.HTMLDocument;
45+
import java.awt.Color;
46+
import java.awt.Insets;
47+
import java.awt.Point;
48+
import java.awt.Robot;
49+
50+
public class TestBrowserBGColor extends JFrame implements HyperlinkListener {
51+
52+
private static TestBrowserBGColor b;
53+
private static JEditorPane browser;
54+
55+
public static void main(final String[] args) throws Exception {
56+
Robot r = new Robot();
57+
SwingUtilities.invokeAndWait(() -> {
58+
try {
59+
b = new TestBrowserBGColor();
60+
} catch (Exception e) {
61+
throw new RuntimeException(e);
62+
}
63+
b.setSize(Toolkit.getDefaultToolkit().getScreenSize());
64+
b.setVisible(true);
65+
b.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
66+
b.addWindowListener(new WindowAdapter() {
67+
public void windowClosing(WindowEvent e) {
68+
b.dispose();
69+
b = null;
70+
}
71+
});
72+
});
73+
74+
r.waitForIdle();
75+
r.delay(500);
76+
77+
SwingUtilities.invokeAndWait(() -> {
78+
Insets insets = browser.getInsets();
79+
Point loc = browser.getLocationOnScreen();
80+
Color c = r.getPixelColor( loc.x + insets.left+100,
81+
loc.y + insets.top + 100);
82+
b.dispose();
83+
if (!c.equals(Color.WHITE)) {
84+
throw new RuntimeException("webpage background color wrong");
85+
}
86+
});
87+
}
88+
89+
90+
String htmlDoc = " <!DOCTYPE html> <html><style> body { background: #FFF; } </style> <head> <title>Title</title> </head> <body> </body> </html>";
91+
92+
public TestBrowserBGColor() throws IOException, MalformedURLException {
93+
browser = new JEditorPane("text/html", htmlDoc);
94+
browser.setEditable(false);
95+
browser.addHyperlinkListener(this);
96+
JScrollPane scroll = new JScrollPane(browser);
97+
getContentPane().add(scroll);
98+
}
99+
100+
public void hyperlinkUpdate(final HyperlinkEvent e) {
101+
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
102+
JEditorPane pane = (JEditorPane) e.getSource();
103+
if (e instanceof HTMLFrameHyperlinkEvent) {
104+
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
105+
HTMLDocument doc = (HTMLDocument) pane.getDocument();
106+
doc.processHTMLFrameHyperlinkEvent(evt);
107+
} else {
108+
try {
109+
pane.setPage(e.getURL());
110+
} catch (Throwable t) {
111+
t.printStackTrace();
112+
}
113+
}
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)