1
votes

In Odoo 9 there is a WYSIWYG editor which adds <p> tags on any empty line and encloses nearly every filled line with them. This totally wrecks templates. So if you have a nice email template for example for your invoice emails they will get sanitized by this editor even if you don't edit anything manually before sending.

How can we disable this behavior of the WYSIWYG editor?

Edit
The problematic function is text_to_html and can be found in addons/web_editor/static/src/js/backend.js. You can disable its very overeager behavior by simply stripping it down to something like this:

text_to_html: function (text) {
        var value = text || "";
        return value;
    }

However, doing so in the core file will get the changes overwritten on the next update. So my question now is, what would be the correct way to override this function from a custom module. I'm familiar with basic Odoo modules to add for example new models but I'm not sure how I could override Odoo JavaScript which was defined by odoo.define and haven't found a documentation on this so far.

1
Do you know where exactly in backend.js you find that piece of code, which is disturbing you. - Hardik Patadia
Of course. As a workaround I also already hacked this core file and don't have the problem at the moment but I really would like to avoid this as permanent solution. It would be great to have a clean way with a custom addon. - Jey DWork
Can you specify that location of that code? - Hardik Patadia
I'm not sure where you are aiming at but here you go (: github.com/odoo/odoo/blob/… (Line 95 to 104) - Jey DWork

1 Answers

0
votes

A little bit late to the party but if it would be helpful for someone...

First, it is already fixed in ODOO GitHub repository and the code used below is from there. If you can update ODOO. If you can't you should create new module and install it in ODOO. If you don't know how refer to the docs.

Put JavaScript file with this content in the module:

odoo.define('web_editor_sanitize_fix.backend', function (require) {
    'use strict';

    var core = require('web.core');
    var backend = require('web_editor.backend');

    backend.FieldTextHtmlSimple.include({
        text_to_html: function (text) {
            var value = text || "";
            try {
                $(text)[0].innerHTML;
                return text;
            } catch (e) {
                if (value.match(/^\s*$/)) {
                    value = '<p><br/></p>';
                } else {
                    value = "<p>"+value.split(/<br\/?>/).join("<br/></p><p>")+"</p>";
                    value = value.replace(/<p><\/p>/g, '').replace('<p><p>', '<p>').replace('<p><p ', '<p ').replace('</p></p>', '</p>');
                }
            }
            return value;
        }
        });

});

I haven't tested the code but it should work.