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

Html elements don't pick up the styles from their class if id attribute is also

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 5.0
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When an HTML document has elements with both id and class attributes, those elements will ignore CSS styles they should inherit through their class attribute, even when no styles are associated with the id.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code included below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The lines "test1" and "test3" should both be in the same colour and font, since they belong to the same class.
      ACTUAL -
      Notice that the line "test3" will be in the same colour and font as "test2", which is the basic style for the body.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Dimension;

      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;
      import javax.swing.text.html.HTMLEditorKit;
      import javax.swing.text.html.StyleSheet;

      public class Main extends JFrame {
          private JEditorPane editor;

          Main(String title) {
              super(title);
              
              createContents();

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setPreferredSize(new Dimension (300, 500));
              pack ();
              setLocationRelativeTo (null);
          }
          
          private void createContents() {
              HTMLEditorKit editorKit = new HTMLEditorKit();
              editorKit.setStyleSheet(getStyleSheet());

              editor = new JEditorPane();
              editor.setContentType("text/html");
              editor.setDocument(editorKit.createDefaultDocument());
              editor.setEditable(false);
              
              getContentPane().setLayout(new BorderLayout());
              getContentPane().add(editor,BorderLayout.CENTER);
              
              editor.setText("<html><body>" +
                      "<div class=\"foo\"><p>test1</p></div>" +
                      "<div id=\"bar\"><p>test2</p></div>" +
                      "<div class=\"foo\" id=\"baz\"><p>test3</p></div>" +
                      "</body></html>");
              System.out.println(editor.getText());
          }
          
          private StyleSheet getStyleSheet() {
              StyleSheet styleSheet = new StyleSheet();
              styleSheet.addRule("body {; font-family: Tahoma, Lucida Grande, Sans Serif; font-size: 12pt; font-style: normal; font-weight: normal;}");
              styleSheet.addRule(".foo {font-weight: bold; background-color: #e0e0e0}");
              
              return styleSheet;
          }
          
          public static void main (String[] args) {
              SwingUtilities.invokeLater (new Runnable () {
                  public void run () {
                      Main frame = new Main("Demo");
                      frame.setVisible (true);
                  }
              });
          }
      }

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

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: