Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tinymce adding &nbsp

I am tryng to override the default tinymce in peranhacms like suggested here Configure / override Piranha CMS html editor so as not to add &nbsp's to html I spend already about one hour trying to fix that problem. There is a lot of resources on that problem but cant get it work.

  • https://wordpress.org/support/topic/correct-way-to-allow-nbsp-entity-in-tinymce
  • tinyMCE adding p tags and nbsp automatically
  • http://blog.room34.com/archives/5075
  • TinyMCE is adding &nbsp instead of the space when using the word paste

Here is how my tinymce.init looks like.

<script type="text/javascript" src="~/res.ashx/areas/manager/content/js/ext/tiny_mce/tinymce.min.js"></script>
<script type="text/javascript">
    tinymce.init({
        mode: 'specific_textareas',
        editor_selector: "editor",
        apply_source_formatting: false,
        cleanup_on_startup: false,
        trim_span_elements: false,
        cleanup: false,
        convert_urls: false,
        force_br_newlines: true,
        force_p_newlines: false,
        remove_linebreaks: false,
        convert_newlines_to_brs: false,
        forced_root_block: '',
        inline_styles : true,
        entity_encoding: 'raw',
        verify_html: false,
        //forced_root_block: false,
        validate_children: false,
        remove_redundant_brs: false,
        fix_table_elements: false,

        entities: '160,nbsp,38,amp,60,lt,62,gt',

        plugins: [
            "autoresize autolink code hr paste piranhaimage link"
        ],
        width: "100%",
        height: "340",
        autoresize_min_height: 340,
        @if (File.Exists(Server.MapPath("~/areas/manager/content/css/editor.css"))) {
        <text>content_css: "@Url.Content("~/areas/manager/content/css/editor.css")",</text>
        }
        toolbar: "bold italic underline | bullist numlist hr | formatselect removeformat | cut copy paste | link piranhaimage | code",
        paste_auto_cleanup_on_paste: false,
        paste_postprocess: function (pl, o) {
            // remove extra line breaks
            o.node.innerHTML = o.node.innerHTML.replace(/&nbsp;/ig, " ");
            alert("a1");
        },
        cleanup_callback: 'my_cleanup_callback',
    });
    function my_cleanup_callback(type, value) {
        alert("a2");
        switch (type) {
            case 'get_from_editor':
                // Remove &nbsp; characters
                value = value.replace(/&nbsp;/ig, ' ');
                alert("a3");
                break;
            case 'insert_to_editor':
            case 'submit_content':
            case 'get_from_editor_dom':
            case 'insert_to_editor_dom':
            case 'setup_content_dom':
            case 'submit_content_dom':
            default:
                break;
        }
        return value;
    }
</script>

here is the example of html I use to paste in to tinyice textarea

<div class="catelog-box">
    <img src="images/dance.png" alt="dine">
    <div class="cat-detail">
    <h2>Dance</h2>
    <p>Dis purus arcu etiam auctor risus aliquam mid turpis eu vel, nunc rhoncus lacus natoque ridiculus...</p>          
  </div>
</div>

And it is how it is looking in browser source: enter image description here

I put alerts to check if paste_postprocess and my_cleanup_callback actually firing, but they are not. And I am still have &nbsp in the html.

I was trying to set cleanup: true and paste_auto_cleanup_on_paste: true but it is didnt help to fire paste_postprocess and my_cleanup_callback

How would you fix the &nbsp problem?

like image 537
sreginogemoh Avatar asked Jan 06 '15 03:01

sreginogemoh


People also ask

How do you add a custom plugin to TinyMCE?

Custom plugins can be added to a TinyMCE instance by either: Using external_plugins : Use the external_plugins option to specify the URL-based location of the entry point file for the plugin.

How do you add HTML code to TinyMCE?

tinymce. activeEditor. setContent(html, {format: 'raw'});


3 Answers

Just adding entity_encoding: 'raw' solved the problem.

like image 172
Dinesh Ramasamy Avatar answered Sep 21 '22 23:09

Dinesh Ramasamy


The following code clean me everything in the tinymce content

tinymce.init({
    selector: "textarea",
    invalid_elements :'strong,bold,b,em,br,span,div,p,img,a,table,td,th,tr,header,font,body,h,h1,h2,h3,h4,h5',
    invalid_styles: 'color font-size text-decoration font-weight',
    menubar: false,
    toolbar:false,
    statusbar:false,
    forced_root_block : "",
    cleanup: true,
    remove_linebreaks: true,
    convert_newlines_to_brs: false,
    inline_styles : false,
    entity_encoding: 'raw',
    entities: '160,nbsp,38,amp,60,lt,62,gt',
 });}
like image 30
dev111 Avatar answered Sep 23 '22 23:09

dev111


TinyMCE is a monster of options and settings, but given the links you have provided and that you've used the clean-up method from TinyMCE is adding &nbsp instead of the space when using the word paste, have you tried setting:

paste_auto_cleanup_on_paste: true

Since this is set in the example you're referencing. Apart from that guess, I have no idea why the event isn't firing.

Best regards

Håkan

like image 23
Håkan Edling Avatar answered Sep 25 '22 23:09

Håkan Edling