title: Insert HTML into the Body of an HTMLDocument
body: <p>This seems like such a simple question, but I'm having such difficulty with it.</p>
  
  <h2>Problem:</h2>
  
  <p>I have some text to insert into an <code>HTMLDocument</code>.  This text sometimes specifies some html as well. E.G.:</p>
  
  <pre><code>Some &lt;br /&gt;Random &lt;b&gt;HTML&lt;/b&gt;
  </code></pre>
  
  <p>I'm using <code>HTMLEditorKit.insertHTML</code> to insert it at a specified offset.  This works fine, unless the offset is at the begining of the doc (offset = 1).  When this is the case the text gets inserted into the <code>head</code> of the document instead of the <code>body</code>.</p>
  
  <h2>Example:</h2>
  
  <pre><code>editorKitInstance.insertHTML(doc, offset, "&lt;font&gt;"+stringToInsert+"&lt;/font&gt;", 0, 0, HTML.Tag.FONT);
  </code></pre>
  
  <p>I use the font tag so I now what I'm inserting will be in a font tag with no attributes so it won't effect the format.  I need to know this because the last parameter, <code>insertTag</code>, is required and I can't know the contents of <code>stringToInsert</code> until runtime.  If there is already text in the doc (such as "1234567890") then this is the output:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p style="margin-top: 0"&gt;
        1234567890 &lt;font&gt;something &lt;br /&gt;Some &lt;br /&gt;Random &lt;b&gt;HTML&lt;/b&gt;&lt;/font&gt;
      &lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>However if the offset is 1 and the document is empty this is the result:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
  
  &lt;font&gt;Some &lt;br /&gt;Random &lt;b&gt;HTML&lt;/b&gt;&lt;/font&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>Other Notes:</h2>
  
  <ul>
  <li>This is all being done on the
  innerdocument of a <code>JEditorPane</code>.  If
  there is a better way to replace text
  in a <code>JEditorPane</code> with potential
  HTML I would be open to those ideas
  as well.</li>
  </ul>
  
  <p>Any help would be appreciated.  <strong>Thanks!</strong></p>