diff options
-rw-r--r-- | lib/webcals/sax/dav_response.js | 100 | ||||
-rw-r--r-- | test/webcals/sax/dav_response_test.js | 21 |
2 files changed, 86 insertions, 35 deletions
diff --git a/lib/webcals/sax/dav_response.js b/lib/webcals/sax/dav_response.js index 5b3e0b7..7cf78af 100644 --- a/lib/webcals/sax/dav_response.js +++ b/lib/webcals/sax/dav_response.js @@ -21,6 +21,41 @@ } }); + var HrefHandler = Base.create({ + name: 'href', + + //don't add text only elements + //to the stack as objects + onopentag: null, + onclosetag: null, + + onopentag: function() { + if (this.currentTag.handler === this.handler) { + this.stack.push(this.current); + this.current = null; + } + }, + + onclosetag: function() { + var current = this.currentTag; + var data; + + if (current.handler === this.handler) { + data = this.current; + + this.current = this.stack.pop(); + this.current[current.local] = data; + } + }, + + ontext: function(data) { + if (this.currentTag.local === 'href') { + this.current = data; + } + } + + }); + var HttpStatusHandler = TextHandler.create({ name: 'status', @@ -71,13 +106,21 @@ handles: { 'DAV:/href': TextHandler, 'DAV:/status': HttpStatusHandler, - 'DAV:/resourcetype': ArrayHandler + 'DAV:/resourcetype': ArrayHandler, + 'DAV:/principal-URL': HrefHandler }, onopentag: function(data, handler) { //orphan if (data.tagSpec === 'DAV:/propstat') { + //blank slate propstat + if (!('propstat' in this.current)) { + this.current['propstat'] = {}; + } + this.stack.push(this.current); + + //contents will be copied over later. return this.current = {}; } @@ -85,21 +128,24 @@ }, oncomplete: function() { - var parent = this.stack[this.stack.length - 1]; + var propstat = this.stack[this.stack.length - 1]; + propstat = propstat.propstat; var key; - - //console.log(this.current); - - for (key in this.current.prop) { - if (this.current.prop.hasOwnProperty(key)) { - parent[key] = { - status: this.current.status, - value: this.current.prop[key] - } + var status = this.current.status; + var props = this.current.prop; + + delete this.current.status; + delete this.current.prop; + + for (key in props) { + if (props.hasOwnProperty(key)) { + propstat[key] = { + status: status, + value: props[key] + }; } } - }, - + } }); var Response = Base.create({ @@ -109,23 +155,23 @@ 'DAV:/propstat': PropStatHandler }, - //onopentag: function(data, handler) { - //if (data.tagSpec === 'DAV:/response') { - //this.stack.push(this.current); - //return this.current = {}; - //} + onopentag: function(data, handler) { + if (data.tagSpec === 'DAV:/response') { + this.stack.push(this.current); + return this.current = {}; + } - //handler._super.onopentag.call(this, data, handler._super); - //}, + handler._super.onopentag.call(this, data, handler._super); + }, - //oncomplete: function() { - //var parent; + oncomplete: function() { + var parent; - //if (this.current.href) { - //parent = this.stack[this.stack.length - 1]; - //parent[this.current.href] = this.current.propstat; - //} - //} + if (this.current.href) { + parent = this.stack[this.stack.length - 1]; + parent[this.current.href] = this.current.propstat; + } + } }); diff --git a/test/webcals/sax/dav_response_test.js b/test/webcals/sax/dav_response_test.js index 8e84e6b..36616b6 100644 --- a/test/webcals/sax/dav_response_test.js +++ b/test/webcals/sax/dav_response_test.js @@ -34,11 +34,11 @@ suite('webcals/sax/base', function() { }); expected = { - '/calendar/user': { + '/calendar/user/': { 'principal-URL': { status: '200', - value: '/calendar/user/' + value: '/calendar/pinc/' }, resourcetype: { @@ -47,6 +47,11 @@ suite('webcals/sax/base', function() { 'principal', 'collection' ] + }, + + 'current-user-principal': { + status: '404', + value: {} } }, @@ -60,12 +65,12 @@ suite('webcals/sax/base', function() { test('output', function(done) { subject.once('complete', function(data) { - console.log(JSON.stringify(data)); - //assert.deepEqual( - //data, expected, - //"expected \n '" + JSON.stringify(data) + "'\n to equal \n '" + - //JSON.stringify(expected) + '\n"' - //); + console.log(JSON.stringify(data.multistatus)); + assert.deepEqual( + data.multistatus, expected, + "expected \n '" + JSON.stringify(data.multistatus) + + "'\n to equal \n '" + JSON.stringify(expected) + '\n"' + ); done(); }); subject.write(xml).close(); |