Question

I'm writing an extension to nicEdit which will cause all pasted rich text to be pasted as plain text. My code works perfectly in Chrome and IE7, but fails in Firefox 3 (and, presumably, Firefox 2, but for different reasons - notably, the lack of a paste event).

The odd part is that my code works perfectly when the text being pasted is plain text. It seems to choke on any formatting. Here are the steps involved in the Firefox version, which are fired on paste (the event is firing, but the paste fails):

  • Get the current selection and range
  • Create a new div with contentEditable = true, and add it to the DOM
  • Move the caret to the new div
  • set a timeout to get the HTML from the new div, clean it, and put it into the old selection (which I grabbed in step 1)
  • return true (allowing the original paste to complete, pasting content into the new div)

It seems that all these steps are executed, but the paste never happens. I get no errors in the console (just a warning relating to a parse error on text-align - the declaration is dropped), and nothing suspicious happens when I debug with Firebug (except that my paste seems empty, which is the problem).

The warning makes me think that Firefox may just be unable to parse the content I've been pasting, but without my intercept code, I am able to paste into the nicEditor just fine.

I've uploaded my Firefox-only test script at http://www.ryankinal.com/paste/firefox.html - this is the code that Firefox executes from my full extension.

A couple things to note:

  • I've taken a fair amount of inspiration from the TinyMCE paste plugin
  • I'm using some jQuery for element selection
  • There also seems to be some modification to Function.prototype in nicEdit, which I find odd
  • I could use another Rich Text Editor, but consider this an exercise in debugging
  • The code linked above is intended ONLY FOR FIREFOX
Was it helpful?

Solution

The problem is that the paste event is simply too late to redirect the caret in some browsers, including Firefox. You'll be able to do the caret-redirect trick if you intercept keyboard shortcuts for pasting (Ctrl+V, Cmd+V and Shift+Insert) and ignore the paste event, but this means pastes triggered by the context and edit menus are not covered. I think this is the best you can do, however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top