3 var XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLText, XMLWriterBase,
4 extend =
function(child, parent) {
for (var key in parent) {
if (hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype =
new ctor(); child.__super__ = parent.prototype;
return child; },
5 hasProp = {}.hasOwnProperty;
7 XMLDeclaration = require(
'./XMLDeclaration');
9 XMLDocType = require(
'./XMLDocType');
11 XMLCData = require(
'./XMLCData');
13 XMLComment = require(
'./XMLComment');
15 XMLElement = require(
'./XMLElement');
17 XMLRaw = require(
'./XMLRaw');
19 XMLText = require(
'./XMLText');
21 XMLProcessingInstruction = require(
'./XMLProcessingInstruction');
23 XMLDTDAttList = require(
'./XMLDTDAttList');
25 XMLDTDElement = require(
'./XMLDTDElement');
27 XMLDTDEntity = require(
'./XMLDTDEntity');
29 XMLDTDNotation = require(
'./XMLDTDNotation');
31 XMLWriterBase = require(
'./XMLWriterBase');
33 module.exports = XMLStringWriter = (
function(superClass) {
34 extend(XMLStringWriter, superClass);
36 function XMLStringWriter(options) {
37 XMLStringWriter.__super__.constructor.call(
this, options);
40 XMLStringWriter.prototype.document =
function(doc) {
41 var child, i, len, r, ref;
44 for (i = 0, len = ref.length; i < len; i++) {
48 case !(child instanceof XMLDeclaration):
49 return this.declaration(child);
50 case !(child instanceof XMLDocType):
51 return this.docType(child);
52 case !(child instanceof XMLComment):
53 return this.comment(child);
54 case !(child instanceof XMLProcessingInstruction):
55 return this.processingInstruction(child);
57 return this.element(child, 0);
61 if (this.pretty && r.slice(-
this.newline.length) === this.newline) {
62 r = r.slice(0, -this.newline.length);
67 XMLStringWriter.prototype.attribute =
function(att) {
68 return ' ' + att.name +
'="' + att.value +
'"';
71 XMLStringWriter.prototype.cdata =
function(node, level) {
72 return this.space(level) +
'<![CDATA[' + node.text +
']]>' + this.newline;
75 XMLStringWriter.prototype.comment =
function(node, level) {
76 return this.space(level) +
'<!-- ' + node.text +
' -->' + this.newline;
79 XMLStringWriter.prototype.declaration =
function(node, level) {
81 r = this.space(level);
82 r +=
'<?xml version="' + node.version +
'"';
83 if (node.encoding != null) {
84 r +=
' encoding="' + node.encoding +
'"';
86 if (node.standalone != null) {
87 r +=
' standalone="' + node.standalone +
'"';
94 XMLStringWriter.prototype.docType =
function(node, level) {
95 var child, i, len, r, ref;
97 r = this.space(level);
98 r +=
'<!DOCTYPE ' + node.root().name;
99 if (node.pubID && node.sysID) {
100 r +=
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"';
101 }
else if (node.sysID) {
102 r +=
' SYSTEM "' + node.sysID +
'"';
104 if (node.children.length > 0) {
108 for (i = 0, len = ref.length; i < len; i++) {
112 case !(child instanceof XMLDTDAttList):
113 return this.dtdAttList(child, level + 1);
114 case !(child instanceof XMLDTDElement):
115 return this.dtdElement(child, level + 1);
116 case !(child instanceof XMLDTDEntity):
117 return this.dtdEntity(child, level + 1);
118 case !(child instanceof XMLDTDNotation):
119 return this.dtdNotation(child, level + 1);
120 case !(child instanceof XMLCData):
121 return this.cdata(child, level + 1);
122 case !(child instanceof XMLComment):
123 return this.comment(child, level + 1);
124 case !(child instanceof XMLProcessingInstruction):
125 return this.processingInstruction(child, level + 1);
127 throw new Error(
"Unknown DTD node type: " + child.constructor.name);
138 XMLStringWriter.prototype.element =
function(node, level) {
139 var att, child, i, len, name, r, ref, ref1, space;
140 level || (level = 0);
141 space = this.space(level);
143 r += space +
'<' + node.name;
144 ref = node.attributes;
146 if (!hasProp.call(ref, name))
continue;
148 r += this.attribute(att);
150 if (node.children.length === 0 || node.children.every(
function(e) {
151 return e.value ===
'';
153 if (this.allowEmpty) {
154 r +=
'></' + node.name +
'>' + this.newline;
156 r +=
'/>' + this.newline;
158 }
else if (this.pretty && node.children.length === 1 && (node.children[0].value != null)) {
160 r += node.children[0].value;
161 r +=
'</' + node.name +
'>' + this.newline;
163 r +=
'>' + this.newline;
164 ref1 = node.children;
165 for (i = 0, len = ref1.length; i < len; i++) {
169 case !(child instanceof XMLCData):
170 return this.cdata(child, level + 1);
171 case !(child instanceof XMLComment):
172 return this.comment(child, level + 1);
173 case !(child instanceof XMLElement):
174 return this.element(child, level + 1);
175 case !(child instanceof XMLRaw):
176 return this.raw(child, level + 1);
177 case !(child instanceof XMLText):
178 return this.text(child, level + 1);
179 case !(child instanceof XMLProcessingInstruction):
180 return this.processingInstruction(child, level + 1);
182 throw new Error(
"Unknown XML node type: " + child.constructor.name);
186 r += space +
'</' + node.name +
'>' + this.newline;
191 XMLStringWriter.prototype.processingInstruction =
function(node, level) {
193 r = this.space(level) +
'<?' + node.target;
195 r +=
' ' + node.value;
197 r +=
'?>' + this.newline;
201 XMLStringWriter.prototype.raw =
function(node, level) {
202 return this.space(level) + node.value + this.newline;
205 XMLStringWriter.prototype.text =
function(node, level) {
206 return this.space(level) + node.value + this.newline;
209 XMLStringWriter.prototype.dtdAttList =
function(node, level) {
211 r = this.space(level) +
'<!ATTLIST ' + node.elementName +
' ' + node.attributeName +
' ' + node.attributeType;
212 if (node.defaultValueType !==
'#DEFAULT') {
213 r +=
' ' + node.defaultValueType;
215 if (node.defaultValue) {
216 r +=
' "' + node.defaultValue +
'"';
218 r +=
'>' + this.newline;
222 XMLStringWriter.prototype.dtdElement =
function(node, level) {
223 return this.space(level) +
'<!ELEMENT ' + node.name +
' ' + node.value +
'>' + this.newline;
226 XMLStringWriter.prototype.dtdEntity =
function(node, level) {
228 r = this.space(level) +
'<!ENTITY';
232 r +=
' ' + node.name;
234 r +=
' "' + node.value +
'"';
236 if (node.pubID && node.sysID) {
237 r +=
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"';
238 }
else if (node.sysID) {
239 r +=
' SYSTEM "' + node.sysID +
'"';
242 r +=
' NDATA ' + node.nData;
245 r +=
'>' + this.newline;
249 XMLStringWriter.prototype.dtdNotation =
function(node, level) {
251 r = this.space(level) +
'<!NOTATION ' + node.name;
252 if (node.pubID && node.sysID) {
253 r +=
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"';
254 }
else if (node.pubID) {
255 r +=
' PUBLIC "' + node.pubID +
'"';
256 }
else if (node.sysID) {
257 r +=
' SYSTEM "' + node.sysID +
'"';
259 r +=
'>' + this.newline;
263 XMLStringWriter.prototype.openNode =
function(node, level) {
264 var att, name, r, ref;
265 level || (level = 0);
266 if (node instanceof XMLElement) {
267 r = this.space(level) +
'<' + node.name;
268 ref = node.attributes;
270 if (!hasProp.call(ref, name))
continue;
272 r += this.attribute(att);
274 r += (node.children ?
'>' :
'/>') + this.newline;
277 r = this.space(level) +
'<!DOCTYPE ' + node.rootNodeName;
278 if (node.pubID && node.sysID) {
279 r +=
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"';
280 }
else if (node.sysID) {
281 r +=
' SYSTEM "' + node.sysID +
'"';
283 r += (node.children ?
' [' :
'>') + this.newline;
288 XMLStringWriter.prototype.closeNode =
function(node, level) {
289 level || (level = 0);
291 case !(node instanceof XMLElement):
292 return this.space(level) +
'</' + node.name +
'>' + this.newline;
293 case !(node instanceof XMLDocType):
294 return this.space(level) +
']>' + this.newline;
298 return XMLStringWriter;