UNPKG

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