X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ffakeobjects%2Fplugin.js;h=f55922a7ea1eefdae1ca49ed619ac3101bee2e77;hb=2f22c0c38f17e75be5541089076885442aaa2377;hp=137c677f99ef6bd0c29002894a8a0acb05fc1ad8;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/plugins/fakeobjects/plugin.js b/_source/plugins/fakeobjects/plugin.js index 137c677..f55922a 100644 --- a/_source/plugins/fakeobjects/plugin.js +++ b/_source/plugins/fakeobjects/plugin.js @@ -1,43 +1,58 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ (function() { + var cssStyle = CKEDITOR.htmlParser.cssStyle, + cssLength = CKEDITOR.tools.cssLength; + + var cssLengthRegex = /^((?:\d*(?:\.\d+))|(?:\d+))(.*)?$/i; + + /* + * Replacing the former CSS length value with the later one, with + * adjustment to the length unit. + */ + function replaceCssLength( length1, length2 ) + { + var parts1 = cssLengthRegex.exec( length1 ), + parts2 = cssLengthRegex.exec( length2 ); + + // Omit pixel length unit when necessary, + // e.g. replaceCssLength( 10, '20px' ) -> 20 + if ( parts1 ) + { + if ( !parts1[ 2 ] && parts2[ 2 ] == 'px' ) + return parts2[ 1 ]; + if ( parts1[ 2 ] == 'px' && !parts2[ 2 ] ) + return parts2[ 1 ] + 'px'; + } + + return length2; + } + var htmlFilterRules = { elements : { $ : function( element ) { - var realHtml = element.attributes._cke_realelement, + var attributes = element.attributes, + realHtml = attributes && attributes[ 'data-cke-realelement' ], realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ), realElement = realFragment && realFragment.children[ 0 ]; - if ( realElement ) + // Width/height in the fake object are subjected to clone into the real element. + if ( realElement && element.attributes[ 'data-cke-resizable' ] ) { - // If we have width/height in the element, we must move it into - // the real element. - - var style = element.attributes.style; - - if ( style ) - { - // Get the width from the style. - var match = /(?:^|\s)width\s*:\s*(\d+)/.exec( style ), - width = match && match[1]; + var styles = new cssStyle( element ).rules, + realAttrs = realElement.attributes, + width = styles.width, + height = styles.height; - // Get the height from the style. - match = /(?:^|\s)height\s*:\s*(\d+)/.exec( style ); - var height = match && match[1]; - - if ( width ) - realElement.attributes.width = width; - - if ( height ) - realElement.attributes.height = height; - } + width && ( realAttrs.width = replaceCssLength( realAttrs.width, width ) ); + height && ( realAttrs.height = replaceCssLength( realAttrs.height, height ) ); } return realElement; @@ -58,54 +73,103 @@ For licensing, see LICENSE.html or http://ckeditor.com/license htmlFilter.addRules( htmlFilterRules ); } }); -})(); -CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable ) -{ - var lang = this.lang.fakeobjects; - var attributes = + CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable ) { - 'class' : className, - src : CKEDITOR.getUrl( 'images/spacer.gif' ), - _cke_realelement : encodeURIComponent( realElement.getOuterHtml() ), - alt : lang[ realElementType ] || lang.unknown - }; - if ( realElementType ) - attributes._cke_real_element_type = realElementType; - if ( isResizable ) - attributes._cke_resizable = isResizable; + var lang = this.lang.fakeobjects, + label = lang[ realElementType ] || lang.unknown; - return this.document.createElement( 'img', { attributes : attributes } ); -}; + var attributes = + { + 'class' : className, + src : CKEDITOR.getUrl( 'images/spacer.gif' ), + 'data-cke-realelement' : encodeURIComponent( realElement.getOuterHtml() ), + 'data-cke-real-node-type' : realElement.type, + alt : label, + title : label, + align : realElement.getAttribute( 'align' ) || '' + }; + + if ( realElementType ) + attributes[ 'data-cke-real-element-type' ] = realElementType; + + if ( isResizable ) + { + attributes[ 'data-cke-resizable' ] = isResizable; -CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) -{ - var writer = new CKEDITOR.htmlParser.basicWriter(); + var fakeStyle = new cssStyle(); - realElement.writeHtml( writer ); + var width = realElement.getAttribute( 'width' ), + height = realElement.getAttribute( 'height' ); - var html = writer.getHtml(); - var lang = this.lang.fakeobjects; + width && ( fakeStyle.rules.width = cssLength( width ) ); + height && ( fakeStyle.rules.height = cssLength( height ) ); + fakeStyle.populate( attributes ); + } - var attributes = + return this.document.createElement( 'img', { attributes : attributes } ); + }; + + CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) { - 'class' : className, - src : CKEDITOR.getUrl( 'images/spacer.gif' ), - _cke_realelement : encodeURIComponent( html ), - alt : lang[ realElementType ] || lang.unknown + var lang = this.lang.fakeobjects, + label = lang[ realElementType ] || lang.unknown, + html; + + var writer = new CKEDITOR.htmlParser.basicWriter(); + realElement.writeHtml( writer ); + html = writer.getHtml(); + + var attributes = + { + 'class' : className, + src : CKEDITOR.getUrl( 'images/spacer.gif' ), + 'data-cke-realelement' : encodeURIComponent( html ), + 'data-cke-real-node-type' : realElement.type, + alt : label, + title : label, + align : realElement.attributes.align || '' + }; + + if ( realElementType ) + attributes[ 'data-cke-real-element-type' ] = realElementType; + + if ( isResizable ) + { + attributes[ 'data-cke-resizable' ] = isResizable; + var realAttrs = realElement.attributes, + fakeStyle = new cssStyle(); + + var width = realAttrs.width, + height = realAttrs.height; + + width != undefined && ( fakeStyle.rules.width = cssLength( width ) ); + height != undefined && ( fakeStyle.rules.height = cssLength ( height ) ); + fakeStyle.populate( attributes ); + } + + return new CKEDITOR.htmlParser.element( 'img', attributes ); }; - if ( realElementType ) - attributes._cke_real_element_type = realElementType; + CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement ) + { + if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT ) + return null; - if ( isResizable ) - attributes._cke_resizable = isResizable; + var element = CKEDITOR.dom.element.createFromHtml( + decodeURIComponent( fakeElement.data( 'cke-realelement' ) ), + this.document ); - return new CKEDITOR.htmlParser.element( 'img', attributes ); -}; + if ( fakeElement.data( 'cke-resizable') ) + { + var width = fakeElement.getStyle( 'width' ), + height = fakeElement.getStyle( 'height' ); -CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement ) -{ - var html = decodeURIComponent( fakeElement.getAttribute( '_cke_realelement' ) ); - return CKEDITOR.dom.element.createFromHtml( html, this.document ); -}; + width && element.setAttribute( 'width', replaceCssLength( element.getAttribute( 'width' ), width ) ); + height && element.setAttribute( 'height', replaceCssLength( element.getAttribute( 'height' ), height ) ); + } + + return element; + }; + +})();