What HTML attributes are allowed on a Tumblr post? (tinymce)

前端 未结 3 530
温柔的废话
温柔的废话 2021-01-06 10:15

When creating a Tumblr post, and using the HTML editor option, when I add specific HTML attributes to elements, the TinyMCE rich text editor strips nearly everything off.

3条回答
  •  逝去的感伤
    2021-01-06 10:58

    Using Markdown sounds like a good alternative, but I’ve read somewhere that it has problems with embedded iframes. If it works for you, then that’s fluffy. I haven’t tested it yet.

    However, what you can do if you don’t want to forgo TinyMCE is the following.

    tinymce.activeEditor.schema.setValidElements('*[*]')
    

    This tells the active editor that now all elements are valid.

    Unfortunately, as soon as you close and reopen the edit window, the freshly started editor will strip them again, so I wrote a little Greasemonkey script that sets the valid_elements automatically at the initialization of the editor.

    // ==UserScript==
    // @name        Lift TinyMCE tag restrictions
    // @namespace   http://claviger.net
    // @include     *.tumblr.com/*
    // @version     1
    // @grant       none
    // ==/UserScript==
    
    if (typeof tinyMCE !== 'undefined') {
        tinyMCE.onAddEditor.add(function(mgr, ed) {
            ed.settings.valid_elements = '*[*]';
        });
    }
    

    It works pretty well for me, but I think in rare cases there’s a race condition going on between the Greasemonkey script and the actual initialization of TinyMCE. I assume the latter must sometimes run before the first and thwart my little hack. It hasn’t happened the last twenty or so times I reloaded the page, though, and if it happens, just reload without saving. (Maybe someone has an idea how to avoid this?)

    By the way, the class attribute doesn’t seem to get stripped, so as another alternative, you could define something like .alignleft in your blog’s theme and use it for the images. Then it wouldn’t look like much in TinyMCE, but the visitor or the blog would see the pretty version.

提交回复
热议问题