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