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