Link to home
Start Free TrialLog in
Avatar of CPL593H
CPL593HFlag for Belgium

asked on

Restrictive (clean) paste in FCKEditor?

Hello,

I'm using FCKEditor for one of my projects, but it allows too much when pasting. I'd like it only to paste bold, italics, underlined and maybe title levels and that's all.

The closest solution I've found is to use the ForcePasteAsPlainText setting, but it works in an ugly way, by opening a new window (thus almost negating the need for the editor). Moreover, bold, italics, underlined and title levels can be useful. In my case, hyperlinks, font types and sizes, images, etc. are a major nuisance.

I'll take any suggestion. My idea right now is to clean the output in PHP after the text is saved, but it is not WYSIWYG anymore.

Cheers,
Stéphane
Avatar of longbloke69
longbloke69

Why not paste into a hidden div, do the cleanup using JS and regex and then output into the wysiwyg editor?
Avatar of CPL593H

ASKER

I do not think such a solution would be transparent for the user, but if you have an idea on how to do so, I'll take it.

Cheers,
Stéphane
I haven't built such a solution, but it would be possible to do a manual clean, you would just need to find a way of getting the data into the external div. The user would not need to be included in the process, other than hitting the button to paste. I'm thinking a basic "div.innerHTML = PastedData", then clean up pasted data and copy and paste (using the fck's in-built functions, i am sadly not versed with fck's features/code) back formatted html "paste(div.innerHTML)".

That's the concept anyway. All this would obviously happen in the blink of an eye, and the user would not even need to know it had left the wysiwyg editor.
Avatar of CPL593H

ASKER

It looks like  EE's text input box does exactly what you describe. However, I'm not competent enough in Javascript to write it. I'd take a link to a site explaining how to clean the input using JS regexes.

Cheers,
Stéphane
Bind onPaste and modify the HTML before paste with some RegEx.
Does this need to work in FireFox?
Avatar of CPL593H

ASKER

Yes, all my projects must be IE6+ and all other Mac and PC browser (I test on IE's, FF, Safari and Opera) and can not rely on client-side programming, except for JavaScript of course (no Flash, ActiveX, etc.)

function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnPaste', cleanit ) ;
}
 
var counter = 0 ;
var ei;
var si;
function cleanit( edI )
{
    edI.ResetIsDirty();
	ei = edI;
	si = setInterval('cleanit1()', 500);
	return true;
}
function cleanit1(){
	if(ei.IsDirty()){
	    var str = ei.GetHTML().replace(/(<(strong|[pP]|[bB]|[iI]|[hH][0-9])\b[^>]*>(.*?)<\/\2>)|<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\4>/g, '$1$5' );
		ei.SetHTML(str);
		clearInterval(si);
	}
}

Open in new window

Avatar of CPL593H

ASKER

Wow sh0e, almost perfect. It still has two problems that are assume are minor: it allows images to be pasted, divs and HTML links too, but in a strange fashion (one line break, the link, another line break).

Now I'm sure that it's just a matter of tweaking the regexp, but JS regexpes are Chinese to me.

Cheers,
Stéphane
The following link describes a way in which you can actually emulate the onpaste event in FireFox.

http://www.actsascommunity.com/snippets/6

I implemented this functionality last week and it works really well.

function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnPaste', cleanit ) ;
}
 
var counter = 0 ;
var ei;
var si;
function cleanit( edI )
{
    edI.ResetIsDirty();
        ei = edI;
        si = setInterval('cleanit1()', 1000);
        return true;
}
function cleanit1(){
        if(ei.IsDirty()){
            var str = ei.GetHTML().replace(/<(?!div|p|strong|b|u|i|h[0-9])([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/ig, '$2' );
                ei.SetHTML(str);
                clearInterval(si);
        }
}

Open in new window

Avatar of CPL593H

ASKER

sh0e: even better, but it still allows images and hyperlinks to be pasted (information that I do not want in my software, which is used for strict text analysis - only exceptions being the usual suspects: bold, italics, underlined, title levels and maybe ordered/unordered lists - tbd later).

From my poor understanding of JS's regexpes, i thought that with your script, only div|p|strong|b|u|i|h[0-9] would be allowed, which looks nice. How do img's and a's (and maybe others that I have not checked) pass through the filter?

Stéphane
ASKER CERTIFIED SOLUTION
Avatar of sh0e
sh0e

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CPL593H

ASKER

Gives me an error in Firefox:

Error: syntax error
Source File: http://192.168.168.128/test.php
Line: 41, Column: 44
Source Code:
            var str = ei.GetHTML().replace((</(?!\/?(strong|b|u|i|h[0-9])\b)[^>]*>)/ig,'');

The error is located at the ((.

As I've said, I don't understand JS's regexpes enough to fix the error. Nevertheless, I'd like to have comment not on the code itself  it's pretty clean  but on the regexp so I understand what it does and how it does it.

Stéphane



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
?! tells it to fail match if it finds it
Avatar of CPL593H

ASKER

Many thanks! You really should publish this hint on FCK's forum, I've read so many times that what it does is impossible!