2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'htmlwriter' );
\r
9 * Class used to write HTML data.
\r
12 * var writer = new CKEDITOR.htmlWriter();
\r
13 * writer.openTag( 'p' );
\r
14 * writer.attribute( 'class', 'MyClass' );
\r
15 * writer.openTagClose( 'p' );
\r
16 * writer.text( 'Hello' );
\r
17 * writer.closeTag( 'p' );
\r
18 * alert( writer.getHtml() ); "<p class="MyClass">Hello</p>"
\r
20 CKEDITOR.htmlWriter = CKEDITOR.tools.createClass(
\r
22 base : CKEDITOR.htmlParser.basicWriter,
\r
26 // Call the base contructor.
\r
30 * The characters to be used for each identation step.
\r
32 * @default "\t" (tab)
\r
34 * // Use two spaces for indentation.
\r
35 * editorInstance.dataProcessor.writer.indentationChars = ' ';
\r
37 this.indentationChars = '\t';
\r
40 * The characters to be used to close "self-closing" elements, like "br" or
\r
45 * // Use HTML4 notation for self-closing elements.
\r
46 * editorInstance.dataProcessor.writer.selfClosingEnd = '>';
\r
48 this.selfClosingEnd = ' />';
\r
51 * The characters to be used for line breaks.
\r
53 * @default "\n" (LF)
\r
55 * // Use CRLF for line breaks.
\r
56 * editorInstance.dataProcessor.writer.lineBreakChars = '\r\n';
\r
58 this.lineBreakChars = '\n';
\r
60 this.forceSimpleAmpersand = false;
\r
62 this.sortAttributes = true;
\r
64 this._.indent = false;
\r
65 this._.indentation = '';
\r
68 var dtd = CKEDITOR.dtd;
\r
70 for ( var e in CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ) )
\r
75 breakBeforeOpen : true,
\r
76 breakAfterOpen : true,
\r
77 breakBeforeClose : !dtd[ e ][ '#' ],
\r
78 breakAfterClose : true
\r
81 this.setRules( 'br',
\r
83 breakAfterOpen : true
\r
85 // Disable indentation on <pre>.
\r
86 this.setRules( 'pre',
\r
95 * Writes the tag opening part for a opener tag.
\r
96 * @param {String} tagName The element name for this tag.
\r
97 * @param {Object} attributes The attributes defined for this tag. The
\r
98 * attributes could be used to inspect the tag.
\r
100 * // Writes "<p".
\r
101 * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );
\r
103 openTag : function( tagName, attributes )
\r
105 var rules = this._.rules[ tagName ];
\r
107 if ( this._.indent )
\r
108 this.indentation();
\r
109 // Do not break if indenting.
\r
110 else if ( rules && rules.breakBeforeOpen )
\r
113 this.indentation();
\r
116 this._.output.push( '<', tagName );
\r
120 * Writes the tag closing part for a opener tag.
\r
121 * @param {String} tagName The element name for this tag.
\r
122 * @param {Boolean} isSelfClose Indicates that this is a self-closing tag,
\r
123 * like "br" or "img".
\r
125 * // Writes ">".
\r
126 * writer.openTagClose( 'p', false );
\r
128 * // Writes " />".
\r
129 * writer.openTagClose( 'br', true );
\r
131 openTagClose : function( tagName, isSelfClose )
\r
133 var rules = this._.rules[ tagName ];
\r
136 this._.output.push( this.selfClosingEnd );
\r
139 this._.output.push( '>' );
\r
141 if ( rules && rules.indent )
\r
142 this._.indentation += this.indentationChars;
\r
145 if ( rules && rules.breakAfterOpen )
\r
150 * Writes an attribute. This function should be called after opening the
\r
151 * tag with {@link #openTagClose}.
\r
152 * @param {String} attName The attribute name.
\r
153 * @param {String} attValue The attribute value.
\r
155 * // Writes ' class="MyClass"'.
\r
156 * writer.attribute( 'class', 'MyClass' );
\r
158 attribute : function( attName, attValue )
\r
160 if ( this.forceSimpleAmpersand )
\r
161 attValue = attValue.replace( /&/, '&' );
\r
163 this._.output.push( ' ', attName, '="', attValue, '"' );
\r
167 * Writes a closer tag.
\r
168 * @param {String} tagName The element name for this tag.
\r
170 * // Writes "</p>".
\r
171 * writer.closeTag( 'p' );
\r
173 closeTag : function( tagName )
\r
175 var rules = this._.rules[ tagName ];
\r
177 if ( rules && rules.indent )
\r
178 this._.indentation = this._.indentation.substr( this.indentationChars.length );
\r
180 if ( this._.indent )
\r
181 this.indentation();
\r
182 // Do not break if indenting.
\r
183 else if ( rules && rules.breakBeforeClose )
\r
186 this.indentation();
\r
189 this._.output.push( '</', tagName, '>' );
\r
191 if ( rules && rules.breakAfterClose )
\r
197 * @param {String} text The text value
\r
199 * // Writes "Hello Word".
\r
200 * writer.text( 'Hello Word' );
\r
202 text : function( text )
\r
204 if ( this._.indent )
\r
206 this.indentation();
\r
207 text = CKEDITOR.tools.ltrim( text );
\r
210 this._.output.push( text );
\r
214 * Writes a comment.
\r
215 * @param {String} comment The comment text.
\r
217 * // Writes "<!-- My comment -->".
\r
218 * writer.comment( ' My comment ' );
\r
220 comment : function( comment )
\r
222 if ( this._.indent )
\r
223 this.indentation();
\r
225 this._.output.push( '<!--', comment, '-->' );
\r
229 * Writes a line break. It uses the {@link #lineBreakChars} property for it.
\r
231 * // Writes "\n" (e.g.).
\r
232 * writer.lineBreak();
\r
234 lineBreak : function()
\r
236 if ( this._.output.length > 0 )
\r
237 this._.output.push( this.lineBreakChars );
\r
238 this._.indent = true;
\r
242 * Writes the current indentation chars. It uses the
\r
243 * {@link #indentationChars} property, repeating it for the current
\r
244 * indentation steps.
\r
246 * // Writes "\t" (e.g.).
\r
247 * writer.indentation();
\r
249 indentation : function()
\r
251 this._.output.push( this._.indentation );
\r
252 this._.indent = false;
\r
256 * Sets formatting rules for a give element. The possible rules are:
\r
258 * <li><b>indent</b>: indent the element contents.</li>
\r
259 * <li><b>breakBeforeOpen</b>: break line before the opener tag for this element.</li>
\r
260 * <li><b>breakAfterOpen</b>: break line after the opener tag for this element.</li>
\r
261 * <li><b>breakBeforeClose</b>: break line before the closer tag for this element.</li>
\r
262 * <li><b>breakAfterClose</b>: break line after the closer tag for this element.</li>
\r
265 * All rules default to "false".
\r
267 * By default, all elements available in the {@link CKEDITOR.dtd.$block),
\r
268 * {@link CKEDITOR.dtd.$listItem} and {@link CKEDITOR.dtd.$tableContent}
\r
269 * lists have all the above rules set to "true". Additionaly, the "br"
\r
270 * element has the "breakAfterOpen" set to "true".
\r
271 * @param {String} tagName The element name to which set the rules.
\r
272 * @param {Object} rules An object containing the element rules.
\r
274 * // Break line before and after "img" tags.
\r
275 * writer.setRules( 'img',
\r
277 * breakBeforeOpen : true
\r
278 * breakAfterOpen : true
\r
281 * // Reset the rules for the "h1" tag.
\r
282 * writer.setRules( 'h1', {} );
\r
284 setRules : function( tagName, rules )
\r
286 this._.rules[ tagName ] = rules;
\r