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

HTMLDocument fails to insert tag "meta"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8
    • client-libs
    • None
    • JDK7u21

      Class javax.swing.text.html.HTMLDocument allows to edit the document.
      Particularly it contains the method:

        public void insertBeforeEnd(Element elem, String htmlText)

      However with this facility one can not insert a tag <meta> into the tag <head>. Please see the sample code and the output below.

      -------------------------------------------------------------------------------------------------------------------------------------
      import java.io.CharArrayReader;
      import java.io.CharArrayWriter;
      import javax.swing.text.Element;
      import javax.swing.text.ElementIterator;
      import javax.swing.text.html.HTML;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;

      public class InsertMetaTag {
          
          public static void main(String[] args) throws Exception {
              String htmlDoc = "<html><head><title>Sample</title></head><body>Any html</body></html>";

              HTMLEditorKit kit = new HTMLEditorKit();
              HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
              HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
              doc.getParser().parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
              htmlReader.flush();

              ElementIterator it = new ElementIterator(doc);
              Element el;
              while ( (el = it.next()) != null ) {
                  if ("head".equals(el.getName())) {
                      String meta = "<meta content='THIS DOES NOT GET INSERTED!' name='any'>";
                      System.out.println("Attempting to insert: " + meta);
                      System.out.println("into: " + htmlDoc);
                      // None of these works:
                      doc.insertBeforeEnd(el, meta);
      // doc.insertAfterStart(el, htmlDoc);
                      // This does not work either:
      // kit.insertHTML(doc, el.getStartOffset(), meta, 0, 0, HTML.Tag.META);
                      // Surprisingly, but this works:
      // kit.insertHTML(doc, el.getStartOffset(), "<head>" + meta + "</head>", 0, 0, HTML.Tag.META);
                      break;
                  }
              }

              CharArrayWriter writer = new CharArrayWriter(1000);
              kit.write(writer, doc, 0, doc.getLength());
              writer.flush();
              String result = writer.toString();
              System.out.println(result);
          }
      }
      -------------------------------------------------------------------------------------------------------------------------------------

      This prints:
      -------------------------------------------------------------------------------------------------------------------------------------
      Attempting to insert: <meta content='THIS DOES NOT GET INSERTED!' name='any'>
      into: <html><head><title>Sample</title></head><body>Any html</body></html>
      <html>
        <head>
          <title>Sample</title>
          
        </head>
        <body>
          Any html
        </body>
      </html>
      -------------------------------------------------------------------------------------------------------------------------------------

            Unassigned Unassigned
            ashusher Alexander Shusherov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: