Ticket #7131: 7131_4.patch

File 7131_4.patch, 16.6 KB (added by Garry Yao, 13 years ago)
  • _source/plugins/pastefromword/filter/default.js

     
    118118                return result;
    119119        };
    120120
     121        // Move list item styles up to list root if they're consistent.
     122        function mergeListStyleType( list )
     123        {
     124                var children = list.children,
     125                        attrs,
     126                        count = list.children.length,
     127                        match,
     128                        mergeStyle,
     129                        styleTypeRegexp = /list-style-type:(.*?)(?:;|$)/,
     130                        stylesFilter = CKEDITOR.plugins.pastefromword.filters.stylesFilter;
     131
     132                attrs = list.attributes;
     133                if ( styleTypeRegexp.exec( attrs.style ) )
     134                        return;
     135
     136                for ( var i = 0; i < count; i++ )
     137                {
     138                        match = styleTypeRegexp.exec( children[ i ].attributes.style );
     139
     140                        if ( match )
     141                        {
     142                                if ( match[ 1 ] == mergeStyle || !mergeStyle )
     143                                        mergeStyle = match[ 1 ];
     144                                else
     145                                {
     146                                        mergeStyle = null;
     147                                        break;
     148                                }
     149                        }
     150                }
     151
     152                if ( mergeStyle )
     153                {
     154                        for ( i = 0; i < count; i++ )
     155                        {
     156                                attrs = children[ i ].attributes;
     157                                attrs.style && ( attrs.style = stylesFilter( [ [ 'list-style-type'] ] )( attrs.style ) || '' );
     158                        }
     159
     160                        list.addStyle( 'list-style-type', mergeStyle );
     161                }
     162        }
     163
    121164        var cssLengthRelativeUnit = /^([.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz){1}?/i;
    122165        var emptyMarginRegex = /^(?:\b0[^\s]*\s*){1,4}$/;               // e.g. 0px 0pt 0px
    123166        var romanLiternalPattern = '^m{0,4}(cm|cd|d?c{0,3})(xc|xl|l?x{0,3})(ix|iv|v?i{0,3})$',
    124167                lowerRomanLiteralRegex = new RegExp( romanLiternalPattern ),
    125168                upperRomanLiteralRegex = new RegExp( romanLiternalPattern.toUpperCase() );
    126169
    127         var listBaseIndent = 0,
    128                  previousListItemMargin;
     170        var orderedPatterns = { 'decimal' : /\d+/, 'lower-roman': lowerRomanLiteralRegex, 'upper-roman': upperRomanLiteralRegex, 'lower-alpha' : /^[a-z]+$/, 'upper-alpha': /^[A-Z]+$/ },
     171                unorderedPatterns = { 'disc' : /[l\u00B7\u2002]/, 'circle' : /[\u006F\u00D8]/,'square' : /[\u006E\u25C6]/},
     172                listMarkerPatterns = { 'ol' : orderedPatterns, 'ul' : unorderedPatterns },
     173                romans = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ],
     174                alpahbets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    129175
    130         CKEDITOR.plugins.pastefromword =
    131         {
    132                 utils :
    133                 {
    134                         // Create a <cke:listbullet> which indicate an list item type.
    135                         createListBulletMarker : function ( bulletStyle, bulletText )
    136                         {
    137                                 var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' ),
    138                                         listType;
     176        // Convert roman numbering back to decimal.
     177        function fromRoman( str )
     178         {
     179                 str = str.toUpperCase();
     180                 var l = romans.length, retVal = 0;
     181                 for ( var i = 0; i < l; ++i )
     182                 {
     183                         for ( var j = romans[i], k = j[1].length; str.substr( 0, k ) == j[1]; str = str.substr( k ) )
     184                                 retVal += j[ 0 ];
     185                 }
     186                 return retVal;
     187         }
    139188
    140                                 // TODO: Support more list style type from MS-Word.
    141                                 if ( !bulletStyle )
    142                                 {
    143                                         bulletStyle = 'decimal';
    144                                         listType = 'ol';
    145                                 }
    146                                 else if ( bulletStyle[ 2 ] )
    147                                 {
    148                                         if ( !isNaN( bulletStyle[ 1 ] ) )
    149                                                 bulletStyle = 'decimal';
    150                                         else if ( lowerRomanLiteralRegex.test( bulletStyle[ 1 ] ) )
    151                                                 bulletStyle = 'lower-roman';
    152                                         else if ( upperRomanLiteralRegex.test( bulletStyle[ 1 ] ) )
    153                                                 bulletStyle = 'upper-roman';
    154                                         else if ( /^[a-z]+$/.test( bulletStyle[ 1 ] ) )
    155                                                 bulletStyle = 'lower-alpha';
    156                                         else if ( /^[A-Z]+$/.test( bulletStyle[ 1 ] ) )
    157                                                 bulletStyle = 'upper-alpha';
    158                                         // Simply use decimal for the rest forms of unrepresentable
    159                                         // numerals, e.g. Chinese...
    160                                         else
    161                                                 bulletStyle = 'decimal';
    162 
    163                                         listType = 'ol';
    164                                 }
    165                                 else
    166                                 {
    167                                         if ( /[l\u00B7\u2002]/.test( bulletStyle[ 1 ] ) )
    168                                                 bulletStyle = 'disc';
    169                                         else if ( /[\u006F\u00D8]/.test( bulletStyle[ 1 ] ) )
    170                                                 bulletStyle = 'circle';
    171                                         else if ( /[\u006E\u25C6]/.test( bulletStyle[ 1 ] ) )
    172                                                 bulletStyle = 'square';
    173                                         else
    174                                                 bulletStyle = 'disc';
    175 
    176                                         listType = 'ul';
    177                                 }
     189        // Convert alphabet numbering back to decimal.
     190        function fromAlphabet( str )
     191        {
     192                str = str.toUpperCase();
     193                var l = alpahbets.length, retVal = 1;
     194                for ( var x = 1; str.length > 0; x *= l )
     195                {
     196                        retVal += alpahbets.indexOf( str.charAt( str.length - 1 ) ) * x;
     197                        str = str.substr( 0, str.length - 1 );
     198                }
     199                return retVal;
     200        }
    178201
    179                                 // Represent list type as CSS style.
    180                                 marker.attributes =
    181                                 {
    182                                         'cke:listtype' : listType,
    183                                         'style' : 'list-style-type:' + bulletStyle + ';'
    184                                 };
     202        var listBaseIndent = 0,
     203                previousListItemMargin = null,
     204                previousListId;
     205
     206        CKEDITOR.plugins.pastefromword =
     207        {
     208                utils :
     209                {
     210                        // Create a <cke:listbullet> which indicate an list item type.
     211                        createListBulletMarker : function ( bullet, bulletText )
     212                        {
     213                                var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' );
     214                                marker.attributes = { 'cke:listsymbol' : bullet[ 0 ] };
    185215                                marker.add( new CKEDITOR.htmlParser.text( bulletText ) );
    186216                                return marker;
    187217                        },
     
    207237                                        listMarker;
    208238
    209239                                if ( ( listMarker = element.removeAnyChildWithName( 'cke:listbullet' ) )
    210                                           && listMarker.length
    211                                           && ( listMarker = listMarker[ 0 ] ) )
     240                                                && listMarker.length
     241                                                && ( listMarker = listMarker[ 0 ] ) )
    212242                                {
    213243                                        element.name = 'cke:li';
    214244
     
    219249                                                                        // Text-indent is not representing list item level any more.
    220250                                                                        [ 'text-indent' ],
    221251                                                                        [ 'line-height' ],
    222                                                                         // Resolve indent level from 'margin-left' value.
     252                                                                        // First attempt is to resolve indent level from on a constant margin increment.
    223253                                                                        [ ( /^margin(:?-left)?$/ ), null, function( margin )
    224254                                                                        {
    225                                                                                 // Be able to deal with component/short-hand form style.
     255                                                                                // Deal with component/short-hand form.
    226256                                                                                var values = margin.split( ' ' );
    227257                                                                                margin = CKEDITOR.plugins.pastefromword.utils.convertToPx( values[ 3 ] || values[ 1 ] || values [ 0 ] );
    228258                                                                                margin = parseInt( margin, 10 );
    229259
    230                                                                                 // Figure out the indent unit by looking at the first increament.
    231                                                                                 if ( !listBaseIndent && previousListItemMargin != undefined && margin > previousListItemMargin )
     260                                                                                // Figure out the indent unit by checking the first time of incrementation.
     261                                                                                if ( !listBaseIndent && previousListItemMargin != null && margin > previousListItemMargin )
    232262                                                                                        listBaseIndent = margin - previousListItemMargin;
    233263
    234                                                                                 attrs[ 'cke:margin' ] = previousListItemMargin = margin;
     264                                                                                previousListItemMargin = margin;
     265
     266                                                                                attrs[ 'cke:indent' ] = listBaseIndent && ( Math.ceil( margin / listBaseIndent ) + 1 ) || 1;
     267                                                                        } ],
     268                                                                        // The best situation: "mso-list:l0 level1 lfo2" tells the belonged list root, list item indentation, etc.
     269                                                                        [ ( /^mso-list$/ ), null, function( val )
     270                                                                        {
     271                                                                                val = val.split( ' ' );
     272                                                                                var listId = Number( val[ 0 ].match( /\d+/ ) ),
     273                                                                                        indent = Number( val[ 1 ].match( /\d+/ ) );
     274
     275                                                                                if ( listId != previousListId )
     276                                                                                        attrs[ 'cke:reset' ] = 1;
     277
     278                                                                                previousListId = listId;
     279                                                                                attrs[ 'cke:indent' ] = indent;
    235280                                                                        } ]
    236                                                         ] )( attrs.style, element ) || '' ;
     281                                                                ] )( attrs.style, element ) || '';
    237282                                        }
    238283
    239                                         // First level list item are always presented without a margin.
    240                                         !attrs[ 'cke:margin' ] && ( attrs[ 'cke:margin' ] = previousListItemMargin = 0 );
     284                                        // First level list item might be presented without a margin.
    241285
     286
     287                                        // In case all above doesn't apply.
     288                                        if ( !attrs[ 'cke:indent' ] )
     289                                        {
     290                                                previousListItemMargin = 0;
     291                                                attrs[ 'cke:indent' ] = 1;
     292                                        }
     293
    242294                                        // Inherit list-type-style from bullet.
    243295                                        var listBulletAttrs = listMarker.attributes,
    244                                                 listBulletStyle = listBulletAttrs.style;
     296                                                        listBulletStyle = listBulletAttrs.style;
    245297
    246298                                        element.addStyle( listBulletStyle );
    247299                                        CKEDITOR.tools.extend( attrs, listBulletAttrs );
    248300                                        return true;
    249301                                }
     302                                // Current list disconnected.
     303                                else
     304                                        previousListId = previousListItemMargin = listBaseIndent = null;
    250305
    251306                                return false;
    252307                        },
     
    374429                                        var children = element.children, child,
    375430                                                        listItem,   // The current processing cke:li element.
    376431                                                        listItemAttrs,
    377                                                         listType,   // Determine the root type of the list.
    378432                                                        listItemIndent, // Indent level of current list item.
     433                                                        lastIndent,
    379434                                                        lastListItem, // The previous one just been added to the list.
    380                                                         list, parentList, // Current staging list and it's parent list if any.
    381                                                         indent;
     435                                                        list, // Current staging list and it's parent list if any.
     436                                                        openedLists = [],
     437                                                        previousListStyleType,
     438                                                        previousListType,
     439                                                        previousNumeric;
    382440
     441                                        // Properties of the list item are to be resolved from the list bullet.
     442                                        var bullet,
     443                                                listType,
     444                                                listStyleType,
     445                                                itemNumeric;
     446
    383447                                        for ( var i = 0; i < children.length; i++ )
    384448                                        {
    385449                                                child = children[ i ];
     
    389453                                                        child.name = 'li';
    390454                                                        listItem = child;
    391455                                                        listItemAttrs = listItem.attributes;
    392                                                         listType = listItem.attributes[ 'cke:listtype' ];
     456                                                        bullet = listItemAttrs[ 'cke:listsymbol' ].match( /^([^\s]+?)([.)]?)$/ );
     457                                                        listType = listStyleType = itemNumeric = null;
    393458
     459                                                        if ( listItemAttrs[ 'cke:ignored' ] )
     460                                                        {
     461                                                                children.splice( i--, 1 );
     462                                                                continue;
     463                                                        }
     464
     465                                                        // This's from a new list root.
     466                                                        listItemAttrs[ 'cke:reset' ] && ( list = lastIndent = lastListItem = null );
     467
    394468                                                        // List item indent level might come from a real list indentation or
    395469                                                        // been resolved from a pseudo list item's margin value, even get
    396470                                                        // no indentation at all.
    397                                                         listItemIndent = parseInt( listItemAttrs[ 'cke:indent' ], 10 )
    398                                                                                                         || listBaseIndent && ( Math.ceil( listItemAttrs[ 'cke:margin' ] / listBaseIndent ) + 1 )
    399                                                                                                         || 1;
     471                                                        listItemIndent = Number( listItemAttrs[ 'cke:indent' ] );
    400472
    401                                                         // Ignore the 'list-style-type' attribute if it's matched with
    402                                                         // the list root element's default style type.
    403                                                         listItemAttrs.style && ( listItemAttrs.style =
    404                                                                 CKEDITOR.plugins.pastefromword.filters.stylesFilter(
    405                                                                         [
    406                                                                                 [ 'list-style-type', listType == 'ol' ? 'decimal' : 'disc' ]
    407                                                                         ] )( listItemAttrs.style )
    408                                                                         || '' );
     473                                                        // We're moving out of the current list, cleaning up.
     474                                                        if ( listItemIndent != lastIndent )
     475                                                                previousListType = previousListStyleType = previousNumeric = null;
    409476
     477                                                        if ( !bullet )
     478                                                        {
     479                                                                listType = 'ol';
     480                                                                listStyleType = 'decimal';
     481                                                        }
     482                                                        else
     483                                                        {
     484                                                                // Probably share the same list style type with previous list item,
     485                                                                // give it priority to avoid ambiguous between C(Alpha) and C.(Roman).
     486                                                                if ( previousListType && listMarkerPatterns[ previousListType ] [ previousListStyleType ].test( bullet[ 1 ] ) )
     487                                                                {
     488                                                                        listType = previousListType;
     489                                                                        listStyleType = previousListStyleType;
     490                                                                }
     491                                                                else
     492                                                                {
     493                                                                        for ( var type in listMarkerPatterns )
     494                                                                        {
     495                                                                                for ( var style in listMarkerPatterns[ type ] )
     496                                                                                {
     497                                                                                        if ( listMarkerPatterns[ type ][ style ].test( bullet[ 1 ] ) )
     498                                                                                        {
     499                                                                                                // Small numbering has higher priority, when dealing with ambiguous
     500                                                                                                // between C(Alpha) and C.(Roman).
     501                                                                                                if ( type == 'ol' && /alpha|roman/.test( style ) )
     502                                                                                                {
     503                                                                                                        var num = /roman/.test( style ) ? fromRoman( bullet[ 1 ] ) : fromAlphabet( bullet[ 1 ] );
     504                                                                                                        if ( !itemNumeric || num < itemNumeric )
     505                                                                                                        {
     506                                                                                                                itemNumeric = num;
     507                                                                                                                listType = type;
     508                                                                                                                listStyleType = style;
     509                                                                                                        }
     510                                                                                                }
     511                                                                                                else
     512                                                                                                {
     513                                                                                                        listType = type;
     514                                                                                                        listStyleType = style;
     515                                                                                                        break;
     516                                                                                                }
     517                                                                                        }
     518                                                                                }
     519                                                                        }
     520                                                                }
     521
     522                                                                // Simply use decimal/disc for the rest forms of unrepresentable
     523                                                                // numerals, e.g. Chinese..., but as long as there a second part
     524                                                                // included, it has a bigger chance of being a order list ;)
     525                                                                !listType && ( listType = bullet[ 2 ] ? 'ol' : 'ul' );
     526                                                        }
     527
     528                                                        previousListType = listType;
     529                                                        previousListStyleType = listStyleType || ( listType == 'ol' ? 'decimal' : 'disc' );
     530                                                        if ( listStyleType && listStyleType != ( listType == 'ol' ? 'decimal' : 'disc' ) )
     531                                                                listItem.addStyle( 'list-style-type', listStyleType );
     532
     533                                                        // Figure out start numbering.
     534                                                        if ( listType == 'ol' )
     535                                                        {
     536                                                                switch ( listStyleType )
     537                                                                {
     538                                                                        case 'decimal' :
     539                                                                                itemNumeric = bullet[ 1 ];
     540                                                                                break;
     541                                                                        case 'lower-roman':
     542                                                                        case 'upper-roman':
     543                                                                                itemNumeric = fromRoman( bullet[ 1 ] );
     544                                                                                break;
     545                                                                        case 'lower-alpha':
     546                                                                        case 'upper-alpha':
     547                                                                                itemNumeric = fromAlphabet( bullet[ 1 ] );
     548                                                                                break;
     549                                                                }
     550
     551                                                                if ( itemNumeric && itemNumeric != ( previousNumeric ? previousNumeric + 1 : 1 ) )
     552                                                                        listItem.attributes.value = itemNumeric;
     553                                                                previousNumeric = itemNumeric;
     554                                                        }
     555
     556                                                        // Start the list construction.
    410557                                                        if ( !list )
    411558                                                        {
    412                                                                 list = new CKEDITOR.htmlParser.element( listType );
     559                                                                openedLists.push( list = new CKEDITOR.htmlParser.element( listType ) );
    413560                                                                list.add( listItem );
    414561                                                                children[ i ] = list;
    415562                                                        }
    416563                                                        else
    417564                                                        {
    418                                                                 if ( listItemIndent > indent )
     565                                                                if ( listItemIndent > lastIndent )
    419566                                                                {
    420                                                                         list = new CKEDITOR.htmlParser.element( listType );
     567                                                                        openedLists.push( list = new CKEDITOR.htmlParser.element( listType ) );
    421568                                                                        list.add( listItem );
    422569                                                                        lastListItem.add( list );
    423570                                                                }
    424                                                                 else if ( listItemIndent < indent )
     571                                                                else if ( listItemIndent < lastIndent )
    425572                                                                {
    426573                                                                        // There might be a negative gap between two list levels. (#4944)
    427                                                                         var diff = indent - listItemIndent,
    428                                                                                 parent;
     574                                                                        var diff = lastIndent - listItemIndent,
     575                                                                                        parent;
    429576                                                                        while ( diff-- && ( parent = list.parent ) )
    430577                                                                                list = parent.parent;
    431578
     
    438585                                                        }
    439586
    440587                                                        lastListItem = listItem;
    441                                                         indent = listItemIndent;
     588                                                        lastIndent = listItemIndent;
    442589                                                }
    443                                                 else
    444                                                         list = null;
     590                                                else if ( list )
     591                                                        list = lastIndent = lastListItem = null;
    445592                                        }
    446593
    447                                         listBaseIndent = 0;
     594                                        for ( i = 0; i < openedLists.length; i++ )
     595                                                mergeListStyleType( openedLists[ i ] );
     596
     597                                        list = lastIndent = lastListItem = previousListId = previousListItemMargin = listBaseIndent = null;
    448598                                },
    449599
    450600                                /**
     
    8951045
    8961046                                                        var listSymbol =  listSymbolNode && ( listSymbolNode.value || 'l.' ),
    8971047                                                                listType = listSymbol.match( /^([^\s]+?)([.)]?)$/ );
    898                                                         return createListBulletMarker( listType, listSymbol );
     1048
     1049                                                        var marker = createListBulletMarker( listType, listSymbol );
     1050
     1051                                                        // Some non-existed list items might be carried by an inconsequential list, indicate by "mso-hide:all/display:none",
     1052                                                        // those are to be removed later, now mark it with "cke:ignored".
     1053                                                        var ancestor = element.getAncestor( 'span' );
     1054                                                        if ( ancestor && /mso-hide:\s*all|display:\s*none/.test( ancestor.attributes.style ) )
     1055                                                                marker.attributes[ 'cke:ignored' ] = 1;
     1056                                                        return marker;
    8991057                                                }
    9001058
    9011059                                                // Update the src attribute of image element with href.
     
    9631121                                        // Provide a white-list of styles that we preserve, those should
    9641122                                        // be the ones that could later be altered with editor tools.
    9651123                                        [
     1124                                                // Leave list-style-type
     1125                                                [ /^list-style-type$/, null ],
     1126
    9661127                                                // Preserve margin-left/right which used as default indent style in the editor.
    9671128                                                [ ( /^margin$|^margin-(?!bottom|top)/ ), null, function( value, element, name )
    9681129                                                        {
     
    11291290                // Allow extending data filter rules.
    11301291                editor.fire( 'beforeCleanWord', { filter : dataFilter } );
    11311292
     1293                data = dataProcessor.toHtml( data, false );
    11321294                try
    11331295                {
    1134                         data = dataProcessor.toHtml( data, false );
    11351296                }
    11361297                catch ( e )
    11371298                {
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy