]>
git.immae.eu Git - perso/Immae/Projets/packagist/piedsjaloux-ckeditor-component.git/blob - sources/plugins/htmlwriter/plugin.js
61d6ac64300bd8afffa6eb72135e2976f87a0534
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
6 CKEDITOR
.plugins
.add( 'htmlwriter', {
7 init: function( editor
) {
8 var writer
= new CKEDITOR
.htmlWriter();
10 writer
.forceSimpleAmpersand
= editor
.config
.forceSimpleAmpersand
;
11 writer
.indentationChars
= editor
.config
.dataIndentationChars
|| '\t';
13 // Overwrite default basicWriter initialized in hmtlDataProcessor constructor.
14 editor
.dataProcessor
.writer
= writer
;
19 * The class used to write HTML data.
21 * var writer = new CKEDITOR.htmlWriter();
22 * writer.openTag( 'p' );
23 * writer.attribute( 'class', 'MyClass' );
24 * writer.openTagClose( 'p' );
25 * writer.text( 'Hello' );
26 * writer.closeTag( 'p' );
27 * alert( writer.getHtml() ); // '<p class="MyClass">Hello</p>'
30 * @extends CKEDITOR.htmlParser.basicWriter
32 CKEDITOR
.htmlWriter
= CKEDITOR
.tools
.createClass( {
33 base: CKEDITOR
.htmlParser
.basicWriter
,
36 * Creates an `htmlWriter` class instance.
41 // Call the base contructor.
45 * The characters to be used for each indentation step.
47 * // Use tab for indentation.
48 * editorInstance.dataProcessor.writer.indentationChars = '\t';
50 this.indentationChars
= '\t';
53 * The characters to be used to close "self-closing" elements, like `<br>` or `<img>`.
55 * // Use HTML4 notation for self-closing elements.
56 * editorInstance.dataProcessor.writer.selfClosingEnd = '>';
58 this.selfClosingEnd
= ' />';
61 * The characters to be used for line breaks.
63 * // Use CRLF for line breaks.
64 * editorInstance.dataProcessor.writer.lineBreakChars = '\r\n';
66 this.lineBreakChars
= '\n';
68 this.sortAttributes
= 1;
71 this._
.indentation
= '';
72 // Indicate preformatted block context status. (http://dev.ckeditor.com/ticket/5789)
76 var dtd
= CKEDITOR
.dtd
;
78 for ( var e
in CKEDITOR
.tools
.extend( {}, dtd
.$nonBodyContent
, dtd
.$block
, dtd
.$listItem
, dtd
.$tableContent
) ) {
80 indent: !dtd
[ e
][ '#' ],
82 breakBeforeClose: !dtd
[ e
][ '#' ],
84 needsSpace: ( e
in dtd
.$block
) && !( e
in { li: 1, dt: 1, dd: 1 } )
88 this.setRules( 'br', { breakAfterOpen: 1 } );
90 this.setRules( 'title', {
95 this.setRules( 'style', {
100 this.setRules( 'pre', {
101 breakAfterOpen: 1, // Keep line break after the opening tag
102 indent: 0 // Disable indentation on <pre>.
108 * Writes the tag opening part for an opener tag.
111 * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );
113 * @param {String} tagName The element name for this tag.
114 * @param {Object} attributes The attributes defined for this tag. The
115 * attributes could be used to inspect the tag.
117 openTag: function( tagName
) {
118 var rules
= this._
.rules
[ tagName
];
120 if ( this._
.afterCloser
&& rules
&& rules
.needsSpace
&& this._
.needsSpace
)
121 this._
.output
.push( '\n' );
125 // Do not break if indenting.
126 else if ( rules
&& rules
.breakBeforeOpen
) {
131 this._
.output
.push( '<', tagName
);
133 this._
.afterCloser
= 0;
137 * Writes the tag closing part for an opener tag.
140 * writer.openTagClose( 'p', false );
143 * writer.openTagClose( 'br', true );
145 * @param {String} tagName The element name for this tag.
146 * @param {Boolean} isSelfClose Indicates that this is a self-closing tag,
147 * like `<br>` or `<img>`.
149 openTagClose: function( tagName
, isSelfClose
) {
150 var rules
= this._
.rules
[ tagName
];
153 this._
.output
.push( this.selfClosingEnd
);
155 if ( rules
&& rules
.breakAfterClose
)
156 this._
.needsSpace
= rules
.needsSpace
;
158 this._
.output
.push( '>' );
160 if ( rules
&& rules
.indent
)
161 this._
.indentation
+= this.indentationChars
;
164 if ( rules
&& rules
.breakAfterOpen
)
166 tagName
== 'pre' && ( this._
.inPre
= 1 );
170 * Writes an attribute. This function should be called after opening the
171 * tag with {@link #openTagClose}.
173 * // Writes ' class="MyClass"'.
174 * writer.attribute( 'class', 'MyClass' );
176 * @param {String} attName The attribute name.
177 * @param {String} attValue The attribute value.
179 attribute: function( attName
, attValue
) {
181 if ( typeof attValue
== 'string' ) {
182 this.forceSimpleAmpersand
&& ( attValue
= attValue
.replace( /&/g, '&' ) );
183 // Browsers don't always escape special character in attribute values. (http://dev.ckeditor.com/ticket/4683, http://dev.ckeditor.com/ticket/4719).
184 attValue
= CKEDITOR
.tools
.htmlEncodeAttr( attValue
);
187 this._
.output
.push( ' ', attName
, '="', attValue
, '"' );
191 * Writes a closer tag.
194 * writer.closeTag( 'p' );
196 * @param {String} tagName The element name for this tag.
198 closeTag: function( tagName
) {
199 var rules
= this._
.rules
[ tagName
];
201 if ( rules
&& rules
.indent
)
202 this._
.indentation
= this._
.indentation
.substr( this.indentationChars
.length
);
206 // Do not break if indenting.
207 else if ( rules
&& rules
.breakBeforeClose
) {
212 this._
.output
.push( '</', tagName
, '>' );
213 tagName
== 'pre' && ( this._
.inPre
= 0 );
215 if ( rules
&& rules
.breakAfterClose
) {
217 this._
.needsSpace
= rules
.needsSpace
;
220 this._
.afterCloser
= 1;
226 * // Writes 'Hello Word'.
227 * writer.text( 'Hello Word' );
229 * @param {String} text The text value
231 text: function( text
) {
232 if ( this._
.indent
) {
234 !this._
.inPre
&& ( text
= CKEDITOR
.tools
.ltrim( text
) );
237 this._
.output
.push( text
);
243 * // Writes "<!-- My comment -->".
244 * writer.comment( ' My comment ' );
246 * @param {String} comment The comment text.
248 comment: function( comment
) {
252 this._
.output
.push( '<!--', comment
, '-->' );
256 * Writes a line break. It uses the {@link #lineBreakChars} property for it.
258 * // Writes '\n' (e.g.).
259 * writer.lineBreak();
261 lineBreak: function() {
262 if ( !this._
.inPre
&& this._
.output
.length
> 0 )
263 this._
.output
.push( this.lineBreakChars
);
268 * Writes the current indentation character. It uses the {@link #indentationChars}
269 * property, repeating it for the current indentation steps.
271 * // Writes '\t' (e.g.).
272 * writer.indentation();
274 indentation: function() {
275 if ( !this._
.inPre
&& this._
.indentation
)
276 this._
.output
.push( this._
.indentation
);
281 * Empties the current output buffer. It also brings back the default
282 * values of the writer flags.
289 this._
.indentation
= '';
290 this._
.afterCloser
= 0;
292 this._
.needsSpace
= 0;
296 * Sets formatting rules for a given element. Possible rules are:
298 * * `indent` – indent the element content.
299 * * `breakBeforeOpen` – break line before the opener tag for this element.
300 * * `breakAfterOpen` – break line after the opener tag for this element.
301 * * `breakBeforeClose` – break line before the closer tag for this element.
302 * * `breakAfterClose` – break line after the closer tag for this element.
304 * All rules default to `false`. Each function call overrides rules that are
305 * already present, leaving the undefined ones untouched.
307 * By default, all elements available in the {@link CKEDITOR.dtd#$block},
308 * {@link CKEDITOR.dtd#$listItem}, and {@link CKEDITOR.dtd#$tableContent}
309 * lists have all the above rules set to `true`. Additionaly, the `<br>`
310 * element has the `breakAfterOpen` rule set to `true`.
312 * // Break line before and after "img" tags.
313 * writer.setRules( 'img', {
314 * breakBeforeOpen: true
315 * breakAfterOpen: true
318 * // Reset the rules for the "h1" tag.
319 * writer.setRules( 'h1', {} );
321 * @param {String} tagName The name of the element for which the rules are set.
322 * @param {Object} rules An object containing the element rules.
324 setRules: function( tagName
, rules
) {
325 var currentRules
= this._
.rules
[ tagName
];
328 CKEDITOR
.tools
.extend( currentRules
, rules
, true );
330 this._
.rules
[ tagName
] = rules
;
336 * Whether to force using `'&'` instead of `'&'` in element attributes
337 * values. It is not recommended to change this setting for compliance with the
338 * W3C XHTML 1.0 standards ([C.12, XHTML 1.0](http://www.w3.org/TR/xhtml1/#C_12)).
340 * // Use `'&'` instead of `'&'`
341 * CKEDITOR.config.forceSimpleAmpersand = true;
343 * @cfg {Boolean} [forceSimpleAmpersand=false]
344 * @member CKEDITOR.config
348 * The characters to be used for indenting HTML output produced by the editor.
349 * Using characters different from `' '` (space) and `'\t'` (tab) is not recommended
350 * as it will mess the code.
353 * CKEDITOR.config.dataIndentationChars = '';
355 * // Use two spaces for indentation.
356 * CKEDITOR.config.dataIndentationChars = ' ';
358 * @cfg {String} [dataIndentationChars='\t']
359 * @member CKEDITOR.config