JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5
[ckeditor.git] / _source / plugins / fakeobjects / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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 attributes = element.attributes,\r
15                                         realHtml = attributes && attributes[ 'data-cke-realelement' ],\r
16                                         realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ),\r
17                                         realElement = realFragment && realFragment.children[ 0 ];\r
18 \r
19                                 // If we have width/height in the element, we must move it into\r
20                                 // the real element.\r
21                                 if ( realElement && element.attributes[ 'data-cke-resizable' ] )\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                 label = lang[ realElementType ] || lang.unknown;\r
67 \r
68         var attributes =\r
69         {\r
70                 'class' : className,\r
71                 src : CKEDITOR.getUrl( 'images/spacer.gif' ),\r
72                 'data-cke-realelement' : encodeURIComponent( realElement.getOuterHtml() ),\r
73                 'data-cke-real-node-type' : realElement.type,\r
74                 alt : label,\r
75                 title : label,\r
76                 align : realElement.getAttribute( 'align' ) || ''\r
77         };\r
78 \r
79         if ( realElementType )\r
80                 attributes[ 'data-cke-real-element-type' ] = realElementType;\r
81 \r
82         if ( isResizable )\r
83                 attributes[ 'data-cke-resizable' ] = isResizable;\r
84 \r
85         return this.document.createElement( 'img', { attributes : attributes } );\r
86 };\r
87 \r
88 CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable )\r
89 {\r
90         var lang = this.lang.fakeobjects,\r
91                 label = lang[ realElementType ] || lang.unknown,\r
92                 html;\r
93 \r
94         var writer = new CKEDITOR.htmlParser.basicWriter();\r
95         realElement.writeHtml( writer );\r
96         html = writer.getHtml();\r
97 \r
98         var attributes =\r
99         {\r
100                 'class' : className,\r
101                 src : CKEDITOR.getUrl( 'images/spacer.gif' ),\r
102                 'data-cke-realelement' : encodeURIComponent( html ),\r
103                 'data-cke-real-node-type' : realElement.type,\r
104                 alt : label,\r
105                 title : label,\r
106                 align : realElement.attributes.align || ''\r
107         };\r
108 \r
109         if ( realElementType )\r
110                 attributes[ 'data-cke-real-element-type' ] = realElementType;\r
111 \r
112         if ( isResizable )\r
113                 attributes[ 'data-cke-resizable' ] = isResizable;\r
114 \r
115         return new CKEDITOR.htmlParser.element( 'img', attributes );\r
116 };\r
117 \r
118 CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement )\r
119 {\r
120         if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT )\r
121                 return null;\r
122 \r
123         return CKEDITOR.dom.element.createFromHtml(\r
124                 decodeURIComponent( fakeElement.data( 'cke-realelement' ) ),\r
125                 this.document );\r
126 };\r