00001
00002 (function() {
00003 var XMLStringifier,
00004 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
00005 hasProp = {}.hasOwnProperty;
00006
00007 module.exports = XMLStringifier = (function() {
00008 function XMLStringifier(options) {
00009 this.assertLegalChar = bind(this.assertLegalChar, this);
00010 var key, ref, value;
00011 this.allowSurrogateChars = options != null ? options.allowSurrogateChars : void 0;
00012 this.noDoubleEncoding = options != null ? options.noDoubleEncoding : void 0;
00013 ref = (options != null ? options.stringify : void 0) || {};
00014 for (key in ref) {
00015 if (!hasProp.call(ref, key)) continue;
00016 value = ref[key];
00017 this[key] = value;
00018 }
00019 }
00020
00021 XMLStringifier.prototype.eleName = function(val) {
00022 val = '' + val || '';
00023 return this.assertLegalChar(val);
00024 };
00025
00026 XMLStringifier.prototype.eleText = function(val) {
00027 val = '' + val || '';
00028 return this.assertLegalChar(this.elEscape(val));
00029 };
00030
00031 XMLStringifier.prototype.cdata = function(val) {
00032 val = '' + val || '';
00033 if (val.match(/]]>/)) {
00034 throw new Error("Invalid CDATA text: " + val);
00035 }
00036 return this.assertLegalChar(val);
00037 };
00038
00039 XMLStringifier.prototype.comment = function(val) {
00040 val = '' + val || '';
00041 if (val.match(/--/)) {
00042 throw new Error("Comment text cannot contain double-hypen: " + val);
00043 }
00044 return this.assertLegalChar(val);
00045 };
00046
00047 XMLStringifier.prototype.raw = function(val) {
00048 return '' + val || '';
00049 };
00050
00051 XMLStringifier.prototype.attName = function(val) {
00052 return '' + val || '';
00053 };
00054
00055 XMLStringifier.prototype.attValue = function(val) {
00056 val = '' + val || '';
00057 return this.attEscape(val);
00058 };
00059
00060 XMLStringifier.prototype.insTarget = function(val) {
00061 return '' + val || '';
00062 };
00063
00064 XMLStringifier.prototype.insValue = function(val) {
00065 val = '' + val || '';
00066 if (val.match(/\?>/)) {
00067 throw new Error("Invalid processing instruction value: " + val);
00068 }
00069 return val;
00070 };
00071
00072 XMLStringifier.prototype.xmlVersion = function(val) {
00073 val = '' + val || '';
00074 if (!val.match(/1\.[0-9]+/)) {
00075 throw new Error("Invalid version number: " + val);
00076 }
00077 return val;
00078 };
00079
00080 XMLStringifier.prototype.xmlEncoding = function(val) {
00081 val = '' + val || '';
00082 if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-]|-)*$/)) {
00083 throw new Error("Invalid encoding: " + val);
00084 }
00085 return val;
00086 };
00087
00088 XMLStringifier.prototype.xmlStandalone = function(val) {
00089 if (val) {
00090 return "yes";
00091 } else {
00092 return "no";
00093 }
00094 };
00095
00096 XMLStringifier.prototype.dtdPubID = function(val) {
00097 return '' + val || '';
00098 };
00099
00100 XMLStringifier.prototype.dtdSysID = function(val) {
00101 return '' + val || '';
00102 };
00103
00104 XMLStringifier.prototype.dtdElementValue = function(val) {
00105 return '' + val || '';
00106 };
00107
00108 XMLStringifier.prototype.dtdAttType = function(val) {
00109 return '' + val || '';
00110 };
00111
00112 XMLStringifier.prototype.dtdAttDefault = function(val) {
00113 if (val != null) {
00114 return '' + val || '';
00115 } else {
00116 return val;
00117 }
00118 };
00119
00120 XMLStringifier.prototype.dtdEntityValue = function(val) {
00121 return '' + val || '';
00122 };
00123
00124 XMLStringifier.prototype.dtdNData = function(val) {
00125 return '' + val || '';
00126 };
00127
00128 XMLStringifier.prototype.convertAttKey = '@';
00129
00130 XMLStringifier.prototype.convertPIKey = '?';
00131
00132 XMLStringifier.prototype.convertTextKey = '#text';
00133
00134 XMLStringifier.prototype.convertCDataKey = '#cdata';
00135
00136 XMLStringifier.prototype.convertCommentKey = '#comment';
00137
00138 XMLStringifier.prototype.convertRawKey = '#raw';
00139
00140 XMLStringifier.prototype.assertLegalChar = function(str) {
00141 var chars, chr;
00142 if (this.allowSurrogateChars) {
00143 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
00144 } else {
00145 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
00146 }
00147 chr = str.match(chars);
00148 if (chr) {
00149 throw new Error("Invalid character (" + chr + ") in string: " + str + " at index " + chr.index);
00150 }
00151 return str;
00152 };
00153
00154 XMLStringifier.prototype.elEscape = function(str) {
00155 var ampregex;
00156 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
00157 return str.replace(ampregex, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\r/g, '
');
00158 };
00159
00160 XMLStringifier.prototype.attEscape = function(str) {
00161 var ampregex;
00162 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
00163 return str.replace(ampregex, '&').replace(/</g, '<').replace(/"/g, '"');
00164 };
00165
00166 return XMLStringifier;
00167
00168 })();
00169
00170 }).call(this);