Permalink
Browse files

Added code to normalize tag names and wrote tests

  • Loading branch information...
1 parent 048f20b commit cd43a5ddaed4cd227561cab9a8c3fc7774f1d20d @neopunisher neopunisher committed Oct 7, 2012
Showing with 17 additions and 6 deletions.
  1. +1 −0 README.md
  2. +3 −1 lib/xml2js.js
  3. +2 −4 src/xml2js.coffee
  4. +5 −0 test/fixtures/sample.xml
  5. +6 −1 test/xml2js.test.coffee
@@ -118,6 +118,7 @@ value})``. Possible options are:
* `explicitCharkey` (default: `false`)
* `trim` (default: `false`): Trim the whitespace at the beginning and end of
text nodes.
+ * `normalizeTags` (default: `false`): Normalize all tag names to lowercase.
* `normalize` (default: `false`): Trim whitespaces inside text nodes.
* `explicitRoot` (default: `true`): Set this if you want to get the root
node in the resulting object.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -30,6 +30,7 @@ exports.defaults =
explicitCharkey: false
trim: false
normalize: false
+ normalizeTags: false
attrkey: "$"
charkey: "_"
explicitArray: true
@@ -94,17 +95,14 @@ class exports.Parser extends events.EventEmitter
obj[attrkey][key] = node.attributes[key]
# need a place to store the node name
- obj["#name"] = node.name
+ obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
stack.push obj
@saxParser.onclosetag = =>
obj = stack.pop()
nodeName = obj["#name"]
delete obj["#name"]
- if @options.normalizeTags
- nodeName = nodeName.toLowerCase()
-
s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/)
View
@@ -23,6 +23,11 @@
<item><subitem>Foo.</subitem><subitem>Bar.</subitem></item>
</arraytest>
<emptytest/>
+ <tagcasetest>
+ <tAg>something</tAg>
+ <TAG>something else</TAG>
+ <tag>something third</tag>
+ </tagcasetest>
<ordertest>
<one>1</one>
<two>2</two>
View
@@ -59,7 +59,8 @@ module.exports =
equ r.sample.listtest[0].item[0].subitem[2], 'Foo(3)'
equ r.sample.listtest[0].item[0].subitem[3], 'Foo(4)'
equ r.sample.listtest[0].item[1], 'Qux.'
- equ r.sample.listtest[0].item[2], 'Quux.')
+ equ r.sample.listtest[0].item[2], 'Quux.'
+ equ r.sample.tagcasetest.length, 3)
'test parse with explicitCharkey': skeleton(explicitCharkey: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
@@ -122,6 +123,10 @@ module.exports =
'test invalid empty XML file': skeleton(__xmlString: ' ', (r) ->
equ r, null)
+ 'test enabled normalizeTags': skeleton(normalizeTags: true, (r) ->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.sample.tagcasetest.length, 1)
+
'test parse with custom char and attribute object keys': skeleton(attrkey: 'attrobj', charkey: 'charobj', (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].attrobj.desc, 'Test for CHARs'

0 comments on commit cd43a5d

Please sign in to comment.