Feature suggestion: RRTE — paste from Word button (remove unnecessary formatting)

7 views
Skip to first unread message

ad

unread,
May 21, 2011, 7:37:11 PM5/21/11
to RightJS
Hi, Nik!

I'd like to propose a little improvement to RRTE: can You add a button
with «Clear formatting» functionality?
I mean — not just make a plain text from pasted, but clean that text
from MS Word formatting, inline styles and font definitions etc.

I think it would be useful...

Tried to bind a function that cleans pasted HTML to Ctrl+V key
combination, but without success.
Console gives me an error: «TypeError: Result of expression
'b.find' [undefined] is not a function.» in right.js at line 7.

This error shows up when i press assigned key-combo.

I can clean pasted text and replace content of Rte textarea manually
in console though, without attaching a function to key kombination.

If this helps — this is my function, that i use to strip unnecessary
formatting:

function cleanHTML(str) {
str = str.replace(/<o:p>\s*<\/o:p>/g, "") ;
str = str.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;") ;
str = str.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" ) ;
str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" ) ;
str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
str = str.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" ) ;
str = str.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
str = str.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
str = str.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
str = str.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
str = str.replace( /\s*tab-stops:[^;"]*;?/gi, "" ) ;
str = str.replace( /\s*tab-stops:[^"]*/gi, "" ) ;
str = str.replace( /\s*face="[^"]*"/gi, "" ) ;
str = str.replace( /\s*face=[^ >]*/gi, "" ) ;
str = str.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" ) ;
str = str.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
str = str.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<
$1$3" ) ;
str = str.replace( /\s*style="\s*"/gi, '' ) ;
str = str.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi,
'&nbsp;' ) ;
str = str.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
str = str.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
str = str.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ;
str = str.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;
str = str.replace(/<\\?\?xml[^>]*>/gi, "") ;
str = str.replace(/<\/?\w+:[^>]*>/gi, "") ;
//str = str.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
//str = str.replace( /<H1([^>]*)>/gi, '' ) ;
//str = str.replace( /<H2([^>]*)>/gi, '' ) ;
//str = str.replace( /<H3([^>]*)>/gi, '' ) ;
//str = str.replace( /<H4([^>]*)>/gi, '' ) ;
//str = str.replace( /<H5([^>]*)>/gi, '' ) ;
//str = str.replace( /<H6([^>]*)>/gi, '' ) ;
//str = str.replace( /<\/H\d>/gi, '<br>' ) ; //remove this to take
out breaks where Heading tags were
//str = str.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;
//str = str.replace( /<(B|b)>&nbsp;<\/\b|B>/g, '' ) ;
str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
//some RegEx code for the picky browsers
var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;
str = str.replace( re, "<div$2</div>" ) ;
var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/
font>)","gi") ;
str = str.replace( re2, "<div$2</div>") ;
str = str.replace( /size|SIZE = ([\d]{1})/g, '' ) ;

return str ;
}

I know that clipboard access is limited in browsers, but it can be
done via dialog I think (as it's done with the image insert button)...

And one more thing: in Safari 5.0.5 (OS X 10.5.8) Copy, Cut and Paste
buttons are inactive all the time.
In FF and Opera those buttons are active, as supposed to, but they
doesn't work. Seems like permissions issue.


Thanks,
Alex

Nikolay Nemshilov

unread,
May 24, 2011, 2:33:42 AM5/24/11
to rig...@googlegroups.com
Hi Alex,

I'm not sure it will be possible to hijack Ctrl+V and read stuff directly from the clipboard, but it could be probably done with a separated button. I'll check it out a bit later.

btw, I don't have MSW, could you collect me a bunch of typical data? just some strings, maybe in a zip or something

and one more. Why do you use this crazy list of regular expressions? won't a simple /<[^>]+>/, '' do the trick?

> --
> You received this message because you are subscribed to the Google Groups "RightJS" group.
> To post to this group, send email to rig...@googlegroups.com.
> To unsubscribe from this group, send email to rightjs+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rightjs?hl=en.
>

--
Nikolay V. Nemshilov
The Web-Developer

ad

unread,
May 24, 2011, 5:14:05 PM5/24/11
to RightJS
Hi Nik,

Thanks for your reply.

> I'm not sure it will be possible to hijack Ctrl+V and read stuff directly from the clipboard, but it could be probably done with a separated button. I'll check it out a bit later.

I thought so. And i think that a separate button will be the best
solution possible.

> btw, I don't have MSW, could you collect me a bunch of typical data? just some strings, maybe in a zip or something

My primary OS is Mac OS X Leo. ;)
But I'll try it at work.

> and one more. Why do you use this crazy list of regular expressions? won't a simple /<[^>]+>/, '' do the trick?
I just found these regexps a while ago, and as it not taking a lot of
space their amount is irrelevant for me.
As I said before — I'll do try to collect some common MS-Word internal
data, that it inserts to clipboard upon copying.
Reply all
Reply to author
Forward
0 new messages