]>
Commit | Line | Data |
---|---|---|
1 | /**\r | |
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.\r | |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license\r | |
4 | */\r | |
5 | \r | |
6 | ( function() {\r | |
7 | var cssStyle = CKEDITOR.htmlParser.cssStyle,\r | |
8 | cssLength = CKEDITOR.tools.cssLength;\r | |
9 | \r | |
10 | var cssLengthRegex = /^((?:\d*(?:\.\d+))|(?:\d+))(.*)?$/i;\r | |
11 | \r | |
12 | // Replacing the former CSS length value with the later one, with\r | |
13 | // adjustment to the length unit.\r | |
14 | function replaceCssLength( length1, length2 ) {\r | |
15 | var parts1 = cssLengthRegex.exec( length1 ),\r | |
16 | parts2 = cssLengthRegex.exec( length2 );\r | |
17 | \r | |
18 | // Omit pixel length unit when necessary,\r | |
19 | // e.g. replaceCssLength( 10, '20px' ) -> 20\r | |
20 | if ( parts1 ) {\r | |
21 | if ( !parts1[ 2 ] && parts2[ 2 ] == 'px' )\r | |
22 | return parts2[ 1 ];\r | |
23 | if ( parts1[ 2 ] == 'px' && !parts2[ 2 ] )\r | |
24 | return parts2[ 1 ] + 'px';\r | |
25 | }\r | |
26 | \r | |
27 | return length2;\r | |
28 | }\r | |
29 | \r | |
30 | var htmlFilterRules = {\r | |
31 | elements: {\r | |
32 | $: function( element ) {\r | |
33 | var attributes = element.attributes,\r | |
34 | realHtml = attributes && attributes[ 'data-cke-realelement' ],\r | |
35 | realFragment = realHtml && new CKEDITOR.htmlParser.fragment.fromHtml( decodeURIComponent( realHtml ) ),\r | |
36 | realElement = realFragment && realFragment.children[ 0 ];\r | |
37 | \r | |
38 | // Width/height in the fake object are subjected to clone into the real element.\r | |
39 | if ( realElement && element.attributes[ 'data-cke-resizable' ] ) {\r | |
40 | var styles = new cssStyle( element ).rules,\r | |
41 | realAttrs = realElement.attributes,\r | |
42 | width = styles.width,\r | |
43 | height = styles.height;\r | |
44 | \r | |
45 | width && ( realAttrs.width = replaceCssLength( realAttrs.width, width ) );\r | |
46 | height && ( realAttrs.height = replaceCssLength( realAttrs.height, height ) );\r | |
47 | }\r | |
48 | \r | |
49 | return realElement;\r | |
50 | }\r | |
51 | }\r | |
52 | };\r | |
53 | \r | |
54 | CKEDITOR.plugins.add( 'fakeobjects', {\r | |
55 | // jscs:disable maximumLineLength\r | |
56 | lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r | |
57 | // jscs:enable maximumLineLength\r | |
58 | \r | |
59 | init: function( editor ) {\r | |
60 | // Allow image with all styles and classes plus src, alt and title attributes.\r | |
61 | // We need them when fakeobject is pasted.\r | |
62 | editor.filter.allow( 'img[!data-cke-realelement,src,alt,title](*){*}', 'fakeobjects' );\r | |
63 | },\r | |
64 | \r | |
65 | afterInit: function( editor ) {\r | |
66 | var dataProcessor = editor.dataProcessor,\r | |
67 | htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r | |
68 | \r | |
69 | if ( htmlFilter ) {\r | |
70 | htmlFilter.addRules( htmlFilterRules, {\r | |
71 | applyToAll: true\r | |
72 | } );\r | |
73 | }\r | |
74 | }\r | |
75 | } );\r | |
76 | \r | |
77 | /**\r | |
78 | * @member CKEDITOR.editor\r | |
79 | * @todo\r | |
80 | */\r | |
81 | CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable ) {\r | |
82 | var lang = this.lang.fakeobjects,\r | |
83 | label = lang[ realElementType ] || lang.unknown;\r | |
84 | \r | |
85 | var attributes = {\r | |
86 | 'class': className,\r | |
87 | 'data-cke-realelement': encodeURIComponent( realElement.getOuterHtml() ),\r | |
88 | 'data-cke-real-node-type': realElement.type,\r | |
89 | alt: label,\r | |
90 | title: label,\r | |
91 | align: realElement.getAttribute( 'align' ) || ''\r | |
92 | };\r | |
93 | \r | |
94 | // Do not set "src" on high-contrast so the alt text is displayed. (#8945)\r | |
95 | if ( !CKEDITOR.env.hc )\r | |
96 | attributes.src = CKEDITOR.tools.transparentImageData;\r | |
97 | \r | |
98 | if ( realElementType )\r | |
99 | attributes[ 'data-cke-real-element-type' ] = realElementType;\r | |
100 | \r | |
101 | if ( isResizable ) {\r | |
102 | attributes[ 'data-cke-resizable' ] = isResizable;\r | |
103 | \r | |
104 | var fakeStyle = new cssStyle();\r | |
105 | \r | |
106 | var width = realElement.getAttribute( 'width' ),\r | |
107 | height = realElement.getAttribute( 'height' );\r | |
108 | \r | |
109 | width && ( fakeStyle.rules.width = cssLength( width ) );\r | |
110 | height && ( fakeStyle.rules.height = cssLength( height ) );\r | |
111 | fakeStyle.populate( attributes );\r | |
112 | }\r | |
113 | \r | |
114 | return this.document.createElement( 'img', { attributes: attributes } );\r | |
115 | };\r | |
116 | \r | |
117 | /**\r | |
118 | * @member CKEDITOR.editor\r | |
119 | * @todo\r | |
120 | */\r | |
121 | CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) {\r | |
122 | var lang = this.lang.fakeobjects,\r | |
123 | label = lang[ realElementType ] || lang.unknown,\r | |
124 | html;\r | |
125 | \r | |
126 | var writer = new CKEDITOR.htmlParser.basicWriter();\r | |
127 | realElement.writeHtml( writer );\r | |
128 | html = writer.getHtml();\r | |
129 | \r | |
130 | var attributes = {\r | |
131 | 'class': className,\r | |
132 | 'data-cke-realelement': encodeURIComponent( html ),\r | |
133 | 'data-cke-real-node-type': realElement.type,\r | |
134 | alt: label,\r | |
135 | title: label,\r | |
136 | align: realElement.attributes.align || ''\r | |
137 | };\r | |
138 | \r | |
139 | // Do not set "src" on high-contrast so the alt text is displayed. (#8945)\r | |
140 | if ( !CKEDITOR.env.hc )\r | |
141 | attributes.src = CKEDITOR.tools.transparentImageData;\r | |
142 | \r | |
143 | if ( realElementType )\r | |
144 | attributes[ 'data-cke-real-element-type' ] = realElementType;\r | |
145 | \r | |
146 | if ( isResizable ) {\r | |
147 | attributes[ 'data-cke-resizable' ] = isResizable;\r | |
148 | var realAttrs = realElement.attributes,\r | |
149 | fakeStyle = new cssStyle();\r | |
150 | \r | |
151 | var width = realAttrs.width,\r | |
152 | height = realAttrs.height;\r | |
153 | \r | |
154 | width !== undefined && ( fakeStyle.rules.width = cssLength( width ) );\r | |
155 | height !== undefined && ( fakeStyle.rules.height = cssLength( height ) );\r | |
156 | fakeStyle.populate( attributes );\r | |
157 | }\r | |
158 | \r | |
159 | return new CKEDITOR.htmlParser.element( 'img', attributes );\r | |
160 | };\r | |
161 | \r | |
162 | /**\r | |
163 | * @member CKEDITOR.editor\r | |
164 | * @todo\r | |
165 | */\r | |
166 | CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement ) {\r | |
167 | if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT )\r | |
168 | return null;\r | |
169 | \r | |
170 | var element = CKEDITOR.dom.element.createFromHtml( decodeURIComponent( fakeElement.data( 'cke-realelement' ) ), this.document );\r | |
171 | \r | |
172 | if ( fakeElement.data( 'cke-resizable' ) ) {\r | |
173 | var width = fakeElement.getStyle( 'width' ),\r | |
174 | height = fakeElement.getStyle( 'height' );\r | |
175 | \r | |
176 | width && element.setAttribute( 'width', replaceCssLength( element.getAttribute( 'width' ), width ) );\r | |
177 | height && element.setAttribute( 'height', replaceCssLength( element.getAttribute( 'height' ), height ) );\r | |
178 | }\r | |
179 | \r | |
180 | return element;\r | |
181 | };\r | |
182 | \r | |
183 | } )();\r |