X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Ffakeobjects%2Fplugin.js;h=d4ebce58c109a90d425da1ad40006c9166ebeeb1;hp=234e3a8c756818d4d310c5cae7f2937db741e809;hb=refs%2Ftags%2Fv3.6.1;hpb=4e70ea24db840898be8cc21c950363a52a2a6aba diff --git a/_source/plugins/fakeobjects/plugin.js b/_source/plugins/fakeobjects/plugin.js index 234e3a8..d4ebce5 100644 --- a/_source/plugins/fakeobjects/plugin.js +++ b/_source/plugins/fakeobjects/plugin.js @@ -5,6 +5,33 @@ 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 : @@ -16,28 +43,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ), realElement = realFragment && realFragment.children[ 0 ]; - // If we have width/height in the element, we must move it into - // the real element. + // Width/height in the fake object are subjected to clone into the real element. if ( realElement && element.attributes[ 'data-cke-resizable' ] ) { - var style = element.attributes.style; - - if ( style ) - { - // Get the width from the style. - var match = /(?:^|\s)width\s*:\s*(\d+)/i.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+)/i.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,69 +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, - label = lang[ realElementType ] || lang.unknown; - var attributes = + CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable ) { - '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' ) || '' - }; + var lang = this.lang.fakeobjects, + label = lang[ realElementType ] || lang.unknown; - if ( realElementType ) - attributes[ 'data-cke-real-element-type' ] = realElementType; + 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; - if ( isResizable ) - attributes[ 'data-cke-resizable' ] = isResizable; + var fakeStyle = new cssStyle(); - return this.document.createElement( 'img', { attributes : attributes } ); -}; + var width = realElement.getAttribute( 'width' ), + height = realElement.getAttribute( 'height' ); -CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) -{ - var lang = this.lang.fakeobjects, - label = lang[ realElementType ] || lang.unknown, - html; + width && ( fakeStyle.rules.width = cssLength( width ) ); + height && ( fakeStyle.rules.height = cssLength( height ) ); + fakeStyle.populate( attributes ); + } - var writer = new CKEDITOR.htmlParser.basicWriter(); - realElement.writeHtml( writer ); - html = writer.getHtml(); + return this.document.createElement( 'img', { attributes : attributes } ); + }; - var attributes = + CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) { - '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 || '' + 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[ 'data-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[ 'data-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 ) -{ - if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT ) - return null; + width && element.setAttribute( 'width', replaceCssLength( element.getAttribute( 'width' ), width ) ); + height && element.setAttribute( 'height', replaceCssLength( element.getAttribute( 'height' ), height ) ); + } + + return element; + }; - return CKEDITOR.dom.element.createFromHtml( - decodeURIComponent( fakeElement.data( 'cke-realelement' ) ), - this.document ); -}; +})();