]>
git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blob - sources/plugins/fakeobjects/plugin.js
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 var cssStyle
= CKEDITOR
.htmlParser
.cssStyle
,
8 cssLength
= CKEDITOR
.tools
.cssLength
;
10 var cssLengthRegex
= /^((?:\d*(?:\.\d+))|(?:\d+))(.*)?$/i;
12 // Replacing the former CSS length value with the later one, with
13 // adjustment to the length unit.
14 function replaceCssLength( length1
, length2
) {
15 var parts1
= cssLengthRegex
.exec( length1
),
16 parts2
= cssLengthRegex
.exec( length2
);
18 // Omit pixel length unit when necessary,
19 // e.g. replaceCssLength( 10, '20px' ) -> 20
21 if ( !parts1
[ 2 ] && parts2
[ 2 ] == 'px' )
23 if ( parts1
[ 2 ] == 'px' && !parts2
[ 2 ] )
24 return parts2
[ 1 ] + 'px';
30 var htmlFilterRules
= {
32 $: function( element
) {
33 var attributes
= element
.attributes
,
34 realHtml
= attributes
&& attributes
[ 'data-cke-realelement' ],
35 realFragment
= realHtml
&& new CKEDITOR
.htmlParser
.fragment
.fromHtml( decodeURIComponent( realHtml
) ),
36 realElement
= realFragment
&& realFragment
.children
[ 0 ];
38 // Width/height in the fake object are subjected to clone into the real element.
39 if ( realElement
&& element
.attributes
[ 'data-cke-resizable' ] ) {
40 var styles
= new cssStyle( element
).rules
,
41 realAttrs
= realElement
.attributes
,
43 height
= styles
.height
;
45 width
&& ( realAttrs
.width
= replaceCssLength( realAttrs
.width
, width
) );
46 height
&& ( realAttrs
.height
= replaceCssLength( realAttrs
.height
, height
) );
54 CKEDITOR
.plugins
.add( 'fakeobjects', {
55 // jscs:disable maximumLineLength
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%
57 // jscs:enable maximumLineLength
59 init: function( editor
) {
60 // Allow image with all styles and classes plus src, alt and title attributes.
61 // We need them when fakeobject is pasted.
62 editor
.filter
.allow( 'img[!data-cke-realelement,src,alt,title](*){*}', 'fakeobjects' );
65 afterInit: function( editor
) {
66 var dataProcessor
= editor
.dataProcessor
,
67 htmlFilter
= dataProcessor
&& dataProcessor
.htmlFilter
;
70 htmlFilter
.addRules( htmlFilterRules
, {
78 * Creates fake {@link CKEDITOR.dom.element} based on real element.
79 * Fake element is an img with special attributes, which keep real element properties.
81 * @member CKEDITOR.editor
82 * @param {CKEDITOR.dom.element} realElement Real element to transform.
83 * @param {String} className Class name which will be used as class of fake element.
84 * @param {String} realElementType Stores type of fake element.
85 * @param {Boolean} isResizable Keeps information if element is resizable.
86 * @returns {CKEDITOR.dom.element} Fake element.
88 CKEDITOR
.editor
.prototype.createFakeElement = function( realElement
, className
, realElementType
, isResizable
) {
89 var lang
= this.lang
.fakeobjects
,
90 label
= lang
[ realElementType
] || lang
.unknown
;
94 'data-cke-realelement': encodeURIComponent( realElement
.getOuterHtml() ),
95 'data-cke-real-node-type': realElement
.type
,
98 align: realElement
.getAttribute( 'align' ) || ''
101 // Do not set "src" on high-contrast so the alt text is displayed. (http://dev.ckeditor.com/ticket/8945)
102 if ( !CKEDITOR
.env
.hc
)
103 attributes
.src
= CKEDITOR
.tools
.transparentImageData
;
105 if ( realElementType
)
106 attributes
[ 'data-cke-real-element-type' ] = realElementType
;
109 attributes
[ 'data-cke-resizable' ] = isResizable
;
111 var fakeStyle
= new cssStyle();
113 var width
= realElement
.getAttribute( 'width' ),
114 height
= realElement
.getAttribute( 'height' );
116 width
&& ( fakeStyle
.rules
.width
= cssLength( width
) );
117 height
&& ( fakeStyle
.rules
.height
= cssLength( height
) );
118 fakeStyle
.populate( attributes
);
121 return this.document
.createElement( 'img', { attributes: attributes
} );
125 * Creates fake {@link CKEDITOR.htmlParser.element} based on real element.
127 * @member CKEDITOR.editor
128 * @param {CKEDITOR.dom.element} realElement Real element to transform.
129 * @param {String} className Class name which will be used as class of fake element.
130 * @param {String} realElementType Store type of fake element.
131 * @param {Boolean} isResizable Keep information if element is resizable.
132 * @returns {CKEDITOR.htmlParser.element} Fake htmlParser element.
134 CKEDITOR
.editor
.prototype.createFakeParserElement = function( realElement
, className
, realElementType
, isResizable
) {
135 var lang
= this.lang
.fakeobjects
,
136 label
= lang
[ realElementType
] || lang
.unknown
,
139 var writer
= new CKEDITOR
.htmlParser
.basicWriter();
140 realElement
.writeHtml( writer
);
141 html
= writer
.getHtml();
145 'data-cke-realelement': encodeURIComponent( html
),
146 'data-cke-real-node-type': realElement
.type
,
149 align: realElement
.attributes
.align
|| ''
152 // Do not set "src" on high-contrast so the alt text is displayed. (http://dev.ckeditor.com/ticket/8945)
153 if ( !CKEDITOR
.env
.hc
)
154 attributes
.src
= CKEDITOR
.tools
.transparentImageData
;
156 if ( realElementType
)
157 attributes
[ 'data-cke-real-element-type' ] = realElementType
;
160 attributes
[ 'data-cke-resizable' ] = isResizable
;
161 var realAttrs
= realElement
.attributes
,
162 fakeStyle
= new cssStyle();
164 var width
= realAttrs
.width
,
165 height
= realAttrs
.height
;
167 width
!== undefined && ( fakeStyle
.rules
.width
= cssLength( width
) );
168 height
!== undefined && ( fakeStyle
.rules
.height
= cssLength( height
) );
169 fakeStyle
.populate( attributes
);
172 return new CKEDITOR
.htmlParser
.element( 'img', attributes
);
176 * Creates {@link CKEDITOR.dom.element} from fake element.
178 * @member CKEDITOR.editor
179 * @param {CKEDITOR.dom.element} fakeElement Fake element to transform.
180 * @returns {CKEDITOR.dom.element/null} Returns real element or `null` if transformed element wasn't fake.
182 CKEDITOR
.editor
.prototype.restoreRealElement = function( fakeElement
) {
183 if ( fakeElement
.data( 'cke-real-node-type' ) != CKEDITOR
.NODE_ELEMENT
)
186 var element
= CKEDITOR
.dom
.element
.createFromHtml( decodeURIComponent( fakeElement
.data( 'cke-realelement' ) ), this.document
);
188 if ( fakeElement
.data( 'cke-resizable' ) ) {
189 var width
= fakeElement
.getStyle( 'width' ),
190 height
= fakeElement
.getStyle( 'height' );
192 width
&& element
.setAttribute( 'width', replaceCssLength( element
.getAttribute( 'width' ), width
) );
193 height
&& element
.setAttribute( 'height', replaceCssLength( element
.getAttribute( 'height' ), height
) );