-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XML Prolog support #34
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
* bugfixes and code cleanup by user @laubstein | ||
* https://github.com/tyrasd/jxon/pull/32 | ||
* | ||
* adapted for nodejs and npm by @tyrasd (Martin Raifer <[email protected]>) | ||
* adapted for nodejs and npm by @tyrasd (Martin Raifer <[email protected]>) | ||
*/ | ||
|
||
(function(root, factory) { | ||
|
@@ -54,11 +54,16 @@ | |
trueIsEmpty: false, | ||
autoDate: false, | ||
ignorePrefixedNodes: false, | ||
parseValues: false | ||
parseValues: false, | ||
forceXmlProlog: false, | ||
defaultXmlProlog: '<?xml version="1.0" encoding="UTF-8"?>' | ||
}; | ||
var aCache = []; | ||
var rIsNull = /^\s*$/; | ||
var rIsBool = /^(?:true|false)$/i; | ||
var rXmlProlog = /^(<\?xml.*?\?>[\n]?)/; | ||
var rXmlPrologAttributes = /\b(version|encoding|standalone)="([^"]+?)"/g; | ||
var oXmlProlog = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. global variable :( |
||
var DOMParser; | ||
|
||
return new (function() { | ||
|
@@ -76,6 +81,56 @@ | |
} | ||
}; | ||
|
||
/** | ||
* Helper function: given a xml prolog (eg: <?xml version="1.0"?>), | ||
* returns a object (eg: { version: "1.0" }). | ||
*/ | ||
function getPrologFromString(sXmlProlog) { | ||
var matches = sXmlProlog.match(rXmlPrologAttributes); | ||
var oReturn = {}; | ||
for (var i = 0; i < matches.length; i++) { | ||
var currentAttribute = matches[i].split('='); | ||
oReturn[currentAttribute[0]] = currentAttribute[1].replace(/["]/g, ''); | ||
} | ||
|
||
return oReturn; | ||
}; | ||
|
||
/** | ||
* Helper function: given an object representing a xml prolog (eg: { version: "1.0", encoding: "UTF-8", standalone: "true" }), | ||
* returns a string (eg: <?xml version="1.0" encoding="UTF-8" standalone="true"?>). | ||
*/ | ||
function getPrologFromObject(oProlog) { | ||
var ret = []; | ||
for (var prop in oProlog) { | ||
if (oProlog.hasOwnProperty(prop)) { | ||
ret.push(prop + '="' + oProlog[prop] + '"'); | ||
} | ||
} | ||
|
||
return ret.length ? '<?xml ' + ret.join(' ') + '?>' : ''; | ||
}; | ||
|
||
/** | ||
* Helper function: check for empty object | ||
*/ | ||
function isEmptyObject(obj) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true && JSON.stringify(obj) === JSON.stringify({}); | ||
}; | ||
|
||
/** | ||
* Helper function: clone a object | ||
*/ | ||
function cloneObject(obj) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this one needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually not needed... I will change de default value to undefined. |
||
return JSON.parse(JSON.stringify(obj)); | ||
}; | ||
|
||
function parseText(sValue) { | ||
if (!opts.parseValues) { | ||
return sValue; | ||
|
@@ -253,7 +308,12 @@ | |
if (isNodeJs) { | ||
oParentEl.setAttribute(sName.slice(1), vValue); | ||
} | ||
// do nothing: special handling of xml namespaces is done via createElementNS() | ||
// do nothing: special handling of xml namespaces is done via createElementNS() | ||
} else if (sName === opts.attrPrefix + 'xmlProlog') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should really exist outside the |
||
// store xmlProlog | ||
if (vValue) { | ||
oXmlProlog = cloneObject(vValue); | ||
} | ||
} else if (sName.charAt(0) === opts.attrPrefix) { | ||
oParentEl.setAttribute(sName.slice(1), vValue); | ||
} else if (vValue.constructor === Array) { | ||
|
@@ -289,7 +349,14 @@ | |
} | ||
this.xmlToJs = this.build = function(oXMLParent, nVerbosity /* optional */ , bFreeze /* optional */ , bNesteAttributes /* optional */ ) { | ||
var _nVerb = arguments.length > 1 && typeof nVerbosity === 'number' ? nVerbosity & 3 : /* put here the default verbosity level: */ 1; | ||
return createObjTree(oXMLParent, _nVerb, bFreeze || false, arguments.length > 3 ? bNesteAttributes : _nVerb === 3); | ||
var oTree = createObjTree(oXMLParent, _nVerb, bFreeze || false, arguments.length > 3 ? bNesteAttributes : _nVerb === 3); | ||
|
||
if (!isEmptyObject(oXmlProlog)) { | ||
oTree[opts.attrPrefix + 'xmlProlog'] = cloneObject(oXmlProlog); | ||
oXmlProlog = {}; | ||
} | ||
|
||
return oTree; | ||
}; | ||
|
||
this.jsToXml = this.unbuild = function(oObjTree, sNamespaceURI /* optional */ , sQualifiedName /* optional */ , oDocumentType /* optional */ ) { | ||
|
@@ -304,15 +371,33 @@ | |
DOMParser = new xmlDom.DOMParser(); | ||
} | ||
|
||
if (rXmlProlog.test(xmlStr)) { | ||
oXmlProlog = getPrologFromString(xmlStr.match(rXmlProlog)[0]); | ||
} | ||
|
||
return DOMParser.parseFromString(xmlStr, 'application/xml'); | ||
}; | ||
|
||
this.xmlToString = function(xmlObj) { | ||
var sReturn = ''; | ||
|
||
if (typeof xmlObj.xml !== 'undefined') { | ||
return xmlObj.xml; | ||
sReturn = xmlObj.xml; | ||
} else { | ||
return (new xmlDom.XMLSerializer()).serializeToString(xmlObj); | ||
sReturn = (new xmlDom.XMLSerializer()).serializeToString(xmlObj); | ||
} | ||
|
||
// if the original string has a prolog or forceXmlProlog option is true, we include a new one or replace the existent | ||
if (!isEmptyObject(oXmlProlog) || opts.forceXmlProlog) { | ||
if (rXmlProlog.test(sReturn)) { | ||
// replace the prolog | ||
sReturn = sReturn.replace(rXmlProlog, (getPrologFromObject(oXmlProlog) || opts.defaultXmlProlog)); | ||
} else { | ||
sReturn = (getPrologFromObject(oXmlProlog) || opts.defaultXmlProlog) + sReturn; | ||
} | ||
} | ||
|
||
return sReturn; | ||
}; | ||
|
||
this.stringToJs = function(str) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these attributes could also be quoted in single quotes
'
according to the specsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake... that's easy to fix.