1 /* 2 * ***** BEGIN LICENSE BLOCK ***** 3 * Zimbra Collaboration Suite Web Client 4 * Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. 5 * 6 * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: https://www.zimbra.com/license 9 * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15 10 * have been added to cover use of software over a computer network and provide for limited attribution 11 * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B. 12 * 13 * Software distributed under the License is distributed on an "AS IS" basis, 14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. 15 * See the License for the specific language governing rights and limitations under the License. 16 * The Original Code is Zimbra Open Source Web Client. 17 * The Initial Developer of the Original Code is Zimbra, Inc. All rights to the Original Code were 18 * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015. 19 * 20 * All portions of the code are Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved. 21 * ***** END LICENSE BLOCK ***** 22 */ 23 24 25 /** 26 * @class 27 * Use this class to implement an efficient String Buffer. It is especially useful for assembling HTML. 28 * <p> 29 * Usage: 30 * <ol> 31 * <li>For a small amount of text, call it statically as: 32 * <pre> 33 * AjxBuffer.concat("a", 1, "b", this.getFoo(), ...); 34 * </pre> 35 * </li> 36 * <li>Or create an instance and use that to assemble a big pile of HTML: 37 * <pre> 38 * var buffer = new AjxBuffer(); 39 * buffer.append("foo", myObject.someOtherFoo(), ...); 40 * ... 41 * buffer.append(fooo.yetMoreFoo()); 42 * return buffer.toString(); 43 * </pre> 44 * </li> 45 * </ol> 46 * 47 * It is useful (and quicker!) to create a single buffer and then pass that to subroutines 48 * that are doing assembly of HTML pieces for you. 49 * </p><p> 50 * Note: in both modes you can pass as many arguments you like to the 51 * methods -- this is quite a bit faster than concatenating the arguments 52 * with the + sign (eg: do not do <code>buffer.append("a" + b.foo());</code>). 53 * 54 * @author Owen Williams 55 * 56 * @private 57 */ 58 AjxBuffer = function() { 59 this.clear(); 60 if (arguments.length > 0) { 61 arguments.join = this.buffer.join; 62 this.buffer[this.buffer.length] = arguments.join(""); 63 } 64 } 65 AjxBuffer.prototype.toString = function () { 66 return this.buffer.join(""); 67 } 68 AjxBuffer.prototype.join = function (delim) { 69 if (delim == null) delim = ""; 70 return this.buffer.join(delim); 71 } 72 AjxBuffer.prototype.append = function () { 73 arguments.join = this.buffer.join; 74 this.buffer[this.buffer.length] = arguments.join(""); 75 } 76 AjxBuffer.prototype.join = function (str) { 77 return this.buffer.join(str); 78 } 79 AjxBuffer.prototype.set = function(str) { 80 this.buffer = [str]; 81 } 82 AjxBuffer.prototype.clear = function() { 83 this.buffer = []; 84 } 85 AjxBuffer.concat = function() { 86 arguments.join = Array.prototype.join; 87 return arguments.join(""); 88 } 89 AjxBuffer.append = AjxBuffer.concat; 90