Robbie Robbie - 1 year ago 32
HTML Question

Insert HTML into the Body of an HTMLDocument

This seems like such a simple question, but I'm having such difficulty with it.


I have some text to insert into an
. This text sometimes specifies some html as well. E.G.:

Some <br />Random <b>HTML</b>

I'm using
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
of the document instead of the


editorKitInstance.insertHTML(doc, offset, "<font>"+stringToInsert+"</font>", 0, 0, HTML.Tag.FONT);

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,
, is required and I can't know the contents of
until runtime. If there is already text in the doc (such as "1234567890") then this is the output:


<p style="margin-top: 0">
1234567890 <font>something <br />Some <br />Random <b>HTML</b></font>

However if the offset is 1 and the document is empty this is the result:


<font>Some <br />Random <b>HTML</b></font>

Other Notes:

  • This is all being done on the
    innerdocument of a
    . If
    there is a better way to replace text
    in a
    with potential
    HTML I would be open to those ideas
    as well.

Any help would be appreciated. Thanks!

Answer Source

There are several things you should know about the internal structure of the HTMLDocument.

  • First of all - the body does not start at position 0. All textual content of the document is stored in an instance of javax.swing.text.AbstractDocument$Content. This includes the title and script tags as well. The position/offset argument of ANY document and editor kit function refers to the text in this Content instance! You have to determine the start of the body element to correctly insert content into the body. BTW: Even though you didn't define a body element in your HTML, it will auto-generated by the parser.
  • Simply inserting at a position tends to have unexpected side effects. You need to know where you want to put the content in relation to the (HTML) elements at this position. E.g. if you have the following text in your document: "...</span><span>..." - there is only one position (referring to the Content instance) for "at the end of the first span", "between the spans" and "at the start of the second span". To solve this problem there are 4 functions in the HTMLDocument API:
    • insertAfterEnd
    • insertAfterStart
    • insertBeforeEnd
    • insertBeforeStart

As a conclusion: for a general solutions, you have to find the BODY element to tell the document to "insertAfterStart" of the body and at the start offset of the body element.

The following snipped should work in any case:

HTMLDocument htmlDoc = ...;
Element[] roots = htmlDoc.getRootElements(); // #0 is the HTML element, #1 the bidi-root
Element body = null;
for( int i = 0; i < roots[0].getElementCount(); i++ ) {
    Element element = roots[0].getElement( i );
    if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
        body = element;
htmlDoc.insertAfterStart( body, "<font>text</font>" );

If you're sure that the header is always empty, there is another way: new StringReader( "<font>test</font>" ), htmlDoc, 1 );

But this will throw a RuntimeException, if the header is not empty.

By the way, I prefer to use JWebEngine to handle and render HTML content since it keeps header and content separated, so inserting at position 0 always works.