I'm using this native javascript method in GWT to insert text at the cursor position of a RichTextArea. It works sometimes but often gives me this error message: "refNode.insertData is not a function. It seems to happen whenever the cursor is on a blank line.

```
public native void insertText(String text, int pos) /*-{
    var elem = this.@com.google.gwt.user.client.ui.UIObject::getElement()();
    var refNode = elem.contentWindow.getSelection().getRangeAt(0).endContainer;
    refNode.insertData(pos, text);
}-*/;

```

So I need to debug this javascript and don't know where to begin. I know very little about javascript and only got this method I'm using off a stack [question](https://stackoverflow.com/questions/16818520/inserthtml-for-gwt-richtextarea-without-exiting-formatting). I'm getting the cursor position from another native method I copied from this [question](https://stackoverflow.com/questions/13491259/cursor-position-in-richtexteditor).

I read that this error is because refNode isn't the correct type of object. I figured somebody would know what type of object it actually is and can help me handle this situation.