'TinyMCE directionality ltr all sub-elements

As far as I know the directionality plugin in TinyMCE, with the button ltr, only apply dir="ltr" to the selected top element. I want to change this behaviour, so it will apply the ltr directionality to all subtree elements of the selected element, in which the direction is not ltr.



Solution 1:[1]

I solved it outside of the directionality plugin using:

editor.on('ExecCommand', function(e) {
                        switch(e.command){
                            case "mceDirectionLTR":
                                var selectedNode = tinymce.activeEditor.selection.getNode();
                                changeDirection(selectedNode,'ltr');
                                break;
                            case "mceDirectionRTL":
                                var selectedNode = tinymce.activeEditor.selection.getNode();
                                changeDirection(selectedNode,'rtl');
                                break;
                        }
                    });

And

function changeDirection(rootNode, desiredDirectionLiteral){
        var node; var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_ELEMENT, null, false);
        while (node = walk.nextNode()) {
            if (node.dir !== null && node.dir !== '' && node.dir !== desiredDirectionLiteral) {
                node.dir = desiredDirectionLiteral;
            }
        } 
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan B