Make WordPress Core

Ticket #31441: 31441.2.patch

File 31441.2.patch, 6.2 KB (added by iseulde, 10 years ago)
  • src/wp-includes/class-wp-editor.php

     
    368368                                                'wplink',
    369369                                                'wpdialogs',
    370370                                                'wpview',
     371                                                'wptextpattern'
    371372                                        );
    372373
    373374                                        if ( ! self::$has_medialib ) {
  • src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js

     
     1( function( tinymce ) {
     2        tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
     3                var $$ = editor.$;
     4                var patterns = [];
     5
     6                function add( regExp, callback ) {
     7                        patterns.push( {
     8                                regExp: regExp,
     9                                callback: callback
     10                        } );
     11                }
     12
     13                add( /^[*-]\s/, function() {
     14                        this.execCommand( 'InsertUnorderedList' );
     15                } );
     16
     17                add( /^1[.)]\s/, function() {
     18                        this.execCommand( 'InsertOrderedList' );
     19                } );
     20
     21                add( /^>\s/, function() {
     22                        this.execCommand( 'mceBlockQuote' );
     23                } );
     24
     25                editor.on( 'keyup', function( event ) {
     26                        var node;
     27
     28                        if ( event.keyCode !== tinymce.util.VK.SPACEBAR ) {
     29                                return;
     30                        }
     31
     32                        node = editor.selection.getRng().startContainer;
     33
     34                        if ( node.nodeType !== 3 ) {
     35                                return;
     36                        }
     37
     38                        tinymce.each( patterns, function( pattern ) {
     39                                var text = node.nodeValue,
     40                                        replace;
     41
     42                                if ( node.parentNode.firstChild !== node ) {
     43                                        return;
     44                                }
     45
     46                                replace = text.replace( pattern.regExp, '' );
     47
     48                                if ( text === replace ) {
     49                                        return;
     50                                }
     51
     52                                editor.undoManager.transact( function() {
     53                                        $$( node.parentNode ).replaceWith( replace ? document.createTextNode( replace ) : document.createElement( 'BR' ) );
     54
     55                                        pattern.callback.apply( editor );
     56                                } );
     57
     58                                return false;
     59                        } );
     60                } );
     61        } );
     62} )( window.tinymce );
  • tests/qunit/editor/js/utils.js

     
    131131
    132132        // TODO: Replace this with the new event logic in 3.5
    133133        function type(chr) {
    134                 var editor = tinymce.activeEditor, keyCode, charCode, evt, startElm, rng;
     134                var editor = tinymce.activeEditor, keyCode, charCode, evt, startElm, rng, textNode;
     135
     136                function charCodeToKeyCode(charCode) {
     137                        var lookup = {
     138                                '0': 48, '1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 57,'a': 65, 'b': 66, 'c': 67,
     139                                'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81,
     140                                'r': 82, 's': 83, 't': 84, 'u': 85,     'v': 86, 'w': 87, 'x': 88, 'y': 89, ' ': 32, ',': 188, '-': 189, '.': 190, '/': 191, '\\': 220,
     141                                '[': 219, ']': 221, '\'': 222, ';': 186, '=': 187, ')': 41
     142                        };
     143
     144                        return lookup[String.fromCharCode(charCode)];
     145                }
    135146
    136147                function fakeEvent(target, type, evt) {
    137148                        editor.dom.fire(target, type, evt);
     
    139150
    140151                // Numeric keyCode
    141152                if (typeof(chr) == "number") {
    142                         charCode = keyCode = chr;
     153                        charCode = chr;
     154                        keyCode = charCodeToKeyCode(charCode);
    143155                } else if (typeof(chr) == "string") {
    144156                        // String value
    145157                        if (chr == '\b') {
     
    150162                                charCode = chr.charCodeAt(0);
    151163                        } else {
    152164                                charCode = chr.charCodeAt(0);
    153                                 keyCode = charCode;
     165                                keyCode = charCodeToKeyCode(charCode);
    154166                        }
    155167                } else {
    156168                        evt = chr;
     169
     170                        if (evt.charCode) {
     171                                chr = String.fromCharCode(evt.charCode);
     172                        }
     173
     174                        if (evt.keyCode) {
     175                                keyCode = evt.keyCode;
     176                        }
    157177                }
    158178
    159179                evt = evt || {keyCode: keyCode, charCode: charCode};
     
    196216                                if (rng.startContainer.nodeType == 3 && rng.collapsed) {
    197217                                        rng.startContainer.insertData(rng.startOffset, chr);
    198218                                        rng.setStart(rng.startContainer, rng.startOffset + 1);
    199                                         rng.collapse(true);
    200                                         editor.selection.setRng(rng);
    201219                                } else {
    202                                         rng.insertNode(editor.getDoc().createTextNode(chr));
     220                                        textNode = editor.getDoc().createTextNode(chr);
     221                                        rng.insertNode(textNode);
     222                                        rng.setStart(textNode, 1);
    203223                                }
     224
     225                                rng.collapse(true);
     226                                editor.selection.setRng(rng);
    204227                        }
    205228                }
    206229
  • tests/qunit/index.html

     
    3939                <script src="wp-includes/js/shortcode.js"></script>
    4040                <script src="wp-admin/js/customize-controls.js"></script>
    4141                <script src="wp-admin/js/customize-controls-utils.js"></script>
     42
     43                <!-- TinyMCE -->
     44
     45                <script src="../../src/wp-includes/js/tinymce/tinymce.js"></script>
     46
     47                <script src="editor/js/utils.js"></script>
     48
     49                <script src="wp-includes/js/tinymce/plugins/wptextpattern/plugin.js"></script>
     50
    4251        </body>
    4352</html>
  • tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js

     
     1( function( $, _type ) {
     2
     3        var editor;
     4
     5        function type( string ) {
     6                tinymce.each( string.split( '' ), _type );
     7        }
     8
     9        module( 'tinymce.plugins.wptextpattern', {
     10                beforeEach: function() {
     11                        stop();
     12
     13                        $( '#qunit-fixture' ).append( '<textarea id="editor">' );
     14
     15                        tinymce.init( {
     16                                selector: '#editor',
     17                                plugins: 'wptextpattern',
     18                                init_instance_callback: function() {
     19                                        editor = arguments[0];
     20                                        editor.focus();
     21                                        start();
     22                                }
     23                        } );
     24                },
     25                afterEach: function( assert ) {
     26                        editor.remove();
     27                }
     28        } );
     29
     30        test( 'unordered list', function() {
     31                type( '* ' );
     32                equal( editor.getContent(), '<ul>\n<li></li>\n</ul>' );
     33        } );
     34
     35        test( 'ordered list', function() {
     36                type( '1. ' );
     37                equal( editor.getContent(), '<ol>\n<li></li>\n</ol>' );
     38        } );
     39
     40        test( 'ordered list with text', function() {
     41                type( 'test' );
     42                editor.selection.setCursorLocation();
     43                type( '1. ' );
     44                equal( editor.getContent(), '<ol>\n<li>test</li>\n</ol>' );
     45        } );
     46
     47} )( window.jQuery, window.Utils.type );