JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
58c5bcd43aab18b912bdf7d815fce4f8c2f3d47b
[ckeditor.git] / _source / plugins / fakeobjects / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         var htmlFilterRules =\r
9         {\r
10                 elements :\r
11                 {\r
12                         $ : function( element )\r
13                         {\r
14                                 var realHtml = element.attributes._cke_realelement,\r
15                                         realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ),\r
16                                         realElement = realFragment && realFragment.children[ 0 ];\r
17 \r
18                                 if ( realElement )\r
19                                 {\r
20                                         // If we have width/height in the element, we must move it into\r
21                                         // the real element.\r
22 \r
23                                         var style = element.attributes.style;\r
24 \r
25                                         if ( style )\r
26                                         {\r
27                                                 // Get the width from the style.\r
28                                                 var match = /(?:^|\s)width\s*:\s*(\d+)/i.exec( style ),\r
29                                                         width = match && match[1];\r
30 \r
31                                                 // Get the height from the style.\r
32                                                 match = /(?:^|\s)height\s*:\s*(\d+)/i.exec( style );\r
33                                                 var height = match && match[1];\r
34 \r
35                                                 if ( width )\r
36                                                         realElement.attributes.width = width;\r
37 \r
38                                                 if ( height )\r
39                                                         realElement.attributes.height = height;\r
40                                         }\r
41                                 }\r
42 \r
43                                 return realElement;\r
44                         }\r
45                 }\r
46         };\r
47 \r
48         CKEDITOR.plugins.add( 'fakeobjects',\r
49         {\r
50                 requires : [ 'htmlwriter' ],\r
51 \r
52                 afterInit : function( editor )\r
53                 {\r
54                         var dataProcessor = editor.dataProcessor,\r
55                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
56 \r
57                         if ( htmlFilter )\r
58                                 htmlFilter.addRules( htmlFilterRules );\r
59                 }\r
60         });\r
61 })();\r
62 \r
63 CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable )\r
64 {\r
65         var lang = this.lang.fakeobjects;\r
66         var attributes =\r
67         {\r
68                 'class' : className,\r
69                 src : CKEDITOR.getUrl( 'images/spacer.gif' ),\r
70                 _cke_realelement : encodeURIComponent( realElement.getOuterHtml() ),\r
71                 alt : lang[ realElementType ] || lang.unknown\r
72         };\r
73         if ( realElementType )\r
74                 attributes._cke_real_element_type = realElementType;\r
75         if ( isResizable )\r
76                 attributes._cke_resizable = isResizable;\r
77 \r
78         return this.document.createElement( 'img', { attributes : attributes } );\r
79 };\r
80 \r
81 CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable )\r
82 {\r
83         var writer = new CKEDITOR.htmlParser.basicWriter();\r
84 \r
85         realElement.writeHtml( writer );\r
86 \r
87         var html = writer.getHtml();\r
88         var lang = this.lang.fakeobjects;\r
89 \r
90         var attributes =\r
91         {\r
92                 'class' : className,\r
93                 src : CKEDITOR.getUrl( 'images/spacer.gif' ),\r
94                 _cke_realelement : encodeURIComponent( html ),\r
95                 alt : lang[ realElementType ] || lang.unknown\r
96         };\r
97 \r
98         if ( realElementType )\r
99                 attributes._cke_real_element_type = realElementType;\r
100 \r
101         if ( isResizable )\r
102                 attributes._cke_resizable = isResizable;\r
103 \r
104         return new CKEDITOR.htmlParser.element( 'img', attributes );\r
105 };\r
106 \r
107 CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement )\r
108 {\r
109         var html = decodeURIComponent( fakeElement.getAttribute( '_cke_realelement' ) );\r
110         return CKEDITOR.dom.element.createFromHtml( html, this.document );\r
111 };\r