1

Topic: TinyMCE causing exceptions when initialized second time in one page

Nov 3, 2009: update below

In short:
When I attach an editor to a dynamically created textarea the second time, it doesn't load the contents of the textarea, and causes a bunch of exceptions.

In detail:
My CMS allows the user to instantiate TinyMCE multiple times on one page, but only once at a time.  When the user chooses to "Edit > Contents", a javascript file and html file are loaded via AJAX (using the jQuery library).  The newly loaded Javascript file contains code that initializes an editor instance, and attaches it to a textarea that is contained in the newly loaded html file.  I've got other Javascript code that populates the content of the textarea, before the editor is attached to it. If it matters, the tiny_mce.js library is only loaded once on page load. It isn't loaded dynamically.

The first time I initialize the editor, everything works perfectly.  I can click all the editor buttons, the editor contains the appropriate content, I can retrieve the content from it (using getContent()), and so on.

When I close the modal box that contains the editor, I call various removal & deletion commands in hopes of completely obliterating any occurrence of the editor:

mce.remove(); //mce is the editor
tinyMCE.execCommand('mceRemoveControl', true, 'vik-content-field'); //vik-content-field is the id of the textarea it gets attached to
delete mce;

I'm guessing none of those fully work, because poop hits the fan the next time I try to "Edit > Contents".

The second time, the editor loads up, but the content isn't there.  If I try to edit the contents via the paste-as-text button, or the table button (I can't click inside the editor to type), I get a Javascript exception. 

I'm using the latest version of TinyMCE which I downloaded today.  I've only been testing in FF 3.5.

Absolutely any insight would be appreciated.  I thought I'd move my CMS from FCKEditor to TinyMCE, but it's taken me 3 hours (so far) on this one problem.

Last edited by quasipickle (2009-11-04 06:37:28)

2

Re: TinyMCE causing exceptions when initialized second time in one page

Check --> http://crowded.se/test/c.php
I'm doing something similar there. Did you try to reorder thhe way you do things? I se you remove the actual textarea before you remove the editor, not sure if that's what's causing the problems.

3

Re: TinyMCE causing exceptions when initialized second time in one page

I actually hide the textarea, remove tinyMCE, then remove the textarea.  Re-ordering (removing the textarea first, then the editor) didn't do anything different.

4

Re: TinyMCE causing exceptions when initialized second time in one page

After some debuging, I've found that tinyMCE does know about the content it's supposed to be displaying & it does load it into the tinyMCE object, but it just never injects it into the iframe.  I'm still trying to figure out where that goes south.  I'm guessing/hoping/praying if I can figure that out, the rest will just work.

I've changed to using tiny_mce_src.js in an attempt to figure out what's going on.  Right now, when I call tinyMCE.activeEditor.getContent() it throws this exception:

uncaught exception: [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIDOMHTMLDocument.implementation]" nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)" location: "JS frame :: http://myserver.com/_include/path/to//tiny_mce_src.js :: anonymous :: line 5366" data: no]

Line 5366 in my source file (which may be slightly different than the package because I've been throwing console.debug() everywhere) is:

impl = n.ownerDocument.implementation;

in the Serializer.serialize() function.  When I rewrite that part so this particular line isn't called, I don't get an exception.

Obviously this is a symptom of the problem, not the cause.  I'm hoping someone with more experience with the internals of tinyMCE & Javascript can use this to help point to the problem.

Last edited by quasipickle (2009-11-04 06:37:15)

5

Re: TinyMCE causing exceptions when initialized second time in one page

i have the same problem at the moment. i'm loading tinymce via ajax and after first usage tinymce throws this strange exception.

perhaps there is an error in firefox because opera, safari and chrome do not have this problem. in these browser all is okay, even in internet explorer 8 there all is ok :-/ i'm very confused about this.

@Chrazy: i don't think that you are doing the same. you are not loading tinymce via ajax, i believe.

6

Re: TinyMCE causing exceptions when initialized second time in one page

If you clobber TinyMCE instances errors like this might happen. Use the proper API by removing the editors.

Best regards,
Spocke - Main developer of TinyMCE

7

Re: TinyMCE causing exceptions when initialized second time in one page

I'm thinking it's Firefox too.  Safari handles it fine.

8

Re: TinyMCE causing exceptions when initialized second time in one page

but why? i testing new installations of firefox and will post it here in a minute... lets try to fix it wink

// edit
the problem does not exist in every firefox. i have two computers, in one ff the error comes up, in the other not :-/ very strange....

Last edited by g3ck0 (2009-11-11 01:56:56)

9

Re: TinyMCE causing exceptions when initialized second time in one page

spocke wrote:

If you clobber TinyMCE instances errors like this might happen. Use the proper API by removing the editors.

Mind pointing me to what the proper APIs are?  I've looked through the documentation & these forums, and the only methods mentioned that I could find were what I tried in the beginning.

10

Re: TinyMCE causing exceptions when initialized second time in one page

i have no remove-method or something like that. in all cases the use of tinymce is okay. even in an other firefox with the same addons. i don't know if the addon-settings are the same but this is very strange. i don't think that there might be an api problem because then there has to always the same error, perhaps even in opera/safari/chrome/ie.

// edit
it's very interessting... when this error occurs than i can't even execute some other javascript-functions. but some are working.

for your information: i have written my own dialog with an overlay. in this dialog html gets loaded via ajax. in some cases the tinymce replaces a textarea in this html, but not in every case. i have also some other functionalities on my page, like delete or move up/down. when i open a update-dialog (with tinymce), close it again and open it once again, the tinymce is loaded but i can't entry any data and in the firefox console (addon firebug) there occurs the error described in the first post. then i close the dialog. i have one more dialog but this one has nothing to do with tinymce. there are only input field. when the error occurs i have not the possibilty to open the dialog again. i don't know why. my code is correct and does not load the tinymce again :-/ but other methods like (ajax-)delete or move are okay.

// edit(2)
i have included the remove-command but nothing happend. the error is there!

if ((tinyMCE != undefined) && (tinyMCE.activeEditor != undefined))
{
    alert('remove-tiny-mce');
    tinyMCE.activeEditor.remove();
}

// edit(3)
i have to correct me! this was th error! i have not called this remove-method at every position. now after some extended tests i cannot reproduce this strange error. thanks a lot! now i can go to bed big_smile

Last edited by g3ck0 (2009-11-11 02:50:28)

11

Re: TinyMCE causing exceptions when initialized second time in one page

@quasipickle: please post your complete method how you call the tinymce!

my problem isn't solved. i don't know why but i think that it hast to do with the toolbar and/or the plugins behind.

// edit
@spocke: have a look at this image. the error comes up before the editor gets completly removed!

http://img689.imageshack.us/img689/7562/screenshotx.png

and this is my code:

if ((tinyMCE != undefined) && (tinyMCE.activeEditor != undefined))
{
    tinyMCE.activeEditor.onRemove.add(function(ed) {
        console.debug('Editor was removed: ' + ed.id);
    });
    console.debug('start removing: ' + tinyMCE.activeEditor.id);
    tinyMCE.activeEditor.remove();
}

now this is your turn wink

Last edited by g3ck0 (2009-11-11 03:27:08)

12

Re: TinyMCE causing exceptions when initialized second time in one page

Here's my init code:

tinyMCE.init({
        button_tile_map:true,
        content_css: SITE_SUBDIR+'/_include/templates/slmpc/editor.css',
        convert_urls: false,
        document_base_url: 'http://www.mydomain.ca/',
        file_browser_callback:function(field_name,url,type,win){
            tinyMCE.activeEditor.windowManager.open({
                file: '/'+SITE_SUBDIR+'_admin/fileManager/index.php?type='+type,
                title:"File/Image browser",
                width:600,
                height:400,
                resizable: "yes",
                close_previous: "no"
            },
            {
                window: win,
                input: field_name
            });
        },
        height:400,
        mode:"textareas",
        plugins:"fullscreen,table,searchreplace,paste,advimage",
        relative_urls: false,
        setup:function(ed){
            ed.onInit.add(function(ed){
                $vik("#vik-editor-loading").slideUp().hide();
                $vik("#vik-content-wrapper").slideDown('normal');
            });
        },
        theme:'advanced',
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_blockformats:"p,h1,h2,h3,h4,h5,h6",
        theme_advanced_buttons1 : "undo,redo,|,cut,copy,paste,pastetext,searchreplace,|,code,cleanup,fullscreen,visualaid",
        theme_advanced_buttons2 : "bold,italic,strikethrough,sub,sup,|,forecolor,backcolor,charmap,formatselect,|,justifyleft,justifycenter,justifyright,justifyfull,blockquote,outdent,indent",
        theme_advanced_buttons3 : "bullist,numlist,|,hr,|,link,unlink,anchor,|,image,|,tablecontrols"
    });

I currently have no removal code, and it works just fine in Safari, still craps out in Firefox.

Last edited by quasipickle (2009-11-11 04:47:01)

13

Re: TinyMCE causing exceptions when initialized second time in one page

here is a better problem description:

1. i load the first time to dialog and close it once again. via jquery.tinymce.js the tinymce gets loaded und after the dialog gets closed the tinymce gets removed. the code comes later...
2. when i load the dialog again and close it once again then (sometimes) the exception occurs. now it's green in the conseole because i have set a try-except-block. This line in the serialize-method n.ownerDocument.implementation brings the problem :-/ when this line is called than the exception occurs.

http://img5.imageshack.us/img5/5115/screenshot2hu.png

here the code which gets executed when the dialog gets closed:

if ((tinyMCE != undefined) && (tinyMCE.activeEditor != undefined))
{
    tinyMCE.activeEditor.onRemove.add(function(ed) {
        console.debug('Editor was removed: ' + ed.id);
    });
    console.debug('start removing: ' + tinyMCE.activeEditor.id);
    try {
        tinyMCE.activeEditor.remove();
    } catch (e) {
        console.debug(e);
    }
}

here the code i've inserted in the tiny_mce_src.js at line 6177 -- in method serialize:

// Nodes needs to be attached to something in WebKit/Opera
// Older builds of Opera crashes if you attach the node to an document created dynamically
// and since we can't feature detect a crash we need to sniff the acutal build number
// This fix will make DOM ranges and make Sizzle happy!
console.debug('serialize: ' + n == undefined);
console.debug('serialize: ' + n.ownerDocument == undefined);
console.debug('serialize: ' + n.ownerDocument.implementation == undefined); // <-- HERE IS NOW THE PROBLEM
impl = n.ownerDocument.implementation;

i hope you can help us with this problem!

14

Re: TinyMCE causing exceptions when initialized second time in one page

Setup a simple test page and I will have a look at it. And by simple I mean the absolute basics for this issue to be reproduced.

Best regards,
Spocke - Main developer of TinyMCE

15

Re: TinyMCE causing exceptions when initialized second time in one page

See answer here :
http://tinymce.moxiecode.com/punbb/view … ?pid=22977

16

Re: TinyMCE causing exceptions when initialized second time in one page

Where exactly do i find a solution? I included the remove-commands but there is still the error. I cannot say when and why it occurs... Even in the current version it's included. I'm going crazy... *grml*

17

Re: TinyMCE causing exceptions when initialized second time in one page

It looks like something is accessing the contents of the editor after you remove it. If it's removed no methods on the editor object will work any more.

Best regards,
Spocke - Main developer of TinyMCE

18

Re: TinyMCE causing exceptions when initialized second time in one page

The problem is like i describes some time ago. i'm loading a textarea (-> becomes tinyMCE) via ajax und show it to the user when the tinyMCE has made the editor out of the textarea. the user can save it or close it. after a reload this error occurs:

uncaught exception: [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIDOMHTMLDocument.implementation]" nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)" location: "JS frame :: http://localhost/gartenplaner/seite/startseite# :: anonymous :: line 0" data: no]

i don't know why and when. sometimes there the error does not occur, sometimes it does.

@spocke: perhaps you have time to chat an icq about this problem? then i can show you a live-demo. Write a PN.

Last edited by g3ck0 (2010-01-28 03:53:29)

19

Re: TinyMCE causing exceptions when initialized second time in one page

I think i have solved my problem: when you apply tinyMCE to a textarea which is on a not visible div then sometimes there occurs this very strange problem. I'm working on a test case for you. Perhaps you can fix it.

It would be nice when this dialog pops up when it's ready and not before...

20

Re: TinyMCE causing exceptions when initialized second time in one page

Yes, please make a small test case for this and we will look into it. File a bug report on SF and attach the HTML file illustrating the problem and we will be very grateful.

Best regards,
Spocke - Main developer of TinyMCE

21

Re: TinyMCE causing exceptions when initialized second time in one page

i have written you a email smile but i can write a bug report too...

edit: here a link to the bug report.

Last edited by g3ck0 (2010-01-28 16:34:52)

22

Re: TinyMCE causing exceptions when initialized second time in one page

I have three TinyMCE editors in my application and I have to switch between state with one editor and state with two editors.

I have fixed the problem this way:

1. when I remove a tinyMCE editor from page I call onDetach() function:
  onDetach(){
      tinyMCE.execCommand('mceRemoveControl', false, id);
      tinyMCEkilled = true;
}

2. a special boolean value (tinyMCEkilled) makes sure that nobody would call
tinyMCE.activeEditor.getContent() or tinyMCE.activeEditor.setContent(content) functions.

Hope this would help somebody.

23

Re: TinyMCE causing exceptions when initialized second time in one page

This problem still seems to exist, and only when the object (or it's parent) is set to display: none. For some reason the object seems to be non-existant (in at least firefox) on line impl = n.ownerDocument.implementation.

Any luck yet? THe bug-report doesn't show any progress yet.

Last edited by DJamin (2010-03-22 14:43:49)

24

Re: TinyMCE causing exceptions when initialized second time in one page

Any chance this issue will get attention soon...!?

Still nothing in the bug report.

25

Re: TinyMCE causing exceptions when initialized second time in one page

The bug report is to complex and doesn't suit the basic needs for a test case. Produce a bare minimum example where this issue occurs and I will have a look at it. Toggling editor instances on/off seems to work just fine on our examples on this site so this might be some implementation issue.

Best regards,
Spocke - Main developer of TinyMCE