plist 0.4.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/plist.js CHANGED
@@ -1,297 +1,23 @@
1
- ;(function (exports, DOMParser, xmlbuilder) {
2
- // Checks if running in a non-browser environment
3
- var inNode = typeof window === 'undefined' ? true : false
4
- , utf8_to_b64
5
- , b64_to_utf8;
6
1
 
7
- // this library runs in browsers and nodejs, set up functions accordingly
8
- if (inNode) {
9
- exports.parseFile = function (filename, callback) {
10
- console.warn('parseFile is deprecated. Please use parseFileSync instead.');
11
- var fs = require('fs');
12
- var inxml = fs.readFileSync(filename, 'utf8');
13
- exports.parseString(inxml, callback);
14
- }
2
+ var i;
15
3
 
16
- exports.parseFileSync = function (filename) {
17
- var fs = require('fs');
18
- var inxml = fs.readFileSync(filename, 'utf8');
19
- return exports.parseStringSync(inxml);
20
- }
4
+ /**
5
+ * Parser functions.
6
+ */
21
7
 
22
- // set up base64 encode/decode functions
23
- utf8_to_b64= function ( str ) {
24
- return new Buffer(str).toString('base64');
25
- }
26
- b64_to_utf8 = function ( str ) {
27
- return new Buffer(str, 'base64').toString('utf8');
28
- }
29
- } else {
30
- // we're in a browser context, find the DOMParser
31
- if (typeof window.DOMParser != 'undefined') {
32
- DOMParser = function(xmlStr) {
33
- return ( new window.DOMParser() ).parseFromString(xmlStr, 'text/xml');
34
- };
35
- } else if (typeof window.ActiveXObject != 'undefined' &&
36
- new window.ActiveXObject('Microsoft.XMLDOM')) {
37
- DOMParser = function(xmlStr) {
38
- var xmlDoc = new window.ActiveXObject('Microsoft.XMLDOM');
39
- xmlDoc.async = 'false';
40
- xmlDoc.loadXML(xmlStr);
41
- return xmlDoc;
42
- };
43
- } else {
44
- throw new Error('No XML parser found');
45
- }
8
+ var parserFunctions = require('./parse');
9
+ for (i in parserFunctions) exports[i] = parserFunctions[i];
46
10
 
47
- // set up base64 encode/decode functions
48
- if (typeof window.btoa == 'undefined') {
49
- throw new Error('No base64 support found');
50
- // TODO: shim btoa and atob if not present (ie < 10)
51
- //http://stackoverflow.com/questions/246801/how-can-you-encode-to-base64-using-javascript/247261#247261
52
- } else {
53
- utf8_to_b64= function ( str ) {
54
- return window.btoa(unescape(encodeURIComponent( str )));
55
- }
56
-
57
- b64_to_utf8 = function ( str ) {
58
- return decodeURIComponent(escape(window.atob( str )));
59
- }
60
- }
11
+ /**
12
+ * Builder functions.
13
+ */
61
14
 
62
- }
15
+ var builderFunctions = require('./build');
16
+ for (i in builderFunctions) exports[i] = builderFunctions[i];
63
17
 
64
- exports.parseString = function (xml, callback) {
65
- console.warn('parseString is deprecated. Please use parseStringSync instead.');
66
- var doc, error, plist;
67
- try {
68
- doc = new DOMParser().parseFromString(xml);
69
- plist = parsePlistXML(doc.documentElement);
70
- } catch(e) {
71
- error = e;
72
- }
73
- callback(error, plist);
74
- }
18
+ /**
19
+ * Add Node.js-specific functions (they're deprecated…).
20
+ */
75
21
 
76
- exports.parseStringSync = function (xml) {
77
- var doc = new DOMParser().parseFromString(xml);
78
- var plist;
79
- if (doc.documentElement.nodeName !== 'plist') {
80
- throw new Error('malformed document. First element should be <plist>');
81
- }
82
- plist = parsePlistXML(doc.documentElement);
83
-
84
- // if the plist is an array with 1 element, pull it out of the array
85
- if(isArray(plist) && plist.length == 1) {
86
- plist = plist[0];
87
- }
88
- return plist;
89
- }
90
-
91
- /**
92
- * convert an XML based plist document into a JSON representation
93
- *
94
- * @param object xml_node current XML node in the plist
95
- * @return built up JSON object
96
- */
97
- function parsePlistXML(node) {
98
- var i, new_obj, key, val, new_arr;
99
- if (!node)
100
- return null;
101
-
102
- if (node.nodeName === 'plist') {
103
- new_arr = [];
104
- for (i=0;i < node.childNodes.length;i++) {
105
- // ignore comment nodes (text)
106
- if (node.childNodes[i].nodeType !== 3) {
107
- new_arr.push( parsePlistXML(node.childNodes[i]));
108
- }
109
- }
110
- return new_arr;
111
- }
112
- else if(node.nodeName === 'dict') {
113
- new_obj = {};
114
- key = null;
115
- for (i=0;i < node.childNodes.length;i++) {
116
- // ignore comment nodes (text)
117
- if (node.childNodes[i].nodeType !== 3) {
118
- if (key === null) {
119
- key = parsePlistXML(node.childNodes[i]);
120
- } else {
121
- new_obj[key] = parsePlistXML(node.childNodes[i]);
122
- key = null;
123
- }
124
- }
125
- }
126
- return new_obj;
127
- }
128
- else if(node.nodeName === 'array') {
129
- new_arr = [];
130
- for (i=0;i < node.childNodes.length;i++) {
131
- // ignore comment nodes (text)
132
- if (node.childNodes[i].nodeType !== 3) {
133
- res = parsePlistXML(node.childNodes[i]);
134
- if (res) new_arr.push( res );
135
- }
136
- }
137
- return new_arr;
138
- }
139
- else if(node.nodeName === '#text') {
140
- // TODO: what should we do with text types? (CDATA sections)
141
- }
142
- else if(node.nodeName === 'key') {
143
- return node.childNodes[0].nodeValue;
144
- }
145
- else if(node.nodeName === 'string') {
146
- var res = '';
147
- for (var d=0; d < node.childNodes.length; d++)
148
- {
149
- res += node.childNodes[d].nodeValue;
150
- }
151
- return res;
152
- }
153
- else if(node.nodeName === 'integer') {
154
- // parse as base 10 integer
155
- return parseInt(node.childNodes[0].nodeValue, 10);
156
- }
157
- else if(node.nodeName === 'real') {
158
- var res = '';
159
- for (var d=0; d < node.childNodes.length; d++)
160
- {
161
- if(node.childNodes[d].nodeType === 3) {
162
- res += node.childNodes[d].nodeValue;
163
- }
164
- }
165
- return parseFloat(res);
166
- }
167
- else if(node.nodeName === 'data') {
168
- var res = '';
169
- for (var d=0; d < node.childNodes.length; d++)
170
- {
171
- if(node.childNodes[d].nodeType === 3) {
172
- res += node.childNodes[d].nodeValue;
173
- }
174
- }
175
-
176
- // validate that the string is encoded as base64
177
- var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
178
- if (!base64Matcher.test(res.replace(/\s/g,'')) ) {
179
- throw new Error('malformed document. <data> element is not base64 encoded');
180
- }
181
-
182
- // decode base64 data as utf8 string
183
- return b64_to_utf8(res);
184
- }
185
- else if(node.nodeName === 'date') {
186
- return new Date(node.childNodes[0].nodeValue);
187
- }
188
- else if(node.nodeName === 'true') {
189
- return true;
190
- }
191
- else if(node.nodeName === 'false') {
192
- return false;
193
- }
194
- }
195
-
196
-
197
- function ISODateString(d){
198
- function pad(n){return n<10 ? '0'+n : n}
199
- return d.getUTCFullYear()+'-'
200
- + pad(d.getUTCMonth()+1)+'-'
201
- + pad(d.getUTCDate())+'T'
202
- + pad(d.getUTCHours())+':'
203
- + pad(d.getUTCMinutes())+':'
204
- + pad(d.getUTCSeconds())+'Z'
205
- }
206
-
207
- // instanceof is horribly unreliable so we use these hackish but safer checks
208
- // http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray
209
- function isArray(obj) {
210
- return Object.prototype.toString.call(obj) === '[object Array]';
211
- }
212
-
213
- function isDate(obj) {
214
- return Object.prototype.toString.call(obj) === '[object Date]';
215
- }
216
-
217
- function isBoolean(obj) {
218
- return (obj === true || obj === false || toString.call(obj) == '[object Boolean]');
219
- }
220
-
221
- function isNumber(obj) {
222
- return Object.prototype.toString.call(obj) === '[object Number]';
223
- }
224
-
225
- function isObject(obj) {
226
- return Object.prototype.toString.call(obj) === '[object Object]';
227
- }
228
-
229
- function isString(obj) {
230
- return Object.prototype.toString.call(obj) === '[object String]';
231
- }
232
-
233
- /**
234
- * generate an XML plist string from the input object
235
- *
236
- * @param object obj the object to convert
237
- * @return string converted plist
238
- */
239
- exports.build = function(obj) {
240
- var XMLHDR = { 'version': '1.0','encoding': 'UTF-8'}
241
- , XMLDTD = { 'ext': 'PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"'}
242
- , doc = xmlbuilder.create()
243
- , child = doc.begin('plist', XMLHDR, XMLDTD).att('version', '1.0');
244
-
245
- walk_obj(obj, child);
246
- return child.end({pretty: true });
247
- }
248
-
249
- // depth first, recursive traversal of a javascript object. when complete,
250
- // next_child contains a reference to the build XML object.
251
- function walk_obj(next, next_child) {
252
- var tag_type, i, prop;
253
-
254
- if(isArray(next)) {
255
- next_child = next_child.ele('array');
256
- for(i=0 ;i < next.length;i++) {
257
- walk_obj(next[i], next_child);
258
- }
259
- }
260
- else if(isObject(next)) {
261
- next_child = next_child.ele('dict');
262
- for(prop in next) {
263
- if(next.hasOwnProperty(prop)) {
264
- next_child.ele('key').txt(prop);
265
- walk_obj(next[prop], next_child);
266
- }
267
- }
268
- }
269
- else if(isNumber(next)) {
270
- // detect if this is an integer or real
271
- tag_type =(next % 1 === 0) ? 'integer' : 'real';
272
- next_child.ele(tag_type).txt(next.toString());
273
- }
274
- else if(isDate(next)) {
275
- next_child.ele('date').raw(ISODateString(new Date(next)));
276
- }
277
- else if(isBoolean(next)) {
278
- val = next ? 'true' : 'false';
279
- next_child.ele(val);
280
- }
281
- else if(isString(next)) {
282
- //if (str!=obj || str.indexOf("\n")>=0) str = "<![CDATA["+str+"]]>";
283
- var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
284
- if (base64Matcher.test(next.replace(/\s/g,''))) {
285
- // data is base 64 encoded so assume it's a <data> node
286
- next_child.ele('data').raw(utf8_to_b64(next));
287
- } else {
288
- // it's not base 64 encoded, assume it's a <string> node
289
- next_child.ele('string').raw(next);
290
- }
291
- }
292
- };
293
-
294
- })(typeof exports === 'undefined' ? plist = {} : exports, typeof window === 'undefined' ? require('xmldom').DOMParser : null, typeof window === 'undefined' ? require('xmlbuilder') : xmlbuilder)
295
- // the above line checks for exports (defined in node) and uses it, or creates
296
- // a global variable and exports to that. also, if in node, require DOMParser
297
- // node-style, in browser it should already be present
22
+ var nodeFunctions = require('./node');
23
+ for (i in nodeFunctions) exports[i] = nodeFunctions[i];
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "plist",
3
- "description": "Mac OS X Plist parser/builder for NodeJS. Convert a Plist file or string into a native JS object and native JS object into a Plist file.",
4
- "version": "0.4.2",
3
+ "description": "Mac OS X Plist parser/builder for Node.js and browsers",
4
+ "version": "1.1.0",
5
5
  "author": "Nathan Rajlich <nathan@tootallnate.net>",
6
- "contributors": [
6
+ "contributors": [
7
7
  "Hans Huebner <hans.huebner@gmail.com>",
8
8
  "Pierre Metrailler",
9
- "Mike Reinstein <reinstein.mike@gmail.com>"
9
+ "Mike Reinstein <reinstein.mike@gmail.com>",
10
+ "Vladimir Tsvang",
11
+ "Mathieu D'Amours"
10
12
  ],
11
13
  "repository": {
12
14
  "type": "git",
@@ -14,24 +16,26 @@
14
16
  },
15
17
  "keywords": [
16
18
  "apple",
19
+ "browser",
17
20
  "mac",
18
21
  "plist",
19
22
  "parser",
20
23
  "xml"
21
24
  ],
22
- "main": "./lib/plist",
25
+ "main": "lib/plist.js",
23
26
  "dependencies": {
24
- "xmlbuilder" : "0.4.x",
25
- "xmldom" : "0.1.x"
27
+ "base64-js": "0.0.6",
28
+ "xmlbuilder": "2.2.1",
29
+ "xmldom": "0.1.x",
30
+ "util-deprecate": "1.0.0"
26
31
  },
27
32
  "devDependencies": {
28
- "nodeunit" : "0.7.x"
33
+ "browserify": "5.10.1",
34
+ "mocha": "1.18.2",
35
+ "multiline": "0.3.4",
36
+ "zuul": "1.10.2"
29
37
  },
30
38
  "scripts": {
31
- "test": "nodeunit tests"
32
- },
33
- "engines": {
34
- "node": ">= 0.1.100"
35
- },
36
- "devDependencies": {}
39
+ "test": "make test"
40
+ }
37
41
  }
package/test/build.js ADDED
@@ -0,0 +1,133 @@
1
+
2
+ var assert = require('assert');
3
+ var build = require('../').build;
4
+ var multiline = require('multiline');
5
+
6
+ describe('plist', function () {
7
+
8
+ describe('build()', function () {
9
+
10
+ it('should create a plist XML string from a String', function () {
11
+ var xml = build('test');
12
+ assert.strictEqual(xml, multiline(function () {/*
13
+ <?xml version="1.0" encoding="UTF-8"?>
14
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
15
+ <plist version="1.0">
16
+ <string>test</string>
17
+ </plist>
18
+ */}));
19
+ });
20
+
21
+ it('should create a plist XML integer from a whole Number', function () {
22
+ var xml = build(3);
23
+ assert.strictEqual(xml, multiline(function () {/*
24
+ <?xml version="1.0" encoding="UTF-8"?>
25
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
26
+ <plist version="1.0">
27
+ <integer>3</integer>
28
+ </plist>
29
+ */}));
30
+ });
31
+
32
+ it('should create a plist XML real from a fractional Number', function () {
33
+ var xml = build(Math.PI);
34
+ assert.strictEqual(xml, multiline(function () {/*
35
+ <?xml version="1.0" encoding="UTF-8"?>
36
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
37
+ <plist version="1.0">
38
+ <real>3.141592653589793</real>
39
+ </plist>
40
+ */}));
41
+ });
42
+
43
+ it('should create a plist XML date from a Date', function () {
44
+ var xml = build(new Date('2010-02-08T21:41:23Z'));
45
+ assert.strictEqual(xml, multiline(function () {/*
46
+ <?xml version="1.0" encoding="UTF-8"?>
47
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
48
+ <plist version="1.0">
49
+ <date>2010-02-08T21:41:23Z</date>
50
+ </plist>
51
+ */}));
52
+ });
53
+
54
+ it('should create a plist XML date from a Buffer', function () {
55
+ var xml = build(new Buffer('☃'));
56
+ assert.strictEqual(xml, multiline(function () {/*
57
+ <?xml version="1.0" encoding="UTF-8"?>
58
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
59
+ <plist version="1.0">
60
+ <data>4piD</data>
61
+ </plist>
62
+ */}));
63
+ });
64
+
65
+ it('should create a plist XML true from a `true` Boolean', function () {
66
+ var xml = build(true);
67
+ assert.strictEqual(xml, multiline(function () {/*
68
+ <?xml version="1.0" encoding="UTF-8"?>
69
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
70
+ <plist version="1.0">
71
+ <true/>
72
+ </plist>
73
+ */}));
74
+ });
75
+
76
+ it('should create a plist XML false from a `false` Boolean', function () {
77
+ var xml = build(false);
78
+ assert.strictEqual(xml, multiline(function () {/*
79
+ <?xml version="1.0" encoding="UTF-8"?>
80
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
81
+ <plist version="1.0">
82
+ <false/>
83
+ </plist>
84
+ */}));
85
+ });
86
+
87
+ it('should create a plist XML dict from an Object', function () {
88
+ var xml = build({ foo: 'bar' });
89
+ assert.strictEqual(xml, multiline(function () {/*
90
+ <?xml version="1.0" encoding="UTF-8"?>
91
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
92
+ <plist version="1.0">
93
+ <dict>
94
+ <key>foo</key>
95
+ <string>bar</string>
96
+ </dict>
97
+ </plist>
98
+ */}));
99
+ });
100
+
101
+ it('should create a plist XML array from an Array', function () {
102
+ var xml = build([ 1, 'foo', false, new Date(1234) ]);
103
+ assert.strictEqual(xml, multiline(function () {/*
104
+ <?xml version="1.0" encoding="UTF-8"?>
105
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
106
+ <plist version="1.0">
107
+ <array>
108
+ <integer>1</integer>
109
+ <string>foo</string>
110
+ <false/>
111
+ <date>1970-01-01T00:00:01Z</date>
112
+ </array>
113
+ </plist>
114
+ */}));
115
+ });
116
+
117
+ it('should properly encode an empty string', function () {
118
+ var xml = build({ a: '' });
119
+ assert.strictEqual(xml, multiline(function () {/*
120
+ <?xml version="1.0" encoding="UTF-8"?>
121
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
122
+ <plist version="1.0">
123
+ <dict>
124
+ <key>a</key>
125
+ <string></string>
126
+ </dict>
127
+ </plist>
128
+ */}));
129
+ });
130
+
131
+ });
132
+
133
+ });