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

paragraphs ids duplicated when using HTMLDocument with JTextPane

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_21"
      Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      If you use a HTMLDocument inside a JTextPane,
      the id of current paragraph is propagated to the newly created paragraph when pressing return key :
      <p id="test">test</p><p id="test"></p>

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a JTextPane, set an HTMLDocument as document, load this html inside :
       '<html><head></head><body><p id="test">test</p></body></html>'

      Then, press return key after "test" to create a new paragraph. The document now contains :

       '<html><head></head><body><p id="test">test</p><p id="test"></p></body></html>'

      The id has been duplicated which is, of course, unexpected

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
       '<html><head></head><body><p id="test">test</p><p></p></body></html>'
      ACTUAL -
      <html><head></head><body><p id="test">test</p><p id="test"></p></body></html>

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Main {
          public static void main(String[] args) throws IOException {
              new TestFrame();
          }
          static Editor editor = new Editor();
          static class TestFrame extends JFrame {
              TestFrame() {
                  setContentPane(editor);
                  setJMenuBar(new Menu());
                  setSize(300,300);
                  setVisible(true);
                  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              }
          }
          
          static class Editor extends JTextPane {
              HTMLDocument htmlDoc;
              HTMLEditorKit editorKit;
              Editor() {
                  editorKit = new HTMLEditorKit();
                  setEditorKit(editorKit);
                  
                  htmlDoc = (HTMLDocument) editorKit.createDefaultDocument();
                  htmlDoc.setPreservesUnknownTags(false);
                  htmlDoc.putProperty("IgnoreCharsetDirective", true);

                  this.setDocument(htmlDoc);
                  this.setEditable(true);
                  this.setContentType("text/html");
                  
                  try {
                      editorKit.insertHTML(htmlDoc, 0, "<p id='test'>a</p>", 1, 0, HTML.Tag.P);
                  } catch (BadLocationException ex) {
                      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (IOException ex) {
                      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  
              }
          }
          static class Menu extends JMenuBar {
              Menu() {
                  add(new JButton(new AbstractAction("print") {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          try {
                              StringWriter w = new StringWriter();
                              new HTMLEditorKit().write(w, editor.htmlDoc, 0, editor.htmlDoc.getLength());
                              System.out.println(w.toString());
                          } catch (IOException ex) {
                              Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                          } catch (BadLocationException ex) {
                              Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                          }
                      }
                  }));
              }
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Couldn't find any

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: