Skip to content

Commit 70b5b31

Browse files
stanioaivanov-jdk
authored andcommitted
8257664: HTMLEditorKit: Wrong CSS relative font sizes
Reviewed-by: aivanov, psadhukhan
1 parent 0b01d69 commit 70b5b31

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,19 @@ private synchronized void refreshResolvedRules(String selectorName,
17281728
}
17291729
}
17301730

1731+
/**
1732+
* Proxy value to compute inherited {@code font-size}.
1733+
*
1734+
* @see <a href="https://www.w3.org/TR/CSS2/cascade.html">Assigning
1735+
* property values, Cascading, and Inheritance</a>
1736+
*/
1737+
private Object fontSizeInherit() {
1738+
if (fontSizeInherit == null) {
1739+
fontSizeInherit = css.new FontSize().parseCssValue("100%");
1740+
}
1741+
return fontSizeInherit;
1742+
}
1743+
17311744

17321745
/**
17331746
* A temporary class used to hold a Vector, a StringBuffer and a
@@ -2810,6 +2823,13 @@ public Object getAttribute(Object key) {
28102823
}
28112824

28122825
Object doGetAttribute(Object key) {
2826+
if (key == CSS.Attribute.FONT_SIZE && !isDefined(key)) {
2827+
// CSS.FontSize represents a specified value and we need
2828+
// to inherit a computed value so don't resolve percentage
2829+
// value from parent.
2830+
return fontSizeInherit();
2831+
}
2832+
28132833
Object retValue = super.getAttribute(key);
28142834
if (retValue != null) {
28152835
return retValue;
@@ -3206,6 +3226,8 @@ else if (firstChar == '#') {
32063226

32073227
static final int DEFAULT_FONT_SIZE = 3;
32083228

3229+
private transient Object fontSizeInherit;
3230+
32093231
private CSS css;
32103232

32113233
/**
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright (c) 2021, 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.awt.Component;
25+
import java.awt.Graphics;
26+
import java.awt.image.BufferedImage;
27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.util.concurrent.atomic.AtomicReference;
30+
import java.util.function.Consumer;
31+
import javax.imageio.ImageIO;
32+
import javax.swing.JEditorPane;
33+
import javax.swing.SwingUtilities;
34+
import javax.swing.text.GlyphView;
35+
import javax.swing.text.View;
36+
37+
/*
38+
* @test
39+
* @bug 8257664
40+
* @summary Tests inherited font-size with parent percentage specification.
41+
* @run main TestWrongCSSFontSize
42+
*/
43+
public class TestWrongCSSFontSize {
44+
45+
private static String text =
46+
"<html><head><style>" +
47+
"body { font-size: 14 }" +
48+
"div span { font-size: 150% }" +
49+
"span { font-size: 200% }" +
50+
"h2, .h2 { font-size: 150% }" +
51+
"</style></head><body>" +
52+
53+
"<h2>Foo</h2>" +
54+
"<div class=h2>Bar</div>" +
55+
"<ol class=h2><li>Baz</li></ol>" +
56+
"<table class=h2><tr><td>Qux</td></tr></table>" +
57+
"<table><thead class=h2><tr><th>Qux</th></tr></thead></table>" +
58+
"<table><tr class=h2><td>Qux</td></tr></table>" +
59+
"<table><tr><td class=h2>Qux</td></tr></table>" +
60+
"<div><span>Quux</span></div>" +
61+
62+
"</body></html>";
63+
64+
private static int expectedFontSize = 21;
65+
private static int expectedAssertions = 8;
66+
67+
private JEditorPane editor;
68+
69+
public void setUp() {
70+
editor = new JEditorPane();
71+
editor.setContentType("text/html");
72+
editor.setText(text);
73+
editor.setSize(editor.getPreferredSize()); // layout
74+
}
75+
76+
public void run() {
77+
int count = forEachTextRun(editor.getUI()
78+
.getRootView(editor), this::assertFontSize);
79+
if (count != expectedAssertions) {
80+
throw new AssertionError("assertion count expected ["
81+
+ expectedAssertions + "] but found [" + count + "]");
82+
}
83+
}
84+
85+
private int forEachTextRun(View view, Consumer<GlyphView> action) {
86+
int tested = 0;
87+
for (int i = 0; i < view.getViewCount(); i++) {
88+
View child = view.getView(i);
89+
if (child instanceof GlyphView) {
90+
if (child.getElement()
91+
.getAttributes().getAttribute("CR") == Boolean.TRUE) {
92+
continue;
93+
}
94+
action.accept((GlyphView) child);
95+
tested += 1;
96+
} else {
97+
tested += forEachTextRun(child, action);
98+
}
99+
}
100+
return tested;
101+
}
102+
103+
private void assertFontSize(GlyphView child) {
104+
printSource(child);
105+
int actualFontSize = child.getFont().getSize();
106+
if (actualFontSize != expectedFontSize) {
107+
throw new AssertionError("font size expected ["
108+
+ expectedFontSize + "] but found [" + actualFontSize +"]");
109+
}
110+
}
111+
112+
private void printSource(View textRun) {
113+
try {
114+
editor.getEditorKit().write(System.out,
115+
editor.getDocument(), textRun.getStartOffset(),
116+
textRun.getEndOffset() - textRun.getStartOffset());
117+
} catch (Exception e) {
118+
e.printStackTrace();
119+
}
120+
}
121+
122+
private static void captureImage(Component comp, String path) {
123+
try {
124+
BufferedImage capture = new BufferedImage(comp.getWidth(),
125+
comp.getHeight(), BufferedImage.TYPE_INT_ARGB);
126+
Graphics g = capture.getGraphics();
127+
comp.paint(g);
128+
g.dispose();
129+
130+
ImageIO.write(capture, "png", new File(path));
131+
} catch (IOException e) {
132+
e.printStackTrace();
133+
}
134+
}
135+
136+
public static void main(String[] args) throws Throwable {
137+
TestWrongCSSFontSize test = new TestWrongCSSFontSize();
138+
AtomicReference<Throwable> failure = new AtomicReference<>();
139+
SwingUtilities.invokeAndWait(() -> {
140+
try {
141+
test.setUp();
142+
test.run();
143+
} catch (Throwable e) {
144+
failure.set(e);
145+
} finally {
146+
if (args.length == 1) {
147+
captureImage(test.editor, args[0]);
148+
}
149+
}
150+
});
151+
if (failure.get() != null) {
152+
throw failure.get();
153+
}
154+
}
155+
156+
}

0 commit comments

Comments
 (0)