2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 var cssStyle = CKEDITOR.htmlParser.cssStyle,
\r
9 cssLength = CKEDITOR.tools.cssLength;
\r
11 var cssLengthRegex = /^((?:\d*(?:\.\d+))|(?:\d+))(.*)?$/i;
\r
14 * Replacing the former CSS length value with the later one, with
\r
15 * adjustment to the length unit.
\r
17 function replaceCssLength( length1, length2 )
\r
19 var parts1 = cssLengthRegex.exec( length1 ),
\r
20 parts2 = cssLengthRegex.exec( length2 );
\r
22 // Omit pixel length unit when necessary,
\r
23 // e.g. replaceCssLength( 10, '20px' ) -> 20
\r
26 if ( !parts1[ 2 ] && parts2[ 2 ] == 'px' )
\r
28 if ( parts1[ 2 ] == 'px' && !parts2[ 2 ] )
\r
29 return parts2[ 1 ] + 'px';
\r
35 var htmlFilterRules =
\r
39 $ : function( element )
\r
41 var attributes = element.attributes,
\r
42 realHtml = attributes && attributes[ 'data-cke-realelement' ],
\r
43 realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ),
\r
44 realElement = realFragment && realFragment.children[ 0 ];
\r
46 // Width/height in the fake object are subjected to clone into the real element.
\r
47 if ( realElement && element.attributes[ 'data-cke-resizable' ] )
\r
49 var styles = new cssStyle( element ).rules,
\r
50 realAttrs = realElement.attributes,
\r
51 width = styles.width,
\r
52 height = styles.height;
\r
54 width && ( realAttrs.width = replaceCssLength( realAttrs.width, width ) );
\r
55 height && ( realAttrs.height = replaceCssLength( realAttrs.height, height ) );
\r
63 CKEDITOR.plugins.add( 'fakeobjects',
\r
65 requires : [ 'htmlwriter' ],
\r
67 afterInit : function( editor )
\r
69 var dataProcessor = editor.dataProcessor,
\r
70 htmlFilter = dataProcessor && dataProcessor.htmlFilter;
\r
73 htmlFilter.addRules( htmlFilterRules );
\r
77 CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable )
\r
79 var lang = this.lang.fakeobjects,
\r
80 label = lang[ realElementType ] || lang.unknown;
\r
84 'class' : className,
\r
85 src : CKEDITOR.getUrl( 'images/spacer.gif' ),
\r
86 'data-cke-realelement' : encodeURIComponent( realElement.getOuterHtml() ),
\r
87 'data-cke-real-node-type' : realElement.type,
\r
90 align : realElement.getAttribute( 'align' ) || ''
\r
93 if ( realElementType )
\r
94 attributes[ 'data-cke-real-element-type' ] = realElementType;
\r
98 attributes[ 'data-cke-resizable' ] = isResizable;
\r
100 var fakeStyle = new cssStyle();
\r
102 var width = realElement.getAttribute( 'width' ),
\r
103 height = realElement.getAttribute( 'height' );
\r
105 width && ( fakeStyle.rules.width = cssLength( width ) );
\r
106 height && ( fakeStyle.rules.height = cssLength( height ) );
\r
107 fakeStyle.populate( attributes );
\r
110 return this.document.createElement( 'img', { attributes : attributes } );
\r
113 CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable )
\r
115 var lang = this.lang.fakeobjects,
\r
116 label = lang[ realElementType ] || lang.unknown,
\r
119 var writer = new CKEDITOR.htmlParser.basicWriter();
\r
120 realElement.writeHtml( writer );
\r
121 html = writer.getHtml();
\r
125 'class' : className,
\r
126 src : CKEDITOR.getUrl( 'images/spacer.gif' ),
\r
127 'data-cke-realelement' : encodeURIComponent( html ),
\r
128 'data-cke-real-node-type' : realElement.type,
\r
131 align : realElement.attributes.align || ''
\r
134 if ( realElementType )
\r
135 attributes[ 'data-cke-real-element-type' ] = realElementType;
\r
139 attributes[ 'data-cke-resizable' ] = isResizable;
\r
140 var realAttrs = realElement.attributes,
\r
141 fakeStyle = new cssStyle();
\r
143 var width = realAttrs.width,
\r
144 height = realAttrs.height;
\r
146 width != undefined && ( fakeStyle.rules.width = cssLength( width ) );
\r
147 height != undefined && ( fakeStyle.rules.height = cssLength ( height ) );
\r
148 fakeStyle.populate( attributes );
\r
151 return new CKEDITOR.htmlParser.element( 'img', attributes );
\r
154 CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement )
\r
156 if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT )
\r
159 var element = CKEDITOR.dom.element.createFromHtml(
\r
160 decodeURIComponent( fakeElement.data( 'cke-realelement' ) ),
\r
163 if ( fakeElement.data( 'cke-resizable') )
\r
165 var width = fakeElement.getStyle( 'width' ),
\r
166 height = fakeElement.getStyle( 'height' );
\r
168 width && element.setAttribute( 'width', replaceCssLength( element.getAttribute( 'width' ), width ) );
\r
169 height && element.setAttribute( 'height', replaceCssLength( element.getAttribute( 'height' ), height ) );
\r