Trouble with pasted content

Trouble with pasted content

by Diane Soini -
Number of replies: 4

We are having trouble with users pasting bad content into their course summaries. As a result we get courses with strange layouts and strange behavior and a lot of confused instructors.

I would like to modify the javascript that atto uses to clean the text on a paste event, which I believe is located in /lib/editor/atto/ui/src/editor/js/clean.js or /lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js. However, it appear that neither script is ever actually used anywhere in moodle. Any modifications I make are never applied, including causing deliberate alerts just to see if the file is ever loaded. A search of all the files seems to indicate these files are not ever used.

Can a developer-minded person shed some light on this?

Average of ratings: -
In reply to Diane Soini

Re: Trouble with pasted content

by lior gil -
Picture of Core developers

You didn't specify which Moodle version you're using so I'll assume it's a 3.X version.

clean.js is the correct file and the functions are _cleanHTML() and _cleanPasteHTML()

In order for any changes to take hold, you'll need to compile the file using shifter and then purge cache.

However, this isn't the recommended choice, because this is a core change, meaning that in the next version update your changes will be overridden.

I had the same issue and found the solution in overriding the original code through an added script.

Here's an example of what I did for one of the functions.


function EditorAddon() {}
EditorAddon.prototype = {
    _cleanHTML: function(content) {
        var rules = [
            // Put here all the rules from the original function.
            // Add here all your new rules.
        ];
        return this._filterContentWithRules(content, rules);
    }
};
// This line will replace the original function with the new one.
Y.Base.mix(Y.M.editor_atto.Editor, [EditorAddon], true, 1);

I don't know if this is the best solution but it worked well for me so far.

In reply to lior gil

Re: Trouble with pasted content

by Diane Soini -

Thank you. That is helpful. Where did you insert this script? I have attempted to just stick it in the footer.php of my theme.


I get a js error telling me that Y.M is undefined. This happens on both 3.1 and 2.8 versions even though they appear to have slightly different js YUI code setting everything up.

In reply to Diane Soini

Re: Trouble with pasted content

by lior gil -
Picture of Core developers

My way was to duplicate one of the editor's plugins, change its name a bit and insert this code into it.

Then, I configured the editor to load this plugin instead of the origin.

In reply to lior gil

Re: Trouble with pasted content

by Diane Soini -

I figured out that I needed to wrap it in a domready thingie otherwise Y.M may or may not be available. I also tested that my _cleanPasteHTML function replaces the original, so you do have to include all the same functionality otherwise you will lose functionality.