Blame view

mobile/source/ext/ckeditor/_source/plugins/fakeobjects/plugin.js 3.31 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  /*
  Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  For licensing, see LICENSE.html or http://ckeditor.com/license
  */
  
  (function()
  {
  	var htmlFilterRules =
  	{
  		elements :
  		{
  			$ : function( element )
  			{
  				var attributes = element.attributes,
  					realHtml = attributes && attributes[ 'data-cke-realelement' ],
  					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.
  				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];
  
  						// 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;
  					}
  				}
  
  				return realElement;
  			}
  		}
  	};
  
  	CKEDITOR.plugins.add( 'fakeobjects',
  	{
  		requires : [ 'htmlwriter' ],
  
  		afterInit : function( editor )
  		{
  			var dataProcessor = editor.dataProcessor,
  				htmlFilter = dataProcessor && dataProcessor.htmlFilter;
  
  			if ( htmlFilter )
  				htmlFilter.addRules( htmlFilterRules );
  		}
  	});
  })();
  
  CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable )
  {
  	var lang = this.lang.fakeobjects,
  		label = lang[ realElementType ] || lang.unknown;
  
  	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;
  
  	return this.document.createElement( 'img', { attributes : attributes } );
  };
  
  CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable )
  {
  	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;
  
  	return new CKEDITOR.htmlParser.element( 'img', attributes );
  };
  
  CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement )
  {
  	if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT )
  		return null;
  
  	return CKEDITOR.dom.element.createFromHtml(
  		decodeURIComponent( fakeElement.data( 'cke-realelement' ) ),
  		this.document );
  };