Skip to content

Document and fix cleanHtml #146

@yonjah

Description

@yonjah

cleanHtml logic is not clear and it seem like it has a lot of unrelated logic.
I couldn't find any documentation for it (probably because the original function was very simple)

Original method just returned a string removing some redundant html tags and white spaces -

$.fn.cleanHtml = function () {
    var html = $(this).html();
    return html && html.replace(/(<br>|\s|<div><br><\/div>|&nbsp;)*$/, '');
};

Now it seem like it has a lot of extra logic was added in

Wysiwyg.prototype.cleanHtml = function( o ) {
        var self = this;
        if ( $( self ).data( "wysiwyg-html-mode" ) === true ) {
            $( self ).html( $( self ).text() );
            $( self ).attr( "contenteditable", true );
            $( self ).data( "wysiwyg-html-mode", false );
        }
        ...

If I understand correctly #4 the code should remove the pre element from the content (not sure why it's needed but it does not seem to revert the instance back once it's done)

And after it we have this code which is used to inject image data in as new form inputs

        // Strip the images with src="data:image/.." out;
        if ( o === true && $( self ).parent().is( "form" ) ) {
            var gGal = $( self ).html;
            if ( $( gGal ).has( "img" ).length ) {
                var gImages = $( "img", $( gGal ) );
                var gResults = [];
                var gEditor = $( self ).parent();
                $.each( gImages, function( i, v ) {
                    if ( $( v ).attr( "src" ).match( /^data:image\/.*$/ ) ) {
                        gResults.push( gImages[ i ] );
                        $( gEditor ).prepend( "<input value='" + $( v ).attr( "src" ) + "' type='hidden' name='postedimage/" + i + "' />" );
                        $( v ).attr( "src", "postedimage/" + i );
                    }
                } );
            }
        }

(I think implementation is a bit buggy since it never actually call the $( self ).html method but I haven't really ran the code to check).
But even if it works correctly it doesn't seem like it has anything to do with the actual clean functionality and it's probably should be included in a unique method (maybe even with a complementary logic which will allow to also remove the inputs that were created).

currently on v2 it also not being exposed at all and it seem to still assume this refers to the wysiwyg instance ( there is a pull request which exposes it but doesn't address any of the other issues)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions