Comparing version 1.0.10 to 1.0.11
{ | ||
"name": "pixl-xml", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "A simple module for parsing and composing XML.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -168,2 +168,30 @@ # Overview | ||
### preserveWhitespace | ||
If you want to preserve whitespace before and after text inside elements, set the `preserveWhitespace` flag to a true value. Note that this has no effect on attributes (whitespace is always preserved there), nor does it effect whitespace |between| complex elements. Example: | ||
```js | ||
var xml_string = '<?xml version="1.0"?> <Document> ' + | ||
' <Simple> Hello </Simple> ' + | ||
' <Node Key=" Value "> Complex </Node> ' + | ||
' </Document> '; | ||
var doc = XML.parse( xml_string, { preserveWhitespace: true } ); | ||
console.log( doc ); | ||
``` | ||
With the `preserveWhitespace` flag set to true, this would produce the following object: | ||
```js | ||
{ | ||
"Simple": " Hello ", | ||
"Node": { | ||
"Key": " Value ", | ||
"_Data": " Complex " | ||
} | ||
} | ||
``` | ||
Notice that the whitespace before/after all the opening and closing tags has no effect on the parsed object. It only has effect |inside| elements that also contain a text value. | ||
## Composing XML | ||
@@ -188,3 +216,3 @@ | ||
Note that elements and attributes may lose their original ordering, as hashes have an undefined key order. However, to keep things consistent, they are both alphabetically sorted when serialized. | ||
Note that elements and attributes may lose their original ordering, as hashes have an undefined key order. However, to keep things consistent, they are both alphabetically sorted when serialized. See [Preserve Sort Order](#preserve-sort-order) below for a possible workaround. | ||
@@ -218,2 +246,12 @@ If you are composing an XML document which has the document root node preserved (see [preserveDocumentNode](#preserveDocumentNode) above), simply omit the name parameter, and only pass in the object. Example: | ||
### Preserve Sort Order | ||
Most modern JavaScript engines including Node.js seem to magically preserve hash key order, although this goes against the ECMAScript specification. If you want to take your chances and skip the alphabetic sort, and instead rely on natural key order, pass `false` as the 6th parameter when composing: | ||
```js | ||
var xml_string = XML.stringify( doc, 'Document', 0, "\t", "\n", false ); | ||
``` | ||
This will render elements and attributes in whatever order they come out of their hashes, which is up to your JavaScript runtime engine. | ||
# Object-Oriented API | ||
@@ -220,0 +258,0 @@ |
16
xml.js
@@ -79,2 +79,3 @@ /* | ||
XML.prototype.preserveAttributes = false; | ||
XML.prototype.preserveWhitespace = false; | ||
XML.prototype.lowerCase = false; | ||
@@ -119,3 +120,3 @@ | ||
if (typeof(branch[this.dataKey]) != 'undefined') branch[this.dataKey] += ' '; else branch[this.dataKey] = ''; | ||
branch[this.dataKey] += trim(decode_entities(before)); | ||
branch[this.dataKey] += !this.preserveWhitespace ? trim(decode_entities(before)) : decode_entities(before); | ||
} | ||
@@ -132,3 +133,3 @@ | ||
if (typeof(branch[this.dataKey]) != 'undefined') branch[this.dataKey] += ' '; else branch[this.dataKey] = ''; | ||
branch[this.dataKey] += trim(decode_entities(tag)); | ||
branch[this.dataKey] += !this.preserveWhitespace ? trim(decode_entities(tag)) : decode_entities(tag); | ||
} // cdata | ||
@@ -489,3 +490,3 @@ else { | ||
var compose_xml = exports.stringify = function compose_xml(node, name, indent, indent_string, eol) { | ||
var compose_xml = exports.stringify = function compose_xml(node, name, indent, indent_string, eol, sort) { | ||
// Compose node into XML including attributes | ||
@@ -495,2 +496,3 @@ // Recurse for child nodes | ||
if (typeof(eol) == 'undefined') eol = "\n"; | ||
if (typeof(sort) == 'undefined') sort = true; | ||
var xml = ""; | ||
@@ -527,3 +529,3 @@ | ||
has_attribs = 1; | ||
var sorted_keys = hash_keys_to_array(node["_Attribs"]).sort(); | ||
var sorted_keys = sort ? hash_keys_to_array(node["_Attribs"]).sort() : hash_keys_to_array(node["_Attribs"]); | ||
for (var idx = 0, len = sorted_keys.length; idx < len; idx++) { | ||
@@ -546,3 +548,3 @@ var key = sorted_keys[idx]; | ||
var sorted_keys = hash_keys_to_array(node).sort(); | ||
var sorted_keys = sort ? hash_keys_to_array(node).sort() : hash_keys_to_array(node); | ||
for (var idx = 0, len = sorted_keys.length; idx < len; idx++) { | ||
@@ -552,3 +554,3 @@ var key = sorted_keys[idx]; | ||
// recurse for node, with incremented indent value | ||
xml += compose_xml( node[key], key, indent + 1, indent_string, eol ); | ||
xml += compose_xml( node[key], key, indent + 1, indent_string, eol, sort ); | ||
} // not _Attribs key | ||
@@ -569,3 +571,3 @@ } // foreach key | ||
// recurse for node in array with same indent | ||
xml += compose_xml( node[idx], name, indent, indent_string, eol ); | ||
xml += compose_xml( node[idx], name, indent, indent_string, eol, sort ); | ||
} | ||
@@ -572,0 +574,0 @@ } // array of nodes |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33811
523
475