Skip to content

Commit

Permalink
TooTallNate#66 - fixed empty keys and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Goldis committed Jan 26, 2016
1 parent 421c7f2 commit f360d7d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function shouldIgnoreNode (node) {
* @api private
*/
function isEmptyNode(node){
if(!node.childNodes || node.childNodes.length == 0) {
if(!node.childNodes || node.childNodes.length === 0) {
return true;
} else {
return false;
Expand Down Expand Up @@ -135,7 +135,7 @@ function parsePlistXML (node) {
new_arr = [];
for (i=0; i < node.childNodes.length; i++) {
// ignore comment nodes (text)
if (!shouldIgnoreNode(node.childNodes[i]) && !isEmptyNode(node)) {
if (!shouldIgnoreNode(node.childNodes[i])) {
new_arr.push( parsePlistXML(node.childNodes[i]));
}
}
Expand Down Expand Up @@ -172,8 +172,8 @@ function parsePlistXML (node) {
// TODO: what should we do with text types? (CDATA sections)

} else if (node.nodeName === 'key') {
if(isEmptyNode(node)) return null;
return node.childNodes[0].nodeValue;

} else if (node.nodeName === 'string') {
res = '';
for (d=0; d < node.childNodes.length; d++) {
Expand Down
49 changes: 44 additions & 5 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ var assert = require('assert');
var parse = require('../').parse;
var multiline = require('multiline');

function isEmpty(o){
for(var i in o){
if(o.hasOwnProperty(i)){
return false;
}
}
return true;
}

describe('plist', function () {

describe('parse()', function () {
Expand Down Expand Up @@ -48,6 +57,36 @@ describe('plist', function () {
assert.strictEqual(parsed, 3.14);
});

it('should parse an empty <key/> in a dictionary', function() {
var xml = multiline(function() {/*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key />
<string />
</dict>
</plist>
*/});
var parsed = parse(xml);
assert.ok(isEmpty(parsed));
});

it('should parse an empty <key></key> in a dictionary', function() {
var xml = multiline(function() {/*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key></key>
<string />
</dict>
</plist>
*/});
var parsed = parse(xml);
assert.ok(isEmpty(parsed));
});

it('should parse a <date> node into a Date', function () {
var xml = multiline(function () {/*
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -80,10 +119,10 @@ describe('plist', function () {
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<data>4pyTIMOgIGxhIG
1v
ZG
U=</data>
</plist>
Expand Down Expand Up @@ -251,9 +290,9 @@ int main(int argc, char *argv[])
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand Down

0 comments on commit f360d7d

Please sign in to comment.