8213781: web page background renders blue in JEditorPane
4895924: Strings in format #rgb not handled by Color.decode() (affects CSS / Swing)
Reviewed-by: serb, sveerabhadra
--- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java Mon Feb 25 11:02:53 2019 +0530
+++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java Tue Feb 26 11:08:07 2019 +0530
@@ -1362,6 +1362,19 @@
} else {
digits = value;
}
+ // Some webpage passes 3 digit color code as in #fff which is
+ // decoded as #000FFF resulting in blue background.
+ // As per https://www.w3.org/TR/CSS1/#color-units,
+ // The three-digit RGB notation (#rgb) is converted into six-digit form
+ // (#rrggbb) by replicating digits, not by adding zeros.
+ // This makes sure that white (#ffffff) can be specified with the short notation
+ // (#fff) and removes any dependencies on the color depth of the display.
+ if (digits.length() == 3) {
+ final String r = digits.substring(0, 1);
+ final String g = digits.substring(1, 2);
+ final String b = digits.substring(2, 3);
+ digits = String.format("%s%s%s%s%s%s", r, r, g, g, b, b);
+ }
String hstr = "0x" + digits;
Color c;
try {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JEditorPane/TestBrowserBGColor.java Tue Feb 26 11:08:07 2019 +0530
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @key headful
+ * @bug 8213781
+ * @summary Verify webpage background color renders correctly in JEditorPane
+ */
+
+import java.awt.Toolkit;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import javax.swing.JDialog;
+import javax.swing.JEditorPane;
+import javax.swing.JFrame;
+import javax.swing.JScrollPane;
+import javax.swing.SwingUtilities;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkListener;
+import javax.swing.text.html.HTMLFrameHyperlinkEvent;
+import javax.swing.text.html.HTMLDocument;
+import java.awt.Color;
+import java.awt.Insets;
+import java.awt.Point;
+import java.awt.Robot;
+
+public class TestBrowserBGColor extends JFrame implements HyperlinkListener {
+
+ private static TestBrowserBGColor b;
+ private static JEditorPane browser;
+
+ public static void main(final String[] args) throws Exception {
+ Robot r = new Robot();
+ SwingUtilities.invokeAndWait(() -> {
+ try {
+ b = new TestBrowserBGColor();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ b.setSize(Toolkit.getDefaultToolkit().getScreenSize());
+ b.setVisible(true);
+ b.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+ b.addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent e) {
+ b.dispose();
+ b = null;
+ }
+ });
+ });
+
+ r.waitForIdle();
+ r.delay(500);
+
+ SwingUtilities.invokeAndWait(() -> {
+ Insets insets = browser.getInsets();
+ Point loc = browser.getLocationOnScreen();
+ Color c = r.getPixelColor( loc.x + insets.left+100,
+ loc.y + insets.top + 100);
+ b.dispose();
+ if (!c.equals(Color.WHITE)) {
+ throw new RuntimeException("webpage background color wrong");
+ }
+ });
+ }
+
+
+ String htmlDoc = " <!DOCTYPE html> <html><style> body { background: #FFF; } </style> <head> <title>Title</title> </head> <body> </body> </html>";
+
+ public TestBrowserBGColor() throws IOException, MalformedURLException {
+ browser = new JEditorPane("text/html", htmlDoc);
+ browser.setEditable(false);
+ browser.addHyperlinkListener(this);
+ JScrollPane scroll = new JScrollPane(browser);
+ getContentPane().add(scroll);
+ }
+
+ public void hyperlinkUpdate(final HyperlinkEvent e) {
+ if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
+ JEditorPane pane = (JEditorPane) e.getSource();
+ if (e instanceof HTMLFrameHyperlinkEvent) {
+ HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
+ HTMLDocument doc = (HTMLDocument) pane.getDocument();
+ doc.processHTMLFrameHyperlinkEvent(evt);
+ } else {
+ try {
+ pane.setPage(e.getURL());
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+ }
+ }
+}