title: JEditorPane, HTMLEditorKit - custom action inserting custom tag
body: <p>I'm faithing with <code>JEditorPane</code>. I need simple editor. I've solved the problem with loading and modified HTML containing custom (two) tags (see <a href="https://stackoverflow.com/questions/7526883/jeditorpane-and-custom-editor-kit">my older post</a>). It displays the document properly and I can even edit it now. I can write text, delete either characters or my custom elements. I won a battle, but haven't won the war. The next step is regrettably very problematical too. I'm unable to insert my custom tags.</p>
  
  <p>I have a custom action:</p>
  
  <pre><code>import my.own.HTMLEditorKit; //extends standard HTMLEditorKit
  import my.own.HTMLDocument; //extends standard HTMLDocument
  
  class InsertElementAction extends StyledTextAction {
      private static final long serialVersionUID = 1L;
  
      public InsertElementAction(String actionName) {
          super(actionName);
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
          JEditorPane editor = getEditor(e);
  
          if (editor == null)
              return;
  
          HTMLDocument doc = (HTMLDocument) editor.getDocument();
          HTMLEditorKit ekit = (HTMLEditorKit) editor.getEditorKit();
          int offset = editor.getSelectionStart();
  
          try {
              ekit.insertHTML(doc, offset, "&lt;span&gt;ahoj&lt;/span&gt;", 0, 0, HTML.Tag.SPAN);
              Element ele = doc.getRootElements()[0];
              ele = ele.getElement(1).getElement(0);
              doc.setInnerHTML(ele, "&lt;bar medium=\"#DEFAULT\" type=\"packaged\" source=\"identifier\" /&gt;");
          }
          catch (BadLocationException ble) {
              throw new Error(ble);
          }
          catch (IOException ioe) {
              throw new Error(ioe);
          }
      }
  }
  </code></pre>
  
  <p>It workts properly. I can insert the <code>span</code> element. But I cannot insert non-standard tag in this way. I can insert just <code>code</code>, <code>span</code> and so on, but not my tag. For my tag I'm forced to use this:</p>
  
  <pre><code>ekit.insertHTML(doc, offset, "x&lt;bar medium=\"#DEFAULT\" type=\"packaged\" source=\"identifier\" /&gt;x", 0, 0, null);
  </code></pre>
  
  <p>There are two critical problems</p>
  
  <ol>
  <li>The custom tag must be bounded with non-whispace characters (here x)</li>
  <li>The current element's body is split</li>
  </ol>
  
  <p>When I insert <code>span</code> element into <code>&lt;p&gt;paragraph&lt;/p&gt;</code>, I get <code>&lt;p&gt;par&lt;span&gt;ahoj&lt;/span&gt;agraph&lt;/p&gt;</code> as expected. Howerever unknown tag is allways inserted as child of <code>body</code> element and the result (e.g. for unknown tag <code>x</code>) is <code>&lt;p&gt;par&lt;/p&gt;&lt;x&gt;ahoj&lt;/x&gt;&lt;p&gt;agraph&lt;/p&gt;</code>.</p>
  
  <p>The work is dead exhausting. I'm faithing with this relatively simple task since weeks. I'm already wasted. If the insertion won't to work, I can scrap it all...</p>