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

Cannot set/insert HTML comments in JEditorPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • generic
    • generic



      Name: yyT116575 Date: 10/25/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      HTML comments cannot be inserted into, or set within, a JEditorPane.

      The following program demonstrates what happens when a JEditorPane is given a
      HTML comment using the setText() method:

      // HTMLCommentTest

      import java.awt.event.*;
      import java.io.*;
      import java.util.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;

      public class HTMLCommentTest extends JFrame
      {
      JEditorPane editorPane;

      public HTMLCommentTest()
      {
      super("HTMLCommentTest");
      setBounds(200, 100, 800, 600);
      addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent event)
      {
      System.exit(0);
      }
      });

      editorPane = new JEditorPane();
      editorPane.setContentType("text/html");
      setContentPane(editorPane);

      HTMLEditorKit kit = (HTMLEditorKit) editorPane.getEditorKit();
      HTMLDocument document = (HTMLDocument) editorPane.getDocument();

      try
      {
      System.out.println("\nBefore:");
      kit.write(System.out, document, 0, document.getLength());

      editorPane.setText("<!-- comment -->");

      System.out.println("\nAfter:");
      kit.write(System.out, document, 0, document.getLength());
      }
      catch (Exception exception)
      {
      System.out.println(exception);
      }
      }

      public static void main(String[] arguments)
      {
      (new HTMLCommentTest()).show();
      }
      }

      The program writes out the document before and after the setText() method is
      invoked with a valid HTML comment. Before the method is called, the document is
      a default HTMLDocument:

          <html><head></head><body><p></p></body></html>

      After setText("<!-- comment -->") is called, the document is as follows:

          <paragraph><head></head><body><p></p></body></paragraph><!-- comment -->

      A similar problem occurs when inserting a comment into an existing document.
      Try using the following line instead of the setText() line:

          kit.insertHTML(document, 0, "<!-- comment -->", 0, 0, HTML.Tag.COMMENT);

      And this produces the following invalid document:

          <html><head></head><body><p></p></body></html><!-- comment -->
      (Review ID: 110766)
      ======================================================================

      Name: yyT116575 Date: 11/20/2000


      E:\>java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      also reproduced on

      jmb@acid:htmlviewer/[293]% java -version
      java version "1.3.0beta_refresh"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0beta_refresh-b09)
      Java HotSpot(TM) Client VM (build 1.3.0beta-b07, mixed mode)


      The following sample of html produces column alignment errors when rendered
      in a JEditorPane. It seems that the comment tag is taken as occupying a
      column, when it should be entirely ignored (cf any other browser).

      This is a problem in particular because server-side CGI-generated html
      often includes comments sprinkled liberally around the html.


      <html><head>
      <title>JEditorPane table bug demo</title>
      </head><body>

      <table border>
      <tr>
       <th>corner</th>
       <th>first column heading</th>
       <th>second column heading</th>
       <th>third column heading</th>
       <th>fourth column heading</th>
      </tr>

      <tr> <!-- this comment pushes "row heading" under "first column heading" -->
       <th>row heading</th>
       <td>first data column</td>
       <td>second data column</td>
       <td>third data column</td>
      </tr>
      </table>

      </body>
      </html>


      I don't think the usage of JEditorPane is relevant, but here's one little app
      that does the job of showing the problem:

      /////////////////////////////////////////////////////////////////////////////
      // HtmlViewer - create a JEditorPane with html content

      import java.io.IOException;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;
      import javax.swing.*;
      import javax.swing.event.*;

      public class HtmlViewer {
          String url;
          JEditorPane htmlPane;

          public static void main(String[] args) {
      String url;
      if (args.length > 0)
      url = args[0];
      else url = "file:test.html";

      HtmlViewer app = new HtmlViewer(url);
      app.createFrame();
          }


          public HtmlViewer(String url) {
      this.url = url;
          }

          void createFrame() {
      htmlPane = new JEditorPane();

      try {
      htmlPane.setEditable(false);
      htmlPane.setContentType("text/html");
      htmlPane.setPage(url);
      } catch (Exception ioException) {
      System.out.println(ioException);
      }

              JScrollPane scroller = new JScrollPane(htmlPane);
      scroller.setPreferredSize(new Dimension(600, 400));

              JFrame frame = new JFrame("HtmlViewer");
              frame.getContentPane().add(scroller, BorderLayout.CENTER);

              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
              frame.pack();
              frame.setVisible(true);
          }

      }//class HtmlViewer
      (Review ID: 112331)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: