Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
As requested in #1084248: Switching input format to TinyMCE causes content not to be saved, to reproduce:
1. select nicEdit for default input format
2. on node edit form enter some text into nicEdit's window
3. disable/switch to another input format/editor
4. change the text and hit preview
result: text from step 2 is restored into text area
the problem with removal is with the way it attaches itself:
Function.prototype.closure = function() {
var __method = this, args = bkLib.toArray(arguments), obj = args.shift();
return function() { if(typeof(bkLib) != 'undefined') { return __method.apply(obj,args.concat(bkLib.toArray(arguments))); } };
}
...
var f = e.parentTag('FORM');
if(f) { bkLib.addEvent( f, 'submit', this.saveContent.closure(this)); }
Attached patch just makes the handler logic fail, far from perfect but I don't see anything in nicEdit code that could help get rid of this handler. Setting to needs work for that reason.
Comment | File | Size | Author |
---|---|---|---|
#9 | wysiwyg-master-nicedit-submit.1132142.9.patch | 1.08 KB | TwoD |
#6 | nicedit.js-1132142.6.patch | 855 bytes | tacituseu |
#5 | wysiwyg-master-nicedit-submit.1132142.5.patch | 771 bytes | TwoD |
#3 | wysiwyg-master-nicedit-submit.1132142.3.patch | 1.23 KB | TwoD |
#2 | nicedit.js-1132142.2.patch | 1.11 KB | tacituseu |
Comments
Comment #1
TwoDBumping this to 7.x-2.x as that's where we apply patches first and backport later. (This patch should apply to both 7.x and 6.x but I've only tested with 7.x yet.)
With this patch I try to clean up after the editor so submit handlers for each editor instance aren't fired at all once they've been removed. This could potentially remove other leftover event handlers too.
The reason I do this temporary call interception the attach() method rather than once in an init() method, and leaving it there, is that this way we should be able to detach just the handlers related to a certain instance. I think that could make things easier if there are multiple forms on a page and some are submitted via AJAX (which can also trigger submit handlers).
Like I said above, I've only had time to test this in IE (8) yet.
Comment #2
tacituseu CreditAttribution: tacituseu commentedThank you, only problem I can see is missing argument in removeEventListener(), tested with Chrome 10, FF 4.0, IE 7 both D6 & D7. Leaving at needs review as I don't feel JS-savvy enough to make any compatibility claims.
Comment #3
TwoDNice catch, without the
false
FF throws an "not enough arguments"-exception.FF 3, Opera 11, Safari 5 all seem fine with this change too so I'd say it's ready to go.
I did make one syntax change tho, since it made things a bit more readable I split the inlined condition to an if-else.
Comment #4
sunIn some weeks/months from now, everyone will have to read study this code and the library's code to figure out _why_ this was done, why it was done as a hack (no pun intended, seems to be the only way right now).
Code comments should always state the non-obvious, everything that cannot be read out of the code, which we do know now, but tend to forget later. Comments should primarily be about *why* something is done, and should also answer the immediate question of "WTF why on earth this way?" :)
Wrong indentation here.
There shouldn't be a space after the property names.
mmm... is there any reason why we're not using our addEvent override to simply skip the submit handler events in question?
In other words: why do we add the handlers to a stack if we're going to remove them anyway? On form submit, Wysiwyg should be triggering the detach methods anyway, no?
Powered by Dreditor.
Comment #5
TwoDOk, I'll try to expand the comment, but I'm having a hard time trying to describe things with words that would make sense unless you do already know the editor pretty well...
The reason I kept the submit handlers in there until after the editor has been removed was that they seemed to be executed before Wysiwyg's handler, and I'm a bit paranoid when it comes to ripping out a function call the editor expects to be made just before the page gets unloaded. For example; TinyMCE and others are very careful to remove certain objects and references to not cause memory leaks that would persist across page loads. Removing those handlers before they've run will lead to problems unless we do all of the internal cleanup calls manually, something that could change between minor versions as browser specific bugs are fixed.
Hence, if this editor decides to do something similar, we'd have a "future proof" (as much as it can be at this point) solution where the editor cleans up most of its internal stuff on its own before we go in and remove the handler.
But, I just noticed the editor's handler actually got invoked after Wysiwyg's detach handler - and its only purpose is to save the content to the textarea, something we already do indirectly via the .remove() call - which does indeed make it pointless to keep that stack of handlers around... Considering its last official release was over two years ago, I'm not expecting it to change much anway (if it does change, it'll probably be a huge rewrite - which would require an implementation rewrite as well).
Enough with my ramblings now, I'm too tired to type more so I'll just attach the revised patch and await your response.
Comment #6
tacituseu CreditAttribution: tacituseu commentedThe handler needs to be restored after creating new instance, as 'submit' is used later in
addForm
by plugins that use dialogs (e.g. Image & Link buttons).Comment #7
sunMuch better :)
I definitely see your perspective, @TwoD :) If I would have spent only half the time that you've worked within these libraries, then I guess I could see the underlying reasonings a bit more clear.
However, we should try hard to make sure that all of our code is easy to understand and follow for newcomers and other developers, who might not have looked into any of these libraries at all yet. They should be able to see and grok what Wysiwyg is doing under the hood, and in edge-cases like this, get to know the reasons for why Wysiwyg has to override and effectively hack an editor's regular behavior to make it work for Drupal. Clean and solid code comments are especially important when it comes to larger, cross-editor API changes; i.e., when (hopefully more) other people need to touch and change existing code to adapt it for something new. That's essentially the only way to ensure that others are able to contribute in the first place and to prevent having to (personally) explain certain code parts, which might be potentially irrelevant for a different, actual task at hand.
Therefore, writing clean code with solid code comments is the only way to solve our topmost problem and reach our ultimate goal: Getting more contributors involved in Wysiwyg. :)
The last minor tweak I'd recommend prior to commit is to replace "removed" with Wysiwyg lingo; i.e., "detached" where appropriate.
The "and they can't be removed once set" part reads a bit confusing/out of context, although I think I understand what is meant. Turning this part into an own sentence might already help; e.g., "NicEdit's submit handlers are anonymous and cannot be unbound."
Lastly, it wouldn't hurt to additionally state the most recent finding for future reference; i.e.: Testing showed that NicEdit's submit handlers are invoked after Wysiwyg's.
Powered by Dreditor.
Comment #8
sunoy, cross-post with #6. Apparently, the issue comment in #6 can almost be used 1:1 as inline code comment right above the line that restores the handler. It's actually a perfect example. ;) If we'd commit the patch in #6, people would have to look up this drupal.org issue to figure out why NicEdit's handler needs to be restored after attaching the editor instance.
Comment #9
TwoDThanks tacituseu! That explains why I saw the handlers getting fired in my earlier tests.
Hope you don't mind me changing the wording a bit.
Comment #10
sunThanks! :)
Comment #11
tacituseu CreditAttribution: tacituseu commentedThanks for a proper solution TwoD!
Comment #12
TwoDCommitted to all relevant branches, -dev snapshots will be updated within 12 hours and this will be in the next official release.
Thank you for patching, testing and reviewing!