JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
c0dea03893db3a6b373fac9cb35b52853643bb3a
[ckeditor.git] / _source / plugins / htmlwriter / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'htmlwriter' );\r
7 \r
8 /**\r
9  * Class used to write HTML data.\r
10  * @constructor\r
11  * @example\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
19  */\r
20 CKEDITOR.htmlWriter = CKEDITOR.tools.createClass(\r
21 {\r
22         base : CKEDITOR.htmlParser.basicWriter,\r
23 \r
24         $ : function()\r
25         {\r
26                 // Call the base contructor.\r
27                 this.base();\r
28 \r
29                 /**\r
30                  * The characters to be used for each identation step.\r
31                  * @type String\r
32                  * @default "\t" (tab)\r
33                  * @example\r
34                  * // Use two spaces for indentation.\r
35                  * editorInstance.dataProcessor.writer.indentationChars = '  ';\r
36                  */\r
37                 this.indentationChars = '\t';\r
38 \r
39                 /**\r
40                  * The characters to be used to close "self-closing" elements, like "br" or\r
41                  * "img".\r
42                  * @type String\r
43                  * @default " />"\r
44                  * @example\r
45                  * // Use HTML4 notation for self-closing elements.\r
46                  * editorInstance.dataProcessor.writer.selfClosingEnd = '>';\r
47                  */\r
48                 this.selfClosingEnd = ' />';\r
49 \r
50                 /**\r
51                  * The characters to be used for line breaks.\r
52                  * @type String\r
53                  * @default "\n" (LF)\r
54                  * @example\r
55                  * // Use CRLF for line breaks.\r
56                  * editorInstance.dataProcessor.writer.lineBreakChars = '\r\n';\r
57                  */\r
58                 this.lineBreakChars = '\n';\r
59 \r
60                 this.forceSimpleAmpersand = false;\r
61 \r
62                 this.sortAttributes = true;\r
63 \r
64                 this._.indent = false;\r
65                 this._.indentation = '';\r
66                 this._.rules = {};\r
67 \r
68                 var dtd = CKEDITOR.dtd;\r
69 \r
70                 for ( var e in CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ) )\r
71                 {\r
72                         this.setRules( e,\r
73                                 {\r
74                                         indent : true,\r
75                                         breakBeforeOpen : true,\r
76                                         breakAfterOpen : true,\r
77                                         breakBeforeClose : !dtd[ e ][ '#' ],\r
78                                         breakAfterClose : true\r
79                                 });\r
80                 }\r
81                 this.setRules( 'br',\r
82                         {\r
83                                 breakAfterOpen : true\r
84                         });\r
85                 // Disable indentation on <pre>.\r
86                 this.setRules( 'pre',\r
87                 {\r
88                   indent: false\r
89                 } );\r
90         },\r
91 \r
92         proto :\r
93         {\r
94                 /**\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
99                  * @example\r
100                  * // Writes "&lt;p".\r
101                  * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );\r
102                  */\r
103                 openTag : function( tagName, attributes )\r
104                 {\r
105                         var rules = this._.rules[ tagName ];\r
106 \r
107                         if ( this._.indent )\r
108                                 this.indentation();\r
109                         // Do not break if indenting.\r
110                         else if ( rules && rules.breakBeforeOpen )\r
111                         {\r
112                                 this.lineBreak();\r
113                                 this.indentation();\r
114                         }\r
115 \r
116                         this._.output.push( '<', tagName );\r
117                 },\r
118 \r
119                 /**\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
124                  * @example\r
125                  * // Writes "&gt;".\r
126                  * writer.openTagClose( 'p', false );\r
127                  * @example\r
128                  * // Writes " /&gt;".\r
129                  * writer.openTagClose( 'br', true );\r
130                  */\r
131                 openTagClose : function( tagName, isSelfClose )\r
132                 {\r
133                         var rules = this._.rules[ tagName ];\r
134 \r
135                         if ( isSelfClose )\r
136                                 this._.output.push( this.selfClosingEnd );\r
137                         else\r
138                         {\r
139                                 this._.output.push( '>' );\r
140 \r
141                                 if ( rules && rules.indent )\r
142                                         this._.indentation += this.indentationChars;\r
143                         }\r
144 \r
145                         if ( rules && rules.breakAfterOpen )\r
146                                 this.lineBreak();\r
147                 },\r
148 \r
149                 /**\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
154                  * @example\r
155                  * // Writes ' class="MyClass"'.\r
156                  * writer.attribute( 'class', 'MyClass' );\r
157                  */\r
158                 attribute : function( attName, attValue )\r
159                 {\r
160                         if ( this.forceSimpleAmpersand )\r
161                                 attValue = attValue.replace( /&amp;/, '&' );\r
162 \r
163                         this._.output.push( ' ', attName, '="', attValue, '"' );\r
164                 },\r
165 \r
166                 /**\r
167                  * Writes a closer tag.\r
168                  * @param {String} tagName The element name for this tag.\r
169                  * @example\r
170                  * // Writes "&lt;/p&gt;".\r
171                  * writer.closeTag( 'p' );\r
172                  */\r
173                 closeTag : function( tagName )\r
174                 {\r
175                         var rules = this._.rules[ tagName ];\r
176 \r
177                         if ( rules && rules.indent )\r
178                                 this._.indentation = this._.indentation.substr( this.indentationChars.length );\r
179 \r
180                         if ( this._.indent )\r
181                                 this.indentation();\r
182                         // Do not break if indenting.\r
183                         else if ( rules && rules.breakBeforeClose )\r
184                         {\r
185                                 this.lineBreak();\r
186                                 this.indentation();\r
187                         }\r
188 \r
189                         this._.output.push( '</', tagName, '>' );\r
190 \r
191                         if ( rules && rules.breakAfterClose )\r
192                                 this.lineBreak();\r
193                 },\r
194 \r
195                 /**\r
196                  * Writes text.\r
197                  * @param {String} text The text value\r
198                  * @example\r
199                  * // Writes "Hello Word".\r
200                  * writer.text( 'Hello Word' );\r
201                  */\r
202                 text : function( text )\r
203                 {\r
204                         if ( this._.indent )\r
205                         {\r
206                                 this.indentation();\r
207                                 text = CKEDITOR.tools.ltrim( text );\r
208                         }\r
209 \r
210                         this._.output.push( text );\r
211                 },\r
212 \r
213                 /**\r
214                  * Writes a comment.\r
215                  * @param {String} comment The comment text.\r
216                  * @example\r
217                  * // Writes "&lt;!-- My comment --&gt;".\r
218                  * writer.comment( ' My comment ' );\r
219                  */\r
220                 comment : function( comment )\r
221                 {\r
222                         if ( this._.indent )\r
223                                 this.indentation();\r
224 \r
225                         this._.output.push( '<!--', comment, '-->' );\r
226                 },\r
227 \r
228                 /**\r
229                  * Writes a line break. It uses the {@link #lineBreakChars} property for it.\r
230                  * @example\r
231                  * // Writes "\n" (e.g.).\r
232                  * writer.lineBreak();\r
233                  */\r
234                 lineBreak : function()\r
235                 {\r
236                         if ( this._.output.length > 0 )\r
237                                 this._.output.push( this.lineBreakChars );\r
238                         this._.indent = true;\r
239                 },\r
240 \r
241                 /**\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
245                  * @example\r
246                  * // Writes "\t" (e.g.).\r
247                  * writer.indentation();\r
248                  */\r
249                 indentation : function()\r
250                 {\r
251                         this._.output.push( this._.indentation );\r
252                         this._.indent = false;\r
253                 },\r
254 \r
255                 /**\r
256                  * Sets formatting rules for a give element. The possible rules are:\r
257                  * <ul>\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
263                  * </ul>\r
264                  *\r
265                  * All rules default to "false".\r
266                  *\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
273                  * @example\r
274                  * // Break line before and after "img" tags.\r
275                  * writer.setRules( 'img',\r
276                  *     {\r
277                  *         breakBeforeOpen : true\r
278                  *         breakAfterOpen : true\r
279                  *     });\r
280                  * @example\r
281                  * // Reset the rules for the "h1" tag.\r
282                  * writer.setRules( 'h1', {} );\r
283                  */\r
284                 setRules : function( tagName, rules )\r
285                 {\r
286                         this._.rules[ tagName ] = rules;\r
287                 }\r
288         }\r
289 });\r