2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 CKEDITOR
.plugins
.add( 'htmlwriter' );
9 * Class used to write HTML data.
12 * var writer = new CKEDITOR.htmlWriter();
13 * writer.openTag( 'p' );
14 * writer.attribute( 'class', 'MyClass' );
15 * writer.openTagClose( 'p' );
16 * writer.text( 'Hello' );
17 * writer.closeTag( 'p' );
18 * alert( writer.getHtml() ); "<p class="MyClass">Hello</p>"
20 CKEDITOR
.htmlWriter
= CKEDITOR
.tools
.createClass(
22 base
: CKEDITOR
.htmlParser
.basicWriter
,
26 // Call the base contructor.
30 * The characters to be used for each identation step.
34 * // Use two spaces for indentation.
35 * editorInstance.dataProcessor.writer.indentationChars = ' ';
37 this.indentationChars
= '\t';
40 * The characters to be used to close "self-closing" elements, like "br" or
45 * // Use HTML4 notation for self-closing elements.
46 * editorInstance.dataProcessor.writer.selfClosingEnd = '>';
48 this.selfClosingEnd
= ' />';
51 * The characters to be used for line breaks.
55 * // Use CRLF for line breaks.
56 * editorInstance.dataProcessor.writer.lineBreakChars = '\r\n';
58 this.lineBreakChars
= '\n';
60 this.forceSimpleAmpersand
= false;
62 this.sortAttributes
= true;
64 this._
.indent
= false;
65 this._
.indentation
= '';
68 var dtd
= CKEDITOR
.dtd
;
70 for ( var e
in CKEDITOR
.tools
.extend( {}, dtd
.$nonBodyContent
, dtd
.$block
, dtd
.$listItem
, dtd
.$tableContent
) )
75 breakBeforeOpen
: true,
76 breakAfterOpen
: true,
77 breakBeforeClose
: !dtd
[ e
][ '#' ],
78 breakAfterClose
: true
87 this.setRules( 'title',
90 breakAfterOpen
: false
93 this.setRules( 'style',
96 breakBeforeClose
: true
99 // Disable indentation on <pre>.
100 this.setRules( 'pre',
109 * Writes the tag opening part for a opener tag.
110 * @param {String} tagName The element name for this tag.
111 * @param {Object} attributes The attributes defined for this tag. The
112 * attributes could be used to inspect the tag.
115 * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );
117 openTag : function( tagName
, attributes
)
119 var rules
= this._
.rules
[ tagName
];
123 // Do not break if indenting.
124 else if ( rules
&& rules
.breakBeforeOpen
)
130 this._
.output
.push( '<', tagName
);
134 * Writes the tag closing part for a opener tag.
135 * @param {String} tagName The element name for this tag.
136 * @param {Boolean} isSelfClose Indicates that this is a self-closing tag,
137 * like "br" or "img".
140 * writer.openTagClose( 'p', false );
142 * // Writes " />".
143 * writer.openTagClose( 'br', true );
145 openTagClose : function( tagName
, isSelfClose
)
147 var rules
= this._
.rules
[ tagName
];
150 this._
.output
.push( this.selfClosingEnd
);
153 this._
.output
.push( '>' );
155 if ( rules
&& rules
.indent
)
156 this._
.indentation
+= this.indentationChars
;
159 if ( rules
&& rules
.breakAfterOpen
)
164 * Writes an attribute. This function should be called after opening the
165 * tag with {@link #openTagClose}.
166 * @param {String} attName The attribute name.
167 * @param {String} attValue The attribute value.
169 * // Writes ' class="MyClass"'.
170 * writer.attribute( 'class', 'MyClass' );
172 attribute : function( attName
, attValue
)
175 if ( typeof attValue
== 'string' )
177 this.forceSimpleAmpersand
&& ( attValue
= attValue
.replace( /&/g, '&' ) );
178 // Browsers don't always escape special character in attribute values. (#4683, #4719).
179 attValue
= CKEDITOR
.tools
.htmlEncodeAttr( attValue
);
182 this._
.output
.push( ' ', attName
, '="', attValue
, '"' );
186 * Writes a closer tag.
187 * @param {String} tagName The element name for this tag.
189 * // Writes "</p>".
190 * writer.closeTag( 'p' );
192 closeTag : function( tagName
)
194 var rules
= this._
.rules
[ tagName
];
196 if ( rules
&& rules
.indent
)
197 this._
.indentation
= this._
.indentation
.substr( this.indentationChars
.length
);
201 // Do not break if indenting.
202 else if ( rules
&& rules
.breakBeforeClose
)
208 this._
.output
.push( '</', tagName
, '>' );
210 if ( rules
&& rules
.breakAfterClose
)
216 * @param {String} text The text value
218 * // Writes "Hello Word".
219 * writer.text( 'Hello Word' );
221 text : function( text
)
226 text
= CKEDITOR
.tools
.ltrim( text
);
229 this._
.output
.push( text
);
234 * @param {String} comment The comment text.
236 * // Writes "<!-- My comment -->".
237 * writer.comment( ' My comment ' );
239 comment : function( comment
)
244 this._
.output
.push( '<!--', comment
, '-->' );
248 * Writes a line break. It uses the {@link #lineBreakChars} property for it.
250 * // Writes "\n" (e.g.).
251 * writer.lineBreak();
253 lineBreak : function()
255 if ( this._
.output
.length
> 0 )
256 this._
.output
.push( this.lineBreakChars
);
257 this._
.indent
= true;
261 * Writes the current indentation chars. It uses the
262 * {@link #indentationChars} property, repeating it for the current
265 * // Writes "\t" (e.g.).
266 * writer.indentation();
268 indentation : function()
270 this._
.output
.push( this._
.indentation
);
271 this._
.indent
= false;
275 * Sets formatting rules for a give element. The possible rules are:
277 * <li><b>indent</b>: indent the element contents.</li>
278 * <li><b>breakBeforeOpen</b>: break line before the opener tag for this element.</li>
279 * <li><b>breakAfterOpen</b>: break line after the opener tag for this element.</li>
280 * <li><b>breakBeforeClose</b>: break line before the closer tag for this element.</li>
281 * <li><b>breakAfterClose</b>: break line after the closer tag for this element.</li>
284 * All rules default to "false". Each call to the function overrides
285 * already present rules, leaving the undefined untouched.
287 * By default, all elements available in the {@link CKEDITOR.dtd.$block),
288 * {@link CKEDITOR.dtd.$listItem} and {@link CKEDITOR.dtd.$tableContent}
289 * lists have all the above rules set to "true". Additionaly, the "br"
290 * element has the "breakAfterOpen" set to "true".
291 * @param {String} tagName The element name to which set the rules.
292 * @param {Object} rules An object containing the element rules.
294 * // Break line before and after "img" tags.
295 * writer.setRules( 'img',
297 * breakBeforeOpen : true
298 * breakAfterOpen : true
301 * // Reset the rules for the "h1" tag.
302 * writer.setRules( 'h1', {} );
304 setRules : function( tagName
, rules
)
306 var currentRules
= this._
.rules
[ tagName
];
309 CKEDITOR
.tools
.extend( currentRules
, rules
, true );
311 this._
.rules
[ tagName
] = rules
;