2 Copyright (c) 2003-2011, 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
= 0;
62 this.sortAttributes
= 1;
65 this._
.indentation
= '';
66 // Indicate preformatted block context status. (#5789)
70 var dtd
= CKEDITOR
.dtd
;
72 for ( var e
in CKEDITOR
.tools
.extend( {}, dtd
.$nonBodyContent
, dtd
.$block
, dtd
.$listItem
, dtd
.$tableContent
) )
79 breakBeforeClose
: !dtd
[ e
][ '#' ],
89 this.setRules( 'title',
95 this.setRules( 'style',
101 // Disable indentation on <pre>.
102 this.setRules( 'pre',
111 * Writes the tag opening part for a opener tag.
112 * @param {String} tagName The element name for this tag.
113 * @param {Object} attributes The attributes defined for this tag. The
114 * attributes could be used to inspect the tag.
117 * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );
119 openTag : function( tagName
, attributes
)
121 var rules
= this._
.rules
[ tagName
];
125 // Do not break if indenting.
126 else if ( rules
&& rules
.breakBeforeOpen
)
132 this._
.output
.push( '<', tagName
);
136 * Writes the tag closing part for a opener tag.
137 * @param {String} tagName The element name for this tag.
138 * @param {Boolean} isSelfClose Indicates that this is a self-closing tag,
139 * like "br" or "img".
142 * writer.openTagClose( 'p', false );
144 * // Writes " />".
145 * writer.openTagClose( 'br', true );
147 openTagClose : function( tagName
, isSelfClose
)
149 var rules
= this._
.rules
[ tagName
];
152 this._
.output
.push( this.selfClosingEnd
);
155 this._
.output
.push( '>' );
157 if ( rules
&& rules
.indent
)
158 this._
.indentation
+= this.indentationChars
;
161 if ( rules
&& rules
.breakAfterOpen
)
163 tagName
== 'pre' && ( this._
.inPre
= 1 );
167 * Writes an attribute. This function should be called after opening the
168 * tag with {@link #openTagClose}.
169 * @param {String} attName The attribute name.
170 * @param {String} attValue The attribute value.
172 * // Writes ' class="MyClass"'.
173 * writer.attribute( 'class', 'MyClass' );
175 attribute : function( attName
, attValue
)
178 if ( typeof attValue
== 'string' )
180 this.forceSimpleAmpersand
&& ( attValue
= attValue
.replace( /&/g, '&' ) );
181 // Browsers don't always escape special character in attribute values. (#4683, #4719).
182 attValue
= CKEDITOR
.tools
.htmlEncodeAttr( attValue
);
185 this._
.output
.push( ' ', attName
, '="', attValue
, '"' );
189 * Writes a closer tag.
190 * @param {String} tagName The element name for this tag.
192 * // Writes "</p>".
193 * writer.closeTag( 'p' );
195 closeTag : function( tagName
)
197 var rules
= this._
.rules
[ tagName
];
199 if ( rules
&& rules
.indent
)
200 this._
.indentation
= this._
.indentation
.substr( this.indentationChars
.length
);
204 // Do not break if indenting.
205 else if ( rules
&& rules
.breakBeforeClose
)
211 this._
.output
.push( '</', tagName
, '>' );
212 tagName
== 'pre' && ( this._
.inPre
= 0 );
214 if ( rules
&& rules
.breakAfterClose
)
220 * @param {String} text The text value
222 * // Writes "Hello Word".
223 * writer.text( 'Hello Word' );
225 text : function( text
)
230 !this._
.inPre
&& ( text
= CKEDITOR
.tools
.ltrim( text
) );
233 this._
.output
.push( text
);
238 * @param {String} comment The comment text.
240 * // Writes "<!-- My comment -->".
241 * writer.comment( ' My comment ' );
243 comment : function( comment
)
248 this._
.output
.push( '<!--', comment
, '-->' );
252 * Writes a line break. It uses the {@link #lineBreakChars} property for it.
254 * // Writes "\n" (e.g.).
255 * writer.lineBreak();
257 lineBreak : function()
259 if ( !this._
.inPre
&& this._
.output
.length
> 0 )
260 this._
.output
.push( this.lineBreakChars
);
265 * Writes the current indentation chars. It uses the
266 * {@link #indentationChars} property, repeating it for the current
269 * // Writes "\t" (e.g.).
270 * writer.indentation();
272 indentation : function()
275 this._
.output
.push( this._
.indentation
);
280 * Sets formatting rules for a give element. The possible rules are:
282 * <li><b>indent</b>: indent the element contents.</li>
283 * <li><b>breakBeforeOpen</b>: break line before the opener tag for this element.</li>
284 * <li><b>breakAfterOpen</b>: break line after the opener tag for this element.</li>
285 * <li><b>breakBeforeClose</b>: break line before the closer tag for this element.</li>
286 * <li><b>breakAfterClose</b>: break line after the closer tag for this element.</li>
289 * All rules default to "false". Each call to the function overrides
290 * already present rules, leaving the undefined untouched.
292 * By default, all elements available in the {@link CKEDITOR.dtd.$block),
293 * {@link CKEDITOR.dtd.$listItem} and {@link CKEDITOR.dtd.$tableContent}
294 * lists have all the above rules set to "true". Additionaly, the "br"
295 * element has the "breakAfterOpen" set to "true".
296 * @param {String} tagName The element name to which set the rules.
297 * @param {Object} rules An object containing the element rules.
299 * // Break line before and after "img" tags.
300 * writer.setRules( 'img',
302 * breakBeforeOpen : true
303 * breakAfterOpen : true
306 * // Reset the rules for the "h1" tag.
307 * writer.setRules( 'h1', {} );
309 setRules : function( tagName
, rules
)
311 var currentRules
= this._
.rules
[ tagName
];
314 CKEDITOR
.tools
.extend( currentRules
, rules
, true );
316 this._
.rules
[ tagName
] = rules
;