1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
var _ = require("underscore");
exports.writer = writer;
function writer(options) {
options = options || {};
if (options.prettyPrint) {
return prettyWriter();
} else {
return simpleWriter();
}
}
var indentedElements = {
div: true,
p: true,
ul: true,
li: true
};
function prettyWriter() {
var indentationLevel = 0;
var indentation = " ";
var stack = [];
var start = true;
var inText = false;
var writer = simpleWriter();
function open(tagName, attributes) {
if (indentedElements[tagName]) {
indent();
}
stack.push(tagName);
writer.open(tagName, attributes);
if (indentedElements[tagName]) {
indentationLevel++;
}
start = false;
}
function close(tagName) {
if (indentedElements[tagName]) {
indentationLevel--;
indent();
}
stack.pop();
writer.close(tagName);
}
function text(value) {
startText();
var text = isInPre() ? value : value.replace("\n", "\n" + indentation);
writer.text(text);
}
function selfClosing(tagName, attributes) {
indent();
writer.selfClosing(tagName, attributes);
}
function insideIndentedElement() {
return stack.length === 0 || indentedElements[stack[stack.length - 1]];
}
function startText() {
if (!inText) {
indent();
inText = true;
}
}
function indent() {
inText = false;
if (!start && insideIndentedElement() && !isInPre()) {
writer._append("\n");
for (var i = 0; i < indentationLevel; i++) {
writer._append(indentation);
}
}
}
function isInPre() {
return _.some(stack, function(tagName) {
return tagName === "pre";
});
}
return {
asString: writer.asString,
open: open,
close: close,
text: text,
selfClosing: selfClosing
};
}
function simpleWriter() {
var fragments = [];
function open(tagName, attributes) {
var attributeString = generateAttributeString(attributes);
fragments.push("<" + tagName + attributeString + ">");
}
function close(tagName) {
fragments.push("</" + tagName + ">");
}
function selfClosing(tagName, attributes) {
var attributeString = generateAttributeString(attributes);
fragments.push("<" + tagName + attributeString + " />");
}
function generateAttributeString(attributes) {
return _.map(attributes, function(value, key) {
return " " + key + '="' + escapeHtmlAttribute(value) + '"';
}).join("");
}
function text(value) {
fragments.push(escapeHtmlText(value));
}
function append(html) {
fragments.push(html);
}
function asString() {
return fragments.join("");
}
return {
asString: asString,
open: open,
close: close,
text: text,
selfClosing: selfClosing,
_append: append
};
}
function escapeHtmlText(value) {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function escapeHtmlAttribute(value) {
return value
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>');
}
|