/* * Copyright 2011 eBay Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ define.Class( 'raptor/templating/compiler/TemplateBuilder', ['raptor'], function(raptor, require) { "use strict"; var INDENT = " "; var stringify = require('raptor/json/stringify'), strings = require('raptor/strings'), Expression = require('raptor/templating/compiler/Expression'), forEach = raptor.forEach; var CodeWriter = function(indent) { this._indent = indent != null ? indent : INDENT + INDENT; this._code = strings.createStringBuilder(); this.firstStatement = true; this._bufferedText = null; this._bufferedContextMethodCalls = null; }; CodeWriter.prototype = { write: function(expression) { this.contextMethodCall("w", expression); }, text: function(text) { if (this._bufferedText === null) { this._bufferedText = text; } else { this._bufferedText += text; } }, contextMethodCall: function(methodName, args) { this.flushText(); if (!this._bufferedContextMethodCalls) { this._bufferedContextMethodCalls = []; } args = require('raptor/arrays').arrayFromArguments(arguments, 1); this._bufferedContextMethodCalls.push([methodName, args]); }, code: function(code) { this.flush(); this._code.append(code); }, statement: function(code) { this.flush(); this.code((this.firstStatement ? "" : "\n") + this._indent + code + "\n"); this.firstStatement = false; }, line: function(code) { this.code(this._indent + code + "\n"); }, indentStr: function(delta) { if (arguments.length === 0) { return this._indent; } else { var indent = this._indent; for (var i=0; i