1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var _ = require('lodash');
|
11 |
|
12 | var HTML_START_CHAR = '<';
|
13 | var HTML_END_CHAR = '>';
|
14 | var HTML_FULL_END_STRING = ' />';
|
15 | var HTML_SPACE_CHAR = ' ';
|
16 | var HTML_ATTR_STRING = '%0="%1"';
|
17 | var HTML_START_TAG_STRING = '<%0';
|
18 | var HTML_END_TAG_STRING = '</%0>';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function HtmlWriter() {
|
27 | |
28 |
|
29 |
|
30 |
|
31 | this.bufferedAttributes=[];
|
32 | |
33 |
|
34 |
|
35 |
|
36 | this.bufferedTags = [];
|
37 | |
38 |
|
39 |
|
40 |
|
41 | this.buffer = '';
|
42 | |
43 |
|
44 |
|
45 |
|
46 | this.indent = true;
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | HtmlWriter.prototype.writeAttribute = function(name, value)
|
56 | {
|
57 | this.bufferedAttributes.push({name:name, value:value});
|
58 | return this;
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | HtmlWriter.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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | HtmlWriter.prototype.writeBeginTag = function(tag) {
|
93 |
|
94 | if (this.indent)
|
95 | {
|
96 |
|
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 |
|
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 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | HtmlWriter.prototype.writeFullBeginTag = function(tag) {
|
122 |
|
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 |
|
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 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | HtmlWriter.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 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | HtmlWriter.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 |
|
182 |
|
183 |
|
184 | HtmlWriter.prototype.write = function(s) {
|
185 | this.buffer += s;
|
186 | return this;
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 | HtmlWriter.prototype.toString = function() {
|
192 | return this.buffer;
|
193 | };
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | HtmlWriter.prototype.writeTo = function(fn)
|
199 | {
|
200 | if (typeof fn === 'function') {
|
201 |
|
202 | fn(this.buffer);
|
203 |
|
204 | this.buffer='';
|
205 |
|
206 | this.bufferedTags.splice(0,this.bufferedTags.length);
|
207 | }
|
208 | };
|
209 |
|
210 | if (typeof exports !== 'undefined') {
|
211 | module.exports.HtmlWriter = HtmlWriter;
|
212 | } |
\ | No newline at end of file |