Steve Hobbs
Steve Hobbs

Reputation: 3326

tinymce.dom.replace throws an exception concerning parentNode

I'm writing a tinyMce plugin which contains a section of code, replacing one element for another. I'm using the editor's dom instance to create the node I want to insert, and I'm using the same instance to do the replacement.

My code is as follows:

var nodeData =
  {
    "data-widgetId": data.widget.widgetKey(),
    "data-instanceKey": "instance1",
    src: "/content/images/icon48/cog.png",
    class: "widgetPlaceholder",
    title: data.widget.getInfo().name
  };

var nodeToInsert = ed.dom.create("img", nodeData);

// Insert this content into the editor window
if (data.mode == 'add') {
  tinymce.DOM.add(ed.getBody(), nodeToInsert);
}
else if (data.mode == 'edit' && data.selected != null) {

  var instanceKey = $(data.selected).attr("data-instancekey");
  var elementToReplace = tinymce.DOM.select("[data-instancekey=" + instanceKey + "]");

  if (elementToReplace.length === 1) {
    ed.dom.replace(elementToReplace[0], nodeToInsert);
  }
  else {
    throw new "No element to replace with that instance key";
  }
}

TinyMCE breaks during the replace, here:

replace : function(n, o, k) {
  var t = this;

  if (is(o, 'array'))
    n = n.cloneNode(true);

  return t.run(o, function(o) {
    if (k) {
      each(tinymce.grep(o.childNodes), function(c) {
        n.appendChild(c);
      });
    }

    return o.parentNode.replaceChild(n, o);
  });
},

..with the error Cannot call method 'replaceChild' of null.

I've verified that the two argument's being passed into replace() are not null and that their parentNode fields are instantiated. I've also taken care to make sure that the elements are being created and replace using the same document instance (I understand I.E has an issue with this).

I've done all this development in Google Chrome, but I receive the same errors in Firefox 4 and IE8 also. Has anyone else come across this?

Thanks in advance

Upvotes: 1

Views: 1146

Answers (1)

Steve Hobbs
Steve Hobbs

Reputation: 3326

As it turns out, I was simply passing in the arguments in the wrong order. I should have been passing the node I wanted to insert first, and the node I wanted to replace second.

Upvotes: 1

Related Questions