4 bind =
function(fn, me){
return function(){
return fn.apply(me, arguments); }; },
5 hasProp = {}.hasOwnProperty;
7 module.exports = XMLStringifier = (
function() {
8 function XMLStringifier(options) {
9 this.assertLegalChar = bind(this.assertLegalChar,
this);
11 this.allowSurrogateChars = options != null ? options.allowSurrogateChars :
void 0;
12 this.noDoubleEncoding = options != null ? options.noDoubleEncoding :
void 0;
13 ref = (options != null ? options.stringify :
void 0) || {};
15 if (!hasProp.call(ref, key))
continue;
21 XMLStringifier.prototype.eleName =
function(val) {
23 return this.assertLegalChar(val);
26 XMLStringifier.prototype.eleText =
function(val) {
28 return this.assertLegalChar(this.elEscape(val));
31 XMLStringifier.prototype.cdata =
function(val) {
33 if (val.match(/]]>/)) {
34 throw new Error(
"Invalid CDATA text: " + val);
36 return this.assertLegalChar(val);
39 XMLStringifier.prototype.comment =
function(val) {
41 if (val.match(/--/)) {
42 throw new Error(
"Comment text cannot contain double-hypen: " + val);
44 return this.assertLegalChar(val);
47 XMLStringifier.prototype.raw =
function(val) {
48 return '' + val ||
'';
51 XMLStringifier.prototype.attName =
function(val) {
52 return '' + val ||
'';
55 XMLStringifier.prototype.attValue =
function(val) {
57 return this.attEscape(val);
60 XMLStringifier.prototype.insTarget =
function(val) {
61 return '' + val ||
'';
64 XMLStringifier.prototype.insValue =
function(val) {
66 if (val.match(/\?>/)) {
67 throw new Error(
"Invalid processing instruction value: " + val);
72 XMLStringifier.prototype.xmlVersion =
function(val) {
74 if (!val.match(/1\.[0-9]+/)) {
75 throw new Error(
"Invalid version number: " + val);
80 XMLStringifier.prototype.xmlEncoding =
function(val) {
82 if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-]|-)*$/)) {
83 throw new Error(
"Invalid encoding: " + val);
88 XMLStringifier.prototype.xmlStandalone =
function(val) {
96 XMLStringifier.prototype.dtdPubID =
function(val) {
97 return '' + val ||
'';
100 XMLStringifier.prototype.dtdSysID =
function(val) {
101 return '' + val ||
'';
104 XMLStringifier.prototype.dtdElementValue =
function(val) {
105 return '' + val ||
'';
108 XMLStringifier.prototype.dtdAttType =
function(val) {
109 return '' + val ||
'';
112 XMLStringifier.prototype.dtdAttDefault =
function(val) {
114 return '' + val ||
'';
120 XMLStringifier.prototype.dtdEntityValue =
function(val) {
121 return '' + val ||
'';
124 XMLStringifier.prototype.dtdNData =
function(val) {
125 return '' + val ||
'';
128 XMLStringifier.prototype.convertAttKey =
'@';
130 XMLStringifier.prototype.convertPIKey =
'?';
132 XMLStringifier.prototype.convertTextKey =
'#text';
134 XMLStringifier.prototype.convertCDataKey =
'#cdata';
136 XMLStringifier.prototype.convertCommentKey =
'#comment';
138 XMLStringifier.prototype.convertRawKey =
'#raw';
140 XMLStringifier.prototype.assertLegalChar =
function(str) {
142 if (this.allowSurrogateChars) {
143 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
145 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
147 chr = str.match(chars);
149 throw new Error(
"Invalid character (" + chr +
") in string: " + str +
" at index " + chr.index);
154 XMLStringifier.prototype.elEscape =
function(str) {
156 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
157 return str.replace(ampregex,
'&').replace(/</g,
'<').replace(/>/g,
'>').replace(/\r/g,
'
');
160 XMLStringifier.prototype.attEscape =
function(str) {
162 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
163 return str.replace(ampregex,
'&').replace(/</g,
'<').replace(/
"/g, '"');
166 return XMLStringifier;