X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Ftools.js;h=a529c6a8b05e0fd7b9d183063bb489388e41f43b;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=a1bff9af0da81409fca280cd54ae3a24aff4b923;hpb=e371ddf8abcb89013e20e6d0dd746adec344d0e5;p=ckeditor.git diff --git a/_source/core/tools.js b/_source/core/tools.js index a1bff9a..a529c6a 100644 --- a/_source/core/tools.js +++ b/_source/core/tools.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -346,19 +346,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Replace characters can't be represented through CSS Selectors string - * by CSS Escape Notation where the character escape sequence consists - * of a backslash character (\) followed by the orginal characters. - * Ref: http://www.w3.org/TR/css3-selectors/#grammar - * @param cssSelectText - * @return the escaped selector text. - */ - escapeCssSelector : function( cssSelectText ) - { - return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' ); - }, - - /** * Gets a unique number for this CKEDITOR execution session. It returns * progressive numbers starting at 1. * @function @@ -651,7 +638,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { return functions.push( function() { - fn.apply( scope || this, arguments ); + return fn.apply( scope || this, arguments ); }) - 1; }, @@ -693,14 +680,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license */ cssLength : (function() { - var decimalRegex = /^\d+(?:\.\d+)?$/; return function( length ) { - return length + ( decimalRegex.test( length ) ? 'px' : '' ); + return length + ( !length || isNaN( Number( length ) ) ? '' : 'px' ); }; })(), /** + * Convert the specified CSS length value to the calculated pixel length inside this page. + * Note: Percentage based value is left intact. + * @param {String} cssLength CSS length value. + */ + convertToPx : ( function () + { + var calculator; + + return function( cssLength ) + { + if ( !calculator ) + { + calculator = CKEDITOR.dom.element.createFromHtml( + '
', CKEDITOR.document ); + CKEDITOR.document.getBody().append( calculator ); + } + + if ( !(/%$/).test( cssLength ) ) + { + calculator.setStyle( 'width', cssLength ); + return calculator.$.clientWidth; + } + + return cssLength; + }; + } )(), + + /** * String specified by {@param str} repeats {@param times} times. * @param str * @param times @@ -740,7 +756,82 @@ For licensing, see LICENSE.html or http://ckeditor.com/license genKey : function() { return Array.prototype.slice.call( arguments ).join( '-' ); + }, + + /** + * Try to avoid differences in the style attribute. + * + * @param {String} styleText The style data to be normalized. + * @param {Boolean} [nativeNormalize=false] Parse the data using the browser. + * @returns {String} The normalized value. + */ + normalizeCssText: function( styleText, nativeNormalize ) { + var props = [], + name, + parsedProps = CKEDITOR.tools.parseCssText( styleText, true, nativeNormalize ); + + for ( name in parsedProps ) + props.push( name + ':' + parsedProps[ name ] ); + + props.sort(); + + return props.length ? ( props.join( ';' ) + ';' ) : ''; + }, + + /** + * Find and convert rgb(x,x,x) colors definition to hexadecimal notation. + * @param {String} styleText The style data (or just a string containing rgb colors) to be converted. + * @returns {String} The style data with rgb colors converted to hexadecimal equivalents. + */ + convertRgbToHex: function( styleText ) { + return styleText.replace( /(?:rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\))/gi, function( match, red, green, blue ) { + var color = [ red, green, blue ]; + // Add padding zeros if the hex value is less than 0x10. + for ( var i = 0; i < 3; i++ ) + color[ i ] = ( '0' + parseInt( color[ i ], 10 ).toString( 16 ) ).slice( -2 ); + return '#' + color.join( '' ); + }); + }, + + /** + * Turn inline style text properties into one hash. + * + * @param {String} styleText The style data to be parsed. + * @param {Boolean} [normalize=false] Normalize properties and values + * (e.g. trim spaces, convert to lower case). + * @param {Boolean} [nativeNormalize=false] Parse the data using the browser. + * @returns {String} The object containing parsed properties. + */ + parseCssText: function( styleText, normalize, nativeNormalize ) { + var retval = {}; + + if ( nativeNormalize ) { + // Injects the style in a temporary span object, so the browser parses it, + // retrieving its final format. + var temp = new CKEDITOR.dom.element( 'span' ); + temp.setAttribute( 'style', styleText ); + styleText = CKEDITOR.tools.convertRgbToHex( temp.getAttribute( 'style' ) || '' ); + } + + // IE will leave a single semicolon when failed to parse the style text. (#3891) + if ( !styleText || styleText == ';' ) + return retval; + + styleText.replace( /"/g, '"' ).replace( /\s*([^:;\s]+)\s*:\s*([^;]+)\s*(?=;|$)/g, function( match, name, value ) { + if ( normalize ) { + name = name.toLowerCase(); + // Normalize font-family property, ignore quotes and being case insensitive. (#7322) + // http://www.w3.org/TR/css3-fonts/#font-family-the-font-family-property + if ( name == 'font-family' ) + value = value.toLowerCase().replace( /["']/g, '' ).replace( /\s*,\s*/g, ',' ); + value = CKEDITOR.tools.trim( value ); + } + + retval[ name ] = value; + }); + return retval; } + }; })();