Skip to content

Commit 729f4f9

Browse files
scientificwareaivanov-jdk
authored andcommitted
8314731: Add support for the alt attribute in the image type input HTML tag
Reviewed-by: aivanov, prr, tr
1 parent 11cdafb commit 729f4f9

File tree

2 files changed

+124
-5
lines changed

2 files changed

+124
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525
package javax.swing.text.html;
2626

2727
import java.awt.Component;
28+
import java.awt.MediaTracker;
2829
import java.awt.Point;
2930
import java.awt.event.ActionEvent;
3031
import java.awt.event.ActionListener;
@@ -43,7 +44,6 @@
4344
import javax.swing.ButtonModel;
4445
import javax.swing.ComboBoxModel;
4546
import javax.swing.DefaultButtonModel;
46-
import javax.swing.Icon;
4747
import javax.swing.ImageIcon;
4848
import javax.swing.JButton;
4949
import javax.swing.JCheckBox;
@@ -273,15 +273,21 @@ private JComponent createInputComponent(AttributeSet attr, Object model) {
273273
maxIsPreferred = 3;
274274
} else if (type.equals("image")) {
275275
String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC);
276+
String altAtt = (String) attr.getAttribute(HTML.Attribute.ALT);
277+
if (altAtt == null) {
278+
altAtt = srcAtt;
279+
}
276280
JButton button;
277281
try {
278282
URL base = ((HTMLDocument)getElement().getDocument()).getBase();
279283
@SuppressWarnings("deprecation")
280284
URL srcURL = new URL(base, srcAtt);
281-
Icon icon = new ImageIcon(srcURL);
282-
button = new JButton(icon);
285+
ImageIcon icon = new ImageIcon(srcURL, altAtt);
286+
button = icon.getImageLoadStatus() == MediaTracker.COMPLETE
287+
? new JButton(icon)
288+
: new JButton(altAtt);
283289
} catch (MalformedURLException e) {
284-
button = new JButton(srcAtt);
290+
button = new JButton(altAtt);
285291
}
286292
if (model != null) {
287293
button.setModel((ButtonModel)model);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2025, 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+
* @bug 8314731
27+
* @key headful
28+
* @summary FormView doesn't support the alt attribute
29+
*/
30+
31+
import java.awt.BorderLayout;
32+
import java.awt.Component;
33+
import java.awt.Container;
34+
import java.awt.Dimension;
35+
36+
import javax.swing.JButton;
37+
import javax.swing.JEditorPane;
38+
import javax.swing.JFrame;
39+
import javax.swing.JScrollPane;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.text.Document;
42+
import javax.swing.text.html.HTMLEditorKit;
43+
import javax.swing.text.html.StyleSheet;
44+
45+
public class bug8314731 {
46+
47+
private static JFrame frame;
48+
private static JEditorPane editorPane;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
SwingUtilities.invokeAndWait(bug8314731::createAndSetVisibleUI);
53+
SwingUtilities.invokeAndWait(() -> {
54+
if (!containsAlt(editorPane)) {
55+
throw new RuntimeException("FormView doesn't support the alt attribute, see JDK-8314731.");
56+
}
57+
});
58+
} finally {
59+
SwingUtilities.invokeAndWait(() -> {
60+
if (frame != null) {
61+
frame.dispose();
62+
}
63+
});
64+
}
65+
}
66+
67+
private static void createAndSetVisibleUI() {
68+
editorPane = new JEditorPane();
69+
editorPane.setEditable(false);
70+
frame = new JFrame("alt attribute test in HTML image type input");
71+
72+
JScrollPane scrollPane = new JScrollPane(editorPane);
73+
HTMLEditorKit kit = new HTMLEditorKit();
74+
editorPane.setEditorKit(kit);
75+
StyleSheet styleSheet = kit.getStyleSheet();
76+
styleSheet.addRule("""
77+
body {
78+
color: #000;
79+
font-family: times;
80+
margin: 4px;
81+
}
82+
""");
83+
String htmlString = """
84+
<html>
85+
<body>
86+
<input type=image
87+
name=point
88+
alt="Logo">
89+
</body>
90+
</html>
91+
""";
92+
Document doc = kit.createDefaultDocument();
93+
editorPane.setDocument(doc);
94+
editorPane.setText(htmlString);
95+
96+
frame.add(scrollPane, BorderLayout.CENTER);
97+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
98+
frame.setSize(new Dimension(400, 200));
99+
frame.setLocationRelativeTo(null);
100+
frame.setVisible(true);
101+
}
102+
103+
private static boolean containsAlt(Container container) {
104+
for (Component c : container.getComponents()) {
105+
if (c instanceof JButton button) {
106+
return "Logo".equals(button.getText());
107+
} else if (c instanceof Container cont) {
108+
return containsAlt(cont);
109+
}
110+
}
111+
return false;
112+
}
113+
}

0 commit comments

Comments
 (0)