]>
Commit | Line | Data |
---|---|---|
c63493c8 IB |
1 | /**\r |
2 | * @license Copyright (c) 2003-2017, 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 | |
1794320d | 56 | lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,es-mx,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,oc,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 |
c63493c8 IB |
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 | |
1794320d IB |
78 | * Creates fake {@link CKEDITOR.dom.element} based on real element.\r |
79 | * Fake element is an img with special attributes, which keep real element properties.\r | |
80 | *\r | |
c63493c8 | 81 | * @member CKEDITOR.editor\r |
1794320d IB |
82 | * @param {CKEDITOR.dom.element} realElement Real element to transform.\r |
83 | * @param {String} className Class name which will be used as class of fake element.\r | |
84 | * @param {String} realElementType Stores type of fake element.\r | |
85 | * @param {Boolean} isResizable Keeps information if element is resizable.\r | |
86 | * @returns {CKEDITOR.dom.element} Fake element.\r | |
c63493c8 IB |
87 | */\r |
88 | CKEDITOR.editor.prototype.createFakeElement = function( realElement, className, realElementType, isResizable ) {\r | |
89 | var lang = this.lang.fakeobjects,\r | |
90 | label = lang[ realElementType ] || lang.unknown;\r | |
91 | \r | |
92 | var attributes = {\r | |
93 | 'class': className,\r | |
94 | 'data-cke-realelement': encodeURIComponent( realElement.getOuterHtml() ),\r | |
95 | 'data-cke-real-node-type': realElement.type,\r | |
96 | alt: label,\r | |
97 | title: label,\r | |
98 | align: realElement.getAttribute( 'align' ) || ''\r | |
99 | };\r | |
100 | \r | |
1794320d | 101 | // Do not set "src" on high-contrast so the alt text is displayed. (http://dev.ckeditor.com/ticket/8945)\r |
c63493c8 IB |
102 | if ( !CKEDITOR.env.hc )\r |
103 | attributes.src = CKEDITOR.tools.transparentImageData;\r | |
104 | \r | |
105 | if ( realElementType )\r | |
106 | attributes[ 'data-cke-real-element-type' ] = realElementType;\r | |
107 | \r | |
108 | if ( isResizable ) {\r | |
109 | attributes[ 'data-cke-resizable' ] = isResizable;\r | |
110 | \r | |
111 | var fakeStyle = new cssStyle();\r | |
112 | \r | |
113 | var width = realElement.getAttribute( 'width' ),\r | |
114 | height = realElement.getAttribute( 'height' );\r | |
115 | \r | |
116 | width && ( fakeStyle.rules.width = cssLength( width ) );\r | |
117 | height && ( fakeStyle.rules.height = cssLength( height ) );\r | |
118 | fakeStyle.populate( attributes );\r | |
119 | }\r | |
120 | \r | |
121 | return this.document.createElement( 'img', { attributes: attributes } );\r | |
122 | };\r | |
123 | \r | |
124 | /**\r | |
1794320d IB |
125 | * Creates fake {@link CKEDITOR.htmlParser.element} based on real element.\r |
126 | *\r | |
c63493c8 | 127 | * @member CKEDITOR.editor\r |
1794320d IB |
128 | * @param {CKEDITOR.dom.element} realElement Real element to transform.\r |
129 | * @param {String} className Class name which will be used as class of fake element.\r | |
130 | * @param {String} realElementType Store type of fake element.\r | |
131 | * @param {Boolean} isResizable Keep information if element is resizable.\r | |
132 | * @returns {CKEDITOR.htmlParser.element} Fake htmlParser element.\r | |
c63493c8 IB |
133 | */\r |
134 | CKEDITOR.editor.prototype.createFakeParserElement = function( realElement, className, realElementType, isResizable ) {\r | |
135 | var lang = this.lang.fakeobjects,\r | |
136 | label = lang[ realElementType ] || lang.unknown,\r | |
137 | html;\r | |
138 | \r | |
139 | var writer = new CKEDITOR.htmlParser.basicWriter();\r | |
140 | realElement.writeHtml( writer );\r | |
141 | html = writer.getHtml();\r | |
142 | \r | |
143 | var attributes = {\r | |
144 | 'class': className,\r | |
145 | 'data-cke-realelement': encodeURIComponent( html ),\r | |
146 | 'data-cke-real-node-type': realElement.type,\r | |
147 | alt: label,\r | |
148 | title: label,\r | |
149 | align: realElement.attributes.align || ''\r | |
150 | };\r | |
151 | \r | |
1794320d | 152 | // Do not set "src" on high-contrast so the alt text is displayed. (http://dev.ckeditor.com/ticket/8945)\r |
c63493c8 IB |
153 | if ( !CKEDITOR.env.hc )\r |
154 | attributes.src = CKEDITOR.tools.transparentImageData;\r | |
155 | \r | |
156 | if ( realElementType )\r | |
157 | attributes[ 'data-cke-real-element-type' ] = realElementType;\r | |
158 | \r | |
159 | if ( isResizable ) {\r | |
160 | attributes[ 'data-cke-resizable' ] = isResizable;\r | |
161 | var realAttrs = realElement.attributes,\r | |
162 | fakeStyle = new cssStyle();\r | |
163 | \r | |
164 | var width = realAttrs.width,\r | |
165 | height = realAttrs.height;\r | |
166 | \r | |
167 | width !== undefined && ( fakeStyle.rules.width = cssLength( width ) );\r | |
168 | height !== undefined && ( fakeStyle.rules.height = cssLength( height ) );\r | |
169 | fakeStyle.populate( attributes );\r | |
170 | }\r | |
171 | \r | |
172 | return new CKEDITOR.htmlParser.element( 'img', attributes );\r | |
173 | };\r | |
174 | \r | |
175 | /**\r | |
1794320d IB |
176 | * Creates {@link CKEDITOR.dom.element} from fake element.\r |
177 | *\r | |
c63493c8 | 178 | * @member CKEDITOR.editor\r |
1794320d IB |
179 | * @param {CKEDITOR.dom.element} fakeElement Fake element to transform.\r |
180 | * @returns {CKEDITOR.dom.element/null} Returns real element or `null` if transformed element wasn't fake.\r | |
c63493c8 IB |
181 | */\r |
182 | CKEDITOR.editor.prototype.restoreRealElement = function( fakeElement ) {\r | |
183 | if ( fakeElement.data( 'cke-real-node-type' ) != CKEDITOR.NODE_ELEMENT )\r | |
184 | return null;\r | |
185 | \r | |
186 | var element = CKEDITOR.dom.element.createFromHtml( decodeURIComponent( fakeElement.data( 'cke-realelement' ) ), this.document );\r | |
187 | \r | |
188 | if ( fakeElement.data( 'cke-resizable' ) ) {\r | |
189 | var width = fakeElement.getStyle( 'width' ),\r | |
190 | height = fakeElement.getStyle( 'height' );\r | |
191 | \r | |
192 | width && element.setAttribute( 'width', replaceCssLength( element.getAttribute( 'width' ), width ) );\r | |
193 | height && element.setAttribute( 'height', replaceCssLength( element.getAttribute( 'height' ), height ) );\r | |
194 | }\r | |
195 | \r | |
196 | return element;\r | |
197 | };\r | |
198 | \r | |
199 | } )();\r |