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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Generated by CoffeeScript 2.4.1
(function() {
var NodeType, WriterState, XMLStreamWriter, XMLWriterBase,
hasProp = {}.hasOwnProperty;
NodeType = require('./NodeType');
XMLWriterBase = require('./XMLWriterBase');
WriterState = require('./WriterState');
// Prints XML nodes to a stream
module.exports = XMLStreamWriter = class XMLStreamWriter extends XMLWriterBase {
// Initializes a new instance of `XMLStreamWriter`
// `stream` output stream
// `options.pretty` pretty prints the result
// `options.indent` indentation string
// `options.newline` newline sequence
// `options.offset` a fixed number of indentations to add to every line
// `options.allowEmpty` do not self close empty element tags
// 'options.dontPrettyTextNodes' if any text is present in node, don't indent or LF
// `options.spaceBeforeSlash` add a space before the closing slash of empty elements
constructor(stream, options) {
super(options);
this.stream = stream;
}
endline(node, options, level) {
if (node.isLastRootNode && options.state === WriterState.CloseTag) {
return '';
} else {
return super.endline(node, options, level);
}
}
document(doc, options) {
var child, i, j, k, len1, len2, ref, ref1, results;
ref = doc.children;
// set a flag so that we don't insert a newline after the last root level node
for (i = j = 0, len1 = ref.length; j < len1; i = ++j) {
child = ref[i];
child.isLastRootNode = i === doc.children.length - 1;
}
options = this.filterOptions(options);
ref1 = doc.children;
results = [];
for (k = 0, len2 = ref1.length; k < len2; k++) {
child = ref1[k];
results.push(this.writeChildNode(child, options, 0));
}
return results;
}
cdata(node, options, level) {
return this.stream.write(super.cdata(node, options, level));
}
comment(node, options, level) {
return this.stream.write(super.comment(node, options, level));
}
declaration(node, options, level) {
return this.stream.write(super.declaration(node, options, level));
}
docType(node, options, level) {
var child, j, len1, ref;
level || (level = 0);
this.openNode(node, options, level);
options.state = WriterState.OpenTag;
this.stream.write(this.indent(node, options, level));
this.stream.write('<!DOCTYPE ' + node.root().name);
// external identifier
if (node.pubID && node.sysID) {
this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
} else if (node.sysID) {
this.stream.write(' SYSTEM "' + node.sysID + '"');
}
// internal subset
if (node.children.length > 0) {
this.stream.write(' [');
this.stream.write(this.endline(node, options, level));
options.state = WriterState.InsideTag;
ref = node.children;
for (j = 0, len1 = ref.length; j < len1; j++) {
child = ref[j];
this.writeChildNode(child, options, level + 1);
}
options.state = WriterState.CloseTag;
this.stream.write(']');
}
// close tag
options.state = WriterState.CloseTag;
this.stream.write(options.spaceBeforeSlash + '>');
this.stream.write(this.endline(node, options, level));
options.state = WriterState.None;
return this.closeNode(node, options, level);
}
element(node, options, level) {
var att, attLen, child, childNodeCount, firstChildNode, j, len, len1, name, prettySuppressed, r, ratt, ref, ref1, ref2, rline;
level || (level = 0);
// open tag
this.openNode(node, options, level);
options.state = WriterState.OpenTag;
r = this.indent(node, options, level) + '<' + node.name;
// attributes
if (options.pretty && options.width > 0) {
len = r.length;
ref = node.attribs;
for (name in ref) {
if (!hasProp.call(ref, name)) continue;
att = ref[name];
ratt = this.attribute(att, options, level);
attLen = ratt.length;
if (len + attLen > options.width) {
rline = this.indent(node, options, level + 1) + ratt;
r += this.endline(node, options, level) + rline;
len = rline.length;
} else {
rline = ' ' + ratt;
r += rline;
len += rline.length;
}
}
} else {
ref1 = node.attribs;
for (name in ref1) {
if (!hasProp.call(ref1, name)) continue;
att = ref1[name];
r += this.attribute(att, options, level);
}
}
this.stream.write(r);
childNodeCount = node.children.length;
firstChildNode = childNodeCount === 0 ? null : node.children[0];
if (childNodeCount === 0 || node.children.every(function(e) {
return (e.type === NodeType.Text || e.type === NodeType.Raw || e.type === NodeType.CData) && e.value === '';
})) {
// empty element
if (options.allowEmpty) {
this.stream.write('>');
options.state = WriterState.CloseTag;
this.stream.write('</' + node.name + '>');
} else {
options.state = WriterState.CloseTag;
this.stream.write(options.spaceBeforeSlash + '/>');
}
} else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType.Text || firstChildNode.type === NodeType.Raw || firstChildNode.type === NodeType.CData) && (firstChildNode.value != null)) {
// do not indent text-only nodes
this.stream.write('>');
options.state = WriterState.InsideTag;
options.suppressPrettyCount++;
prettySuppressed = true;
this.writeChildNode(firstChildNode, options, level + 1);
options.suppressPrettyCount--;
prettySuppressed = false;
options.state = WriterState.CloseTag;
this.stream.write('</' + node.name + '>');
} else {
this.stream.write('>' + this.endline(node, options, level));
options.state = WriterState.InsideTag;
ref2 = node.children;
// inner tags
for (j = 0, len1 = ref2.length; j < len1; j++) {
child = ref2[j];
this.writeChildNode(child, options, level + 1);
}
// close tag
options.state = WriterState.CloseTag;
this.stream.write(this.indent(node, options, level) + '</' + node.name + '>');
}
this.stream.write(this.endline(node, options, level));
options.state = WriterState.None;
return this.closeNode(node, options, level);
}
processingInstruction(node, options, level) {
return this.stream.write(super.processingInstruction(node, options, level));
}
raw(node, options, level) {
return this.stream.write(super.raw(node, options, level));
}
text(node, options, level) {
return this.stream.write(super.text(node, options, level));
}
dtdAttList(node, options, level) {
return this.stream.write(super.dtdAttList(node, options, level));
}
dtdElement(node, options, level) {
return this.stream.write(super.dtdElement(node, options, level));
}
dtdEntity(node, options, level) {
return this.stream.write(super.dtdEntity(node, options, level));
}
dtdNotation(node, options, level) {
return this.stream.write(super.dtdNotation(node, options, level));
}
};
}).call(this);
|