/** * $Id: StringWriter.js 1137 2009-05-22 15:13:40Z spocke $ * * @author Moxiecode * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. */ (function(tinymce) { /**#@+ * @class This class writes nodes into a string. * @member tinymce.dom.StringWriter */ tinymce.create('tinymce.dom.StringWriter', { str : null, tags : null, count : 0, settings : null, indent : null, /** * Constructs a new StringWriter. * * @constructor * @param {Object} s Optional settings object. */ StringWriter : function(s) { this.settings = tinymce.extend({ indent_char : ' ', indentation : 0 }, s); this.reset(); }, /**#@+ * @method */ /** * Resets the writer so it can be reused the contents of the writer is cleared. */ reset : function() { this.indent = ''; this.str = ""; this.tags = []; this.count = 0; }, /** * Writes the start of an element like for example: 0) { n = this.tags.pop(); if (this._writeAttributesEnd(1)) this.writeRaw(''); if (this.settings.indentation > 0) this.writeRaw('\n'); } }, /** * Writes the end of a element. This will add a full end to the element even if it didn't have any children. */ writeFullEndElement : function() { if (this.tags.length > 0) { this._writeAttributesEnd(); this.writeRaw(''); if (this.settings.indentation > 0) this.writeRaw('\n'); } }, /** * Writes a text node value. * * @param {String} v Value to append as a text node. */ writeText : function(v) { this._writeAttributesEnd(); this.writeRaw(this.encode(v)); this.count++; }, /** * Writes a CDATA section. * * @param {String} v Value to write in CDATA. */ writeCDATA : function(v) { this._writeAttributesEnd(); this.writeRaw(''); this.count++; }, /** * Writes a comment. * * @param {String} v Value of the comment. */ writeComment : function(v) { this._writeAttributesEnd(); this.writeRaw(''); this.count++; }, /** * String writer specific function. Enables you to write raw contents to the string. * * @param {String} v String with raw contents to write. */ writeRaw : function(v) { this.str += v; }, /** * String writer specific method. This encodes the raw entities of a string. * * @param {String} s String to encode. * @return {String} String with entity encoding of the raw elements like <>&". */ encode : function(s) { return s.replace(/[<>&"]/g, function(v) { switch (v) { case '<': return '<'; case '>': return '>'; case '&': return '&'; case '"': return '"'; } return v; }); }, /** * Returns a string representation of the elements/nodes written. * * @return {String} String representation of the written elements/nodes. */ getContent : function() { return this.str; }, _writeAttributesEnd : function(s) { if (!this.inAttr) return; this.inAttr = false; if (s && this.elementCount == this.count) { this.writeRaw(' />'); return false; } this.writeRaw('>'); return true; } /**#@-*/ }); })(tinymce);