JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / htmlparser / basicwriter.js
1 /*\r
2 Copyright (c) 2003-2010, 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.htmlParser.basicWriter = CKEDITOR.tools.createClass(\r
7 {\r
8         $ : function()\r
9         {\r
10                 this._ =\r
11                 {\r
12                         output : []\r
13                 };\r
14         },\r
15 \r
16         proto :\r
17         {\r
18                 /**\r
19                  * Writes the tag opening part for a opener tag.\r
20                  * @param {String} tagName The element name for this tag.\r
21                  * @param {Object} attributes The attributes defined for this tag. The\r
22                  *              attributes could be used to inspect the tag.\r
23                  * @example\r
24                  * // Writes "<p".\r
25                  * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );\r
26                  */\r
27                 openTag : function( tagName, attributes )\r
28                 {\r
29                         this._.output.push( '<', tagName );\r
30                 },\r
31 \r
32                 /**\r
33                  * Writes the tag closing part for a opener tag.\r
34                  * @param {String} tagName The element name for this tag.\r
35                  * @param {Boolean} isSelfClose Indicates that this is a self-closing tag,\r
36                  *              like "br" or "img".\r
37                  * @example\r
38                  * // Writes "&gt;".\r
39                  * writer.openTagClose( 'p', false );\r
40                  * @example\r
41                  * // Writes " /&gt;".\r
42                  * writer.openTagClose( 'br', true );\r
43                  */\r
44                 openTagClose : function( tagName, isSelfClose )\r
45                 {\r
46                         if ( isSelfClose )\r
47                                 this._.output.push( ' />' );\r
48                         else\r
49                                 this._.output.push( '>' );\r
50                 },\r
51 \r
52                 /**\r
53                  * Writes an attribute. This function should be called after opening the\r
54                  * tag with {@link #openTagClose}.\r
55                  * @param {String} attName The attribute name.\r
56                  * @param {String} attValue The attribute value.\r
57                  * @example\r
58                  * // Writes ' class="MyClass"'.\r
59                  * writer.attribute( 'class', 'MyClass' );\r
60                  */\r
61                 attribute : function( attName, attValue )\r
62                 {\r
63                         this._.output.push( ' ', attName, '="', attValue, '"' );\r
64                 },\r
65 \r
66                 /**\r
67                  * Writes a closer tag.\r
68                  * @param {String} tagName The element name for this tag.\r
69                  * @example\r
70                  * // Writes "&lt;/p&gt;".\r
71                  * writer.closeTag( 'p' );\r
72                  */\r
73                 closeTag : function( tagName )\r
74                 {\r
75                         this._.output.push( '</', tagName, '>' );\r
76                 },\r
77 \r
78                 /**\r
79                  * Writes text.\r
80                  * @param {String} text The text value\r
81                  * @example\r
82                  * // Writes "Hello Word".\r
83                  * writer.text( 'Hello Word' );\r
84                  */\r
85                 text : function( text )\r
86                 {\r
87                         this._.output.push( text );\r
88                 },\r
89 \r
90                 /**\r
91                  * Writes a comment.\r
92                  * @param {String} comment The comment text.\r
93                  * @example\r
94                  * // Writes "&lt;!-- My comment --&gt;".\r
95                  * writer.comment( ' My comment ' );\r
96                  */\r
97                 comment : function( comment )\r
98                 {\r
99                         this._.output.push( '<!--', comment, '-->' );\r
100                 },\r
101 \r
102                 /**\r
103                  * Writes any kind of data to the ouput.\r
104                  * @example\r
105                  * writer.write( 'This is an &lt;b&gt;example&lt;/b&gt;.' );\r
106                  */\r
107                 write : function( data )\r
108                 {\r
109                         this._.output.push( data );\r
110                 },\r
111 \r
112                 /**\r
113                  * Empties the current output buffer.\r
114                  * @example\r
115                  * writer.reset();\r
116                  */\r
117                 reset : function()\r
118                 {\r
119                         this._.output = [];\r
120                         this._.indent = false;\r
121                 },\r
122 \r
123                 /**\r
124                  * Empties the current output buffer.\r
125                  * @param {Boolean} reset Indicates that the {@link reset} function is to\r
126                  *              be automatically called after retrieving the HTML.\r
127                  * @returns {String} The HTML written to the writer so far.\r
128                  * @example\r
129                  * var html = writer.getHtml();\r
130                  */\r
131                 getHtml : function( reset )\r
132                 {\r
133                         var html = this._.output.join( '' );\r
134 \r
135                         if ( reset )\r
136                                 this.reset();\r
137 \r
138                         return html;\r
139                 }\r
140         }\r
141 });\r