mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
Fixing NPE when using HTML text on a component with null font
This commit is contained in:
@@ -16,17 +16,14 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.*;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JToolTip;
|
||||
import javax.swing.*;
|
||||
import javax.swing.plaf.basic.BasicHTML;
|
||||
import javax.swing.text.AttributeSet;
|
||||
import javax.swing.text.Document;
|
||||
@@ -74,9 +71,9 @@ public class FlatHTML
|
||||
for( int i = 1; i <= 7; i++ )
|
||||
System.out.println( i+": "+ styleSheet.getPointSize( i ) );
|
||||
debug*/
|
||||
int fontBaseSize = c.getFont().getSize();
|
||||
Font font = c.getFont();
|
||||
if( styleSheet.getPointSize( 7 ) != 36f ||
|
||||
styleSheet.getPointSize( 4 ) == fontBaseSize )
|
||||
font == null || styleSheet.getPointSize( 4 ) == font.getSize() )
|
||||
return;
|
||||
|
||||
// check whether view uses "absolute-size" keywords (e.g. "x-large") for font-size
|
||||
@@ -97,7 +94,7 @@ debug*/
|
||||
return;
|
||||
|
||||
// BASE_SIZE rule is parsed in javax.swing.text.html.StyleSheet.addRule()
|
||||
String style = "<style>BASE_SIZE " + c.getFont().getSize() + "</style>";
|
||||
String style = "<style>BASE_SIZE " + font.getSize() + "</style>";
|
||||
String openTag = "";
|
||||
String closeTag = "";
|
||||
|
||||
|
||||
@@ -16,11 +16,8 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.Locale;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.*;
|
||||
import javax.swing.plaf.basic.BasicHTML;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.Document;
|
||||
@@ -28,6 +25,8 @@ import javax.swing.text.View;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Karl Tauber
|
||||
@@ -73,6 +72,15 @@ public class TestFlatHTML
|
||||
testHtmlBaseSize( "<html>${BASE_SIZE}<style type='text/css'>body { color: #f00; }</style><h1>header1</h1>" + body + "</html>", "header1\n" + bodyPlain );
|
||||
}
|
||||
|
||||
@Test
|
||||
void htmlOnComponentWithNullFont() {
|
||||
assertDoesNotThrow( () -> {
|
||||
JLabel label = new JLabel();
|
||||
label.setFont( null );
|
||||
label.setText( "<html>foo<br>bar</html>" );
|
||||
} );
|
||||
}
|
||||
|
||||
private void testHtmlBaseSize( String html, String expectedPlain ) {
|
||||
testHtmlBaseSizeImpl( html, expectedPlain );
|
||||
testHtmlBaseSizeImpl( html.toUpperCase( Locale.ENGLISH ), expectedPlain.toUpperCase( Locale.ENGLISH ) );
|
||||
|
||||
Reference in New Issue
Block a user