Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8300183

JEditorPane does not honor font-size definitions in external css file

XMLWordPrintable

    • x86_64
    • windows_10
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 / Oracle JDK 17.0.5

      A DESCRIPTION OF THE PROBLEM :
      When loading an HTML page containing a link to an external css-file. The font-size definitions in the external css file are ignored in the rendered output.

      REGRESSION : Last worked in version 11.0.17

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the test code provided in the source code section to load the html-file external-css-test.html (javaw.exe -classpath csstest.jar test.CSSTest file:///<path-to>/external-css-test.html)
      The content of the files external-css-test.html and external-css.css is provided in the source section. The files should be located in the same directory.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Font-size definitions from css should be respected by the rendered output.

      ACTUAL -
      All fonts sizes are equal in the output despite the different definitions in the css file.

      ---------- BEGIN SOURCE ----------
      CSSTest.java
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      package test;

      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.event.ActionEvent;
      import java.awt.event.KeyEvent;
      import java.io.IOException;

      import javax.swing.AbstractAction;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.KeyStroke;
      import javax.swing.SwingUtilities;
      import javax.swing.text.Document;

      @SuppressWarnings ("serial")
      public class CSSTest
         extends JFrame
      {

         private final JEditorPane editor;

         public CSSTest (String url)
         {
            super("External CSS Test");

            Container contentPane = super.getContentPane();
            editor = new JEditorPane();
            editor.setEditable(false);
            editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
            editor.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, false);
            contentPane.add(new JScrollPane(editor), BorderLayout.CENTER);

            editor.getActionMap().put("Reload-Page", new AbstractAction("Reload-Page")
            {
               @Override
               public void actionPerformed (final ActionEvent evt)
               {
                  loadPage(url);
               }
            });
            editor.getActionMap().put("Debug-Page", new AbstractAction("Debug-Page")
            {
               @Override
               public void actionPerformed (final ActionEvent evt)
               {
                  debugPage();
               }
            });

            KeyStroke ctrlR = KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK);
            editor.getInputMap().put(ctrlR, "Reload-Page");

            KeyStroke ctrlShiftD = KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
            editor.getInputMap().put(ctrlShiftD, "Debug-Page");

            loadPage(url);
         }

         void loadPage (String url)
         {
            try
            {
               editor.setContentType("text/html");
               editor.getDocument().putProperty(Document.StreamDescriptionProperty, null); // Reload
               editor.setPage(url);
            }
            catch (IOException e)
            {
               editor.setContentType("text/plain");
               editor.setText(e.toString());
               editor.setForeground(Color.red);
            }
         }

         void debugPage ()
         {
            System.out.println(editor.getDocument().getDefaultRootElement());
            System.out.println(editor.getUI().getRootView(editor));
         }

         public static void main (final String[] args)
            throws Exception
         {
            SwingUtilities.invokeLater( () ->
            {
               CSSTest frame = new CSSTest(args[0]);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setSize(500, 800);
               frame.setLocation(0, 0);
               frame.setVisible(true);
            });
         }
      }
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      external-css-test.html
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      <html>
         <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" type="text/css" href="external-css.css">
         </head>
         <body>
            <p class="help_title">Help Title</p>
            <hr>
            <p class="help_subtitle">Help Subtitle</p>
            <p class="help_text">Help text text</p>
            <p class="help_id">Help Id<br>
            <hr>
            <p class="copyright_text">Coyright</p>
            <br></p>
         </body>
      </html>
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      external-css.css
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      p {margin:0; padding:0;}

      .help_title
      {
        font-family: Arial;
        font-size: 24pt;
        font-weight: bold;
        margin-top:16;
        margin-left:10;
        margin-bottom:16;
      }

      .help_subtitle
      {
        font-family: Serif;
        font-size: 14pt;
        font-weight: bold;
        margin-top:5;
        margin-left:20;
        margin-bottom:5;
      }

      .help_text
      {
        font-family: Serif;
        font-size: 12pt;
        margin-bottom:15;
        margin-left:20;
      }


      .help_id
      {
        font-family: Serif;
        font-size: 9pt;
        margin-left:20;
      }

      .copyright_text
      {
        font-family: Serif;
        font-size: 9pt;
        margin-left:10;
      }
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      ---------- END SOURCE ----------

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: