3 var XMLStringifier, camelCase, kebabCase, ref, snakeCase, titleCase,
4 bind =
function(fn, me){
return function(){
return fn.apply(me, arguments); }; },
5 hasProp = {}.hasOwnProperty;
7 ref = require(
'./Utility'), camelCase = ref.camelCase, titleCase = ref.titleCase, kebabCase = ref.kebabCase, snakeCase = ref.snakeCase;
9 module.exports = XMLStringifier = (
function() {
10 function XMLStringifier(options) {
11 this.assertLegalChar = bind(this.assertLegalChar,
this);
13 options || (options = {});
14 this.allowSurrogateChars = options.allowSurrogateChars;
15 this.noDoubleEncoding = options.noDoubleEncoding;
16 this.textCase = options.textCase;
17 ref1 = options.stringify || {};
19 if (!hasProp.call(ref1, key))
continue;
25 XMLStringifier.prototype.eleName =
function(val) {
27 val = this.applyCase(val);
28 return this.assertLegalChar(val);
31 XMLStringifier.prototype.eleText =
function(val) {
33 return this.assertLegalChar(this.elEscape(val));
36 XMLStringifier.prototype.cdata =
function(val) {
38 val = val.replace(
']]>',
']]]]><![CDATA[>');
39 return this.assertLegalChar(val);
42 XMLStringifier.prototype.comment =
function(val) {
44 if (val.match(/--/)) {
45 throw new Error(
"Comment text cannot contain double-hypen: " + val);
47 return this.assertLegalChar(val);
50 XMLStringifier.prototype.raw =
function(val) {
51 return '' + val ||
'';
54 XMLStringifier.prototype.attName =
function(val) {
56 return val = this.applyCase(val);
59 XMLStringifier.prototype.attValue =
function(val) {
61 return this.attEscape(val);
64 XMLStringifier.prototype.insTarget =
function(val) {
65 return '' + val ||
'';
68 XMLStringifier.prototype.insValue =
function(val) {
70 if (val.match(/\?>/)) {
71 throw new Error(
"Invalid processing instruction value: " + val);
76 XMLStringifier.prototype.xmlVersion =
function(val) {
78 if (!val.match(/1\.[0-9]+/)) {
79 throw new Error(
"Invalid version number: " + val);
84 XMLStringifier.prototype.xmlEncoding =
function(val) {
86 if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-]|-)*$/)) {
87 throw new Error(
"Invalid encoding: " + val);
92 XMLStringifier.prototype.xmlStandalone =
function(val) {
100 XMLStringifier.prototype.dtdPubID =
function(val) {
101 return '' + val ||
'';
104 XMLStringifier.prototype.dtdSysID =
function(val) {
105 return '' + val ||
'';
108 XMLStringifier.prototype.dtdElementValue =
function(val) {
109 return '' + val ||
'';
112 XMLStringifier.prototype.dtdAttType =
function(val) {
113 return '' + val ||
'';
116 XMLStringifier.prototype.dtdAttDefault =
function(val) {
118 return '' + val ||
'';
124 XMLStringifier.prototype.dtdEntityValue =
function(val) {
125 return '' + val ||
'';
128 XMLStringifier.prototype.dtdNData =
function(val) {
129 return '' + val ||
'';
132 XMLStringifier.prototype.convertAttKey =
'@';
134 XMLStringifier.prototype.convertPIKey =
'?';
136 XMLStringifier.prototype.convertTextKey =
'#text';
138 XMLStringifier.prototype.convertCDataKey =
'#cdata';
140 XMLStringifier.prototype.convertCommentKey =
'#comment';
142 XMLStringifier.prototype.convertRawKey =
'#raw';
144 XMLStringifier.prototype.assertLegalChar =
function(str) {
146 if (this.allowSurrogateChars) {
147 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
149 chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
151 chr = str.match(chars);
153 throw new Error(
"Invalid character (" + chr +
") in string: " + str +
" at index " + chr.index);
158 XMLStringifier.prototype.applyCase =
function(str) {
159 switch (this.textCase) {
161 return camelCase(str);
163 return titleCase(str);
166 return kebabCase(str);
168 return snakeCase(str);
170 return kebabCase(str).toUpperCase();
176 XMLStringifier.prototype.elEscape =
function(str) {
178 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
179 return str.replace(ampregex,
'&').replace(/</g,
'<').replace(/>/g,
'>').replace(/\r/g,
'
');
182 XMLStringifier.prototype.attEscape =
function(str) {
184 ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
185 return str.replace(ampregex,
'&').replace(/</g,
'<').replace(/
"/g, '"').replace(/\t/g, '	').replace(/\n/g, '
').replace(/\r/g, '
');
188 return XMLStringifier;