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

insertHTML with offset set to 0 inserts text in <head> of document.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs

      FULL PRODUCT VERSION :
      Sun J2SDK 1.4.2_02

      ADDITIONAL OS VERSION INFORMATION :
      Windows 2000 : Version 5.00.21.2195

      Linux <machinename> 2.4.22 #8 Tue Sep 2 17:51:33 PDT 2003 i686 unknown unknown GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      We are calling insertHTML on an HTMLEditorKit, passing doc.getSelectionStart() as the offset. If the cursor is at the very start of the text in the editor pane, getSelectionStart() is returning 1. The text passed into insertHTML is then inserted into the <head> section of the document and does not display in the editor pane. If you were to press <delete> at the start of the text in the editor pane, however, getSelectionStart() returns 0 and the text is inserted into the body of the document.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      As outlined in description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Text should always be inserted into the body of the document.
      ACTUAL -
      As outlined in the description.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;

      import java.awt.event.*;

      import javax.swing.*;

      import javax.swing.text.html.*;

      import javax.swing.text.*;

      import java.util.*;

      import java.io.*;



      public class breakTest extends JFrame

      {

      private JEditorPane pane;

      private JButton dump;

      private JToolBar tb;

      private HTMLEditorKit kit;

      private HTMLDocument doc;



      public breakTest()

      {

          super("test");

          init();

      }

      public void init()

      {

      pane = new JEditorPane();

      kit = (HTMLEditorKit)(pane.getEditorKitForContentType("text/html"));

      doc = (HTMLDocument)(kit.createDefaultDocument());



      pane.setEditorKit(kit);

      pane.setDocument(doc);

      tb = new JToolBar(JToolBar.HORIZONTAL);

      pane.setText("Test\nHello");



      dump=new JButton("Dump");




      dump.addActionListener(new ActionListener()

      {

      public void actionPerformed(ActionEvent e)

      {

      System.out.println(pane.getText());

      }

      });

      tb.add(dump);

      setSize(400,400);

      Container c = getContentPane();

      c.setLayout(new BorderLayout());

      c.add(tb,"North");

      c.add(new JScrollPane(pane),"Center");



      breakAction brAction = new breakAction();

      Keymap map = pane.getKeymap();

      KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 1);

      map.removeKeyStrokeBinding(key);

      map.addActionForKeyStroke(key, brAction);

      }




      //inner class for break action

      class breakAction extends AbstractAction

      {

      public breakAction()

      {

      super();

      }

      public void actionPerformed(ActionEvent event)

      {

          System.out.println("BREAK");

      int offset = pane.getSelectionStart();

      System.out.println("offset="+offset);

      HTMLDocument doc = (HTMLDocument)pane.getDocument();

      EditorKit kit = pane.getEditorKit();

      if(kit instanceof HTMLEditorKit){

      try{



      ((HTMLEditorKit)kit).insertHTML(doc, offset, "<br>", 0, 0, HTML.Tag.BR);




      pane.requestFocus();

      pane.setCaretPosition(offset+1);

      }catch(Exception e){

      }}}}



      public static void main(String args[])

      {

          breakTest test = new breakTest();

          test.setVisible(true);

      }

      }


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

      CUSTOMER SUBMITTED WORKAROUND :
      Haven't found one yet !!!!

            peterz Peter Zhelezniakov
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: