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