Skip to content

Commit 1caba0f

Browse files
stanioprsadhuk
authored andcommitted
8292948: JEditorPane ignores font-size styles in external linked css-file
Reviewed-by: psadhukhan
1 parent eeb625e commit 1caba0f

File tree

4 files changed

+213
-6
lines changed

4 files changed

+213
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, 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
@@ -2821,17 +2821,18 @@ public Object getAttribute(Object key) {
28212821
}
28222822

28232823
Object doGetAttribute(Object key) {
2824-
if (key == CSS.Attribute.FONT_SIZE && !isDefined(key)) {
2824+
Object retValue = super.getAttribute(key);
2825+
if (retValue != null) {
2826+
return retValue;
2827+
}
2828+
2829+
if (key == CSS.Attribute.FONT_SIZE) {
28252830
// CSS.FontSize represents a specified value and we need
28262831
// to inherit a computed value so don't resolve percentage
28272832
// value from parent.
28282833
return fontSizeInherit();
28292834
}
28302835

2831-
Object retValue = super.getAttribute(key);
2832-
if (retValue != null) {
2833-
return retValue;
2834-
}
28352836
// didn't find it... try parent if it's a css attribute
28362837
// that is inherited.
28372838
if (key instanceof CSS.Attribute) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.base {
2+
font: 16px sans-serif;
3+
font-size: 16;
4+
}
5+
6+
.bigger {
7+
font-weight: bold;
8+
font-size: 150%;
9+
}
10+
11+
.smaller {
12+
font-style: italic;
13+
font-size: 75%;
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<title>JDK-8292948: JEditorPane ignores font-size styles in external linked css-file</title>
6+
<link rel="stylesheet" href="TestExternalCSSFontSize.css" type="text/css">
7+
</head>
8+
<body>
9+
10+
<div class="base">
11+
12+
<p class="bigger">Bigger text (24)</p>
13+
14+
<p>Normal size text (16)</p>
15+
16+
<p class="smaller">Smaller text (12)</p>
17+
18+
</div>
19+
20+
</body>
21+
</html>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2022, 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+
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.UncheckedIOException;
27+
import java.net.URL;
28+
import java.util.Arrays;
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.TimeUnit;
31+
32+
import java.awt.Graphics;
33+
import java.awt.image.BufferedImage;
34+
import javax.imageio.ImageIO;
35+
import javax.swing.JEditorPane;
36+
import javax.swing.SwingUtilities;
37+
import javax.swing.text.GlyphView;
38+
import javax.swing.text.View;
39+
40+
/*
41+
* @test
42+
* @bug 8292948
43+
* @summary Tests font-size declarations from an external style sheet.
44+
* @run main TestExternalCSSFontSize
45+
*/
46+
public class TestExternalCSSFontSize {
47+
48+
private static final int[] expectedFontSizes = { 24, 16, 12 };
49+
50+
private volatile JEditorPane editor;
51+
52+
private volatile CountDownLatch loadLatch;
53+
54+
TestExternalCSSFontSize() {}
55+
56+
void setUp() {
57+
String fileName = getClass().getName().replace('.', '/') + ".html";
58+
URL htmlFile = getClass().getClassLoader().getResource(fileName);
59+
if (htmlFile == null) {
60+
throw new IllegalStateException("Resource not found: " + fileName);
61+
}
62+
63+
loadLatch = new CountDownLatch(1);
64+
editor = new JEditorPane();
65+
editor.setContentType("text/html");
66+
editor.addPropertyChangeListener("page", evt -> {
67+
System.out.append("Loaded: ").println(evt.getNewValue());
68+
loadLatch.countDown();
69+
});
70+
try {
71+
editor.setPage(htmlFile);
72+
} catch (IOException e) {
73+
throw new UncheckedIOException(e);
74+
}
75+
}
76+
77+
void verify() {
78+
editor.setSize(editor.getPreferredSize()); // Do lay out text
79+
80+
scanFontSizes(editor.getUI().getRootView(editor), 0);
81+
}
82+
83+
private int scanFontSizes(View view, int branchIndex) {
84+
int currentIndex = branchIndex;
85+
for (int i = 0; i < view.getViewCount(); i++) {
86+
View child = view.getView(i);
87+
if (child instanceof GlyphView) {
88+
if (child.getElement()
89+
.getAttributes().getAttribute("CR") == Boolean.TRUE) {
90+
continue;
91+
}
92+
assertFontSize((GlyphView) child, currentIndex++);
93+
} else {
94+
currentIndex = scanFontSizes(child, currentIndex);
95+
}
96+
}
97+
return currentIndex;
98+
}
99+
100+
private void assertFontSize(GlyphView child, int index) {
101+
printSource(child);
102+
if (index >= expectedFontSizes.length) {
103+
throw new AssertionError("Unexpected text run #"
104+
+ index + " (>= " + expectedFontSizes.length + ")");
105+
}
106+
107+
int actualFontSize = child.getFont().getSize();
108+
if (actualFontSize != expectedFontSizes[index]) {
109+
throw new AssertionError("Font size expected ["
110+
+ expectedFontSizes[index] + "] but found [" + actualFontSize +"]");
111+
}
112+
}
113+
114+
private void printSource(View textRun) {
115+
try {
116+
editor.getEditorKit().write(System.out,
117+
editor.getDocument(), textRun.getStartOffset(),
118+
textRun.getEndOffset() - textRun.getStartOffset());
119+
} catch (Exception e) {
120+
e.printStackTrace();
121+
}
122+
}
123+
124+
void run() throws Throwable {
125+
SwingUtilities.invokeAndWait(this::setUp);
126+
if (loadLatch.await(5, TimeUnit.SECONDS)) {
127+
SwingUtilities.invokeAndWait(this::verify);
128+
} else {
129+
throw new IllegalStateException("Page loading timed out");
130+
}
131+
}
132+
133+
public static void main(String[] args) throws Throwable {
134+
TestExternalCSSFontSize test = new TestExternalCSSFontSize();
135+
boolean success = false;
136+
try {
137+
test.run();
138+
success = true;
139+
} finally {
140+
if (!success || hasOpt(args, "-capture")) {
141+
if (test.editor == null) {
142+
System.err.println("Can't save image (test.editor is null)");
143+
} else {
144+
String suffix = success ? "-success" : "-failure";
145+
SwingUtilities.invokeAndWait(() -> test.captureImage(suffix));
146+
}
147+
}
148+
}
149+
}
150+
151+
private static boolean hasOpt(String[] args, String opt) {
152+
return Arrays.asList(args).contains(opt);
153+
}
154+
155+
private void captureImage(String suffix) {
156+
try {
157+
BufferedImage capture =
158+
new BufferedImage(editor.getWidth(), editor.getHeight(),
159+
BufferedImage.TYPE_INT_ARGB);
160+
Graphics g = capture.getGraphics();
161+
editor.paint(g);
162+
g.dispose();
163+
164+
ImageIO.write(capture, "png",
165+
new File(getClass().getSimpleName() + suffix + ".png"));
166+
} catch (IOException e) {
167+
e.printStackTrace();
168+
}
169+
}
170+
171+
}

0 commit comments

Comments
 (0)