Josh Smith
Josh Smith

Reputation: 15028

Removing external styles from jQuery WYSIWYG editor in PHP

I have an issue with my WYSIWYG editor. If users copy in outside text, this is seen as something like the following:

" p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Lucida Grande'; min-height: 13.0px} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Lucida Grande'} Only the variables assigned in the last loop are accessible from outside the scope of a foreach loop."

This is obviously problematic.

On top of that, there also seems to be an issue with line breaks, i.e. <br /> tags. Sometimes they're picked up, sometimes not.

I've been running the content through strip_tags() like so:

<?php
$body = strip_tags($body, '<a><br><b><i><img><ul><ol><li>');

Any thoughts on what's going on here?

If it helps, I'm using jWYSIWYG for the editor.

Upvotes: 2

Views: 1126

Answers (4)

Josh Smith
Josh Smith

Reputation: 15028

The following code finally got it working for me:

$('iframe').ready(function() {
    $(this).contents().find('.wysiwyg').find('iframe').contents()
    .find('.wysiwyg').bind('paste', function() {
        // Completely strips tags.  Taken from Prototype library.
        var el = $(this);
            var strClean = el.text().replace(/<\/?[^>]+>/gi, '');
            el.text(strClean);
        }, 0);
    });
});

You can see this in action at http://jsfiddle.net/v4LhV/3/

Upvotes: 0

Bradley Kreider
Bradley Kreider

Reputation: 1155

I think the issue tracker for jwysiwg has comments related to this for browser side cleanup.

Upvotes: 0

Scott
Scott

Reputation: 1477

Maybe use preg_replace like http://ideone.com/VjMZY?

$str = preg_replace('/<br[^\>]*?>/', '', $str);

Upvotes: 0

cEz
cEz

Reputation: 5062

Regarding your query about strip_tags():

php > $str="<br><br/><br />";
php > echo strip_tags($str, "<br>");
<br><br />

Is there a chance that <br/> is used and being omitted? If so, add <br/> to strip_tags(), e.g.

php > $str="<br><br/><br />";
php > echo strip_tags($str,'<br><br/>');
<br><br/><br />

Upvotes: 2

Related Questions