UNPKG

5.66 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9var _ = require('lodash');
10// eslint-disable-next-line no-unused-vars
11var HTML_START_CHAR = '<';
12var HTML_END_CHAR = '>';
13var HTML_FULL_END_STRING = ' />';
14var HTML_SPACE_CHAR = ' ';
15var HTML_ATTR_STRING = '%0="%1"';
16var HTML_START_TAG_STRING = '<%0';
17var HTML_END_TAG_STRING = '</%0>';
18/**
19 * @classdesc HtmlWriter class represents a helper class for rendering HTML content.
20 * @class
21 * @constructor
22 */
23function HtmlWriter() {
24 /**
25 * @private
26 * @type {Array}
27 */
28 this.bufferedAttributes=[];
29 /**
30 * @private
31 * @type {Array}
32 */
33 this.bufferedTags = [];
34 /**
35 * @private
36 * @type {String}
37 */
38 this.buffer = '';
39 /**
40 * @private
41 * @type {Integer}
42 */
43 this.indent = true;
44}
45// noinspection JSUnusedGlobalSymbols
46/**
47 * Writes an attribute to an array of attributes that is going to be used in writeBeginTag function
48 * @param {String} name - The name of the HTML attribute
49 * @param {String} value - The value of the HTML attribute
50 * @returns {HtmlWriter}
51 */
52HtmlWriter.prototype.writeAttribute = function(name, value)
53{
54 this.bufferedAttributes.push({name:name, value:value});
55 return this;
56};
57// noinspection JSUnusedGlobalSymbols
58/**
59 * Writes an array of attributes to the output buffer. This attributes are going to be rendered after writeBeginTag or WriteFullBeginTag function call.
60 * @param {Array|Object} obj - An array of attributes or an object that represents an array of attributes
61 * @returns {HtmlWriter}
62 */
63HtmlWriter.prototype.writeAttributes = function(obj)
64{
65 if (obj===null)
66 return this;
67 if (_.isArray(obj)) {
68 for (var i = 0; i < obj.length; i++) {
69 this.bufferedAttributes.push( { name:obj[i].name, value:obj[i].value } );
70 }
71 }
72 else {
73 for (var prop in obj)
74 {
75 if (obj.hasOwnProperty(prop)) {
76 if (obj[prop]!==null) {
77 this.bufferedAttributes.push( { name:prop, value:obj[prop] } );
78 }
79 }
80 }
81 }
82 return this;
83};
84// noinspection JSUnusedGlobalSymbols
85/**
86 * @param {String} tag
87 * @returns {HtmlWriter}
88 */
89HtmlWriter.prototype.writeBeginTag = function(tag) {
90 //write <TAG
91 if (this.indent)
92 {
93 //this.buffer += '\n';
94 this.buffer +=_.repeat('\t', this.bufferedTags.length);
95 }
96 this.buffer += HTML_START_TAG_STRING.replace(/%0/, tag);
97 this.bufferedTags.push(tag);
98 if (this.bufferedAttributes.length>0)
99 {
100 var s = '';
101 _.forEach(this.bufferedAttributes, function(attr) {
102 //write attribute='value'
103 s += HTML_SPACE_CHAR;
104 s += HTML_ATTR_STRING.replace(/%0/,attr.name).replace(/%1/, _.escape(attr.value));
105 });
106 this.buffer += s;
107 }
108 this.bufferedAttributes.splice(0,this.bufferedAttributes.length);
109 this.buffer += HTML_END_CHAR;
110 return this;
111};
112// noinspection JSUnusedGlobalSymbols
113/**
114 * Writes a full begin HTML tag (e.g <div/>).
115 * @param {String} tag
116 * @returns {HtmlWriter}
117 */
118HtmlWriter.prototype.writeFullBeginTag = function(tag) {
119 //write <TAG
120 if (this.indent)
121 {
122 this.buffer += '\n';
123 this.buffer +=_.repeat('\t', this.bufferedTags.length);
124 }
125 this.buffer += HTML_START_TAG_STRING.replace(/%0/, tag);
126 if (this.bufferedAttributes.length>0)
127 {
128 var s = '';
129 _.forEach(this.bufferedAttributes, function(attr) {
130 //write attribute='value'
131 s += HTML_SPACE_CHAR;
132 s += HTML_ATTR_STRING.replace(/%0/,attr.name).replace(/%1/, _.escape(attr.value));
133 });
134 this.buffer += s;
135 }
136 this.bufferedAttributes.splice(0,this.bufferedAttributes.length);
137 this.buffer += HTML_FULL_END_STRING;
138 return this;
139};
140// noinspection JSUnusedGlobalSymbols
141/**
142 * Writes an end HTML tag (e.g </div>) based on the current buffered tags.
143 * @returns {HtmlWriter}
144 */
145HtmlWriter.prototype.writeEndTag = function()
146{
147 var tagsLength = this.bufferedTags ? this.bufferedTags.length : 0;
148 if (tagsLength===0)
149 return this;
150 if (this.indent)
151 {
152 this.buffer += '\n';
153 this.buffer +=_.repeat('\t', tagsLength-1);
154 }
155 this.buffer += HTML_END_TAG_STRING.replace(/%0/,this.bufferedTags[tagsLength-1]);
156 this.bufferedTags.splice(tagsLength-1,1);
157 return this;
158};
159// noinspection JSUnusedGlobalSymbols
160/**
161 *
162 * @param {String} s
163 * @returns {HtmlWriter}
164 */
165HtmlWriter.prototype.writeText = function(s) {
166 if (!s)
167 return this;
168 if (this.indent)
169 {
170 this.buffer += '\n';
171 this.buffer +=_.repeat('\t', this.bufferedTags.length);
172 }
173 this.buffer += _.escape(s);
174 return this;
175};
176/**
177 *
178 * @param {String} s
179 * @returns {HtmlWriter}
180 */
181HtmlWriter.prototype.write = function(s) {
182 this.buffer += s;
183 return this;
184};
185/**
186 * @returns {String}
187 */
188HtmlWriter.prototype.toString = function() {
189 return this.buffer;
190};
191// noinspection JSUnusedGlobalSymbols
192/**
193 * @param {function} fn
194 */
195HtmlWriter.prototype.writeTo = function(fn)
196{
197 if (typeof fn === 'function') {
198 //call function
199 fn(this.buffer);
200 //and clear buffer
201 this.buffer='';
202 //and clear buffered tags
203 this.bufferedTags.splice(0,this.bufferedTags.length);
204 }
205};
206
207if (typeof exports !== 'undefined') {
208 module.exports.HtmlWriter = HtmlWriter;
209}
\No newline at end of file