From 729f4f9b16ca0ede57976f10abbafedee1c6e017 Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Mon, 15 Sep 2025 19:22:47 +0000 Subject: [PATCH] 8314731: Add support for the alt attribute in the image type input HTML tag Reviewed-by: aivanov, prr, tr --- .../javax/swing/text/html/FormView.java | 16 ++- .../jdk/javax/swing/text/html/bug8314731.java | 113 ++++++++++++++++++ 2 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 test/jdk/javax/swing/text/html/bug8314731.java diff --git a/src/java.desktop/share/classes/javax/swing/text/html/FormView.java b/src/java.desktop/share/classes/javax/swing/text/html/FormView.java index 47f10e1abb2..ad13f2b6657 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/FormView.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/FormView.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2025, 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 @@ -25,6 +25,7 @@ package javax.swing.text.html; import java.awt.Component; +import java.awt.MediaTracker; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -43,7 +44,6 @@ import javax.swing.Box; import javax.swing.ButtonModel; import javax.swing.ComboBoxModel; import javax.swing.DefaultButtonModel; -import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; @@ -273,15 +273,21 @@ public class FormView extends ComponentView implements ActionListener { maxIsPreferred = 3; } else if (type.equals("image")) { String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC); + String altAtt = (String) attr.getAttribute(HTML.Attribute.ALT); + if (altAtt == null) { + altAtt = srcAtt; + } JButton button; try { URL base = ((HTMLDocument)getElement().getDocument()).getBase(); @SuppressWarnings("deprecation") URL srcURL = new URL(base, srcAtt); - Icon icon = new ImageIcon(srcURL); - button = new JButton(icon); + ImageIcon icon = new ImageIcon(srcURL, altAtt); + button = icon.getImageLoadStatus() == MediaTracker.COMPLETE + ? new JButton(icon) + : new JButton(altAtt); } catch (MalformedURLException e) { - button = new JButton(srcAtt); + button = new JButton(altAtt); } if (model != null) { button.setModel((ButtonModel)model); diff --git a/test/jdk/javax/swing/text/html/bug8314731.java b/test/jdk/javax/swing/text/html/bug8314731.java new file mode 100644 index 00000000000..d7da4630754 --- /dev/null +++ b/test/jdk/javax/swing/text/html/bug8314731.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2025, 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 + * @bug 8314731 + * @key headful + * @summary FormView doesn't support the alt attribute + */ + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; + +import javax.swing.JButton; +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.SwingUtilities; +import javax.swing.text.Document; +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.StyleSheet; + +public class bug8314731 { + + private static JFrame frame; + private static JEditorPane editorPane; + + public static void main(String[] args) throws Exception { + try { + SwingUtilities.invokeAndWait(bug8314731::createAndSetVisibleUI); + SwingUtilities.invokeAndWait(() -> { + if (!containsAlt(editorPane)) { + throw new RuntimeException("FormView doesn't support the alt attribute, see JDK-8314731."); + } + }); + } finally { + SwingUtilities.invokeAndWait(() -> { + if (frame != null) { + frame.dispose(); + } + }); + } + } + + private static void createAndSetVisibleUI() { + editorPane = new JEditorPane(); + editorPane.setEditable(false); + frame = new JFrame("alt attribute test in HTML image type input"); + + JScrollPane scrollPane = new JScrollPane(editorPane); + HTMLEditorKit kit = new HTMLEditorKit(); + editorPane.setEditorKit(kit); + StyleSheet styleSheet = kit.getStyleSheet(); + styleSheet.addRule(""" + body { + color: #000; + font-family: times; + margin: 4px; + } + """); + String htmlString = """ + + + + + + """; + Document doc = kit.createDefaultDocument(); + editorPane.setDocument(doc); + editorPane.setText(htmlString); + + frame.add(scrollPane, BorderLayout.CENTER); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(new Dimension(400, 200)); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private static boolean containsAlt(Container container) { + for (Component c : container.getComponents()) { + if (c instanceof JButton button) { + return "Logo".equals(button.getText()); + } else if (c instanceof Container cont) { + return containsAlt(cont); + } + } + return false; + } +}