diff options
Diffstat (limited to 'lib/caldav/sax/base.js')
-rw-r--r-- | lib/caldav/sax/base.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/caldav/sax/base.js b/lib/caldav/sax/base.js new file mode 100644 index 0000000..a823aad --- /dev/null +++ b/lib/caldav/sax/base.js @@ -0,0 +1,73 @@ +(function(module, ns) { + + var Base = { + + name: 'base', + + tagField: 'local', + + /** + * Creates a new object with base as its prototype. + * Adds ._super to object as convenience prop to access + * the parents functions. + * + * @param {Object} obj function overrides. + * @return {Object} new object. + */ + create: function(obj) { + var key; + var child = Object.create(this); + + child._super = this; + + for (key in obj) { + if (obj.hasOwnProperty(key)) { + child[key] = obj[key]; + } + } + + return child; + }, + + onopentag: function(data, handler) { + var current = this.current; + var name = data[handler.tagField]; + + this.stack.push(this.current); + + if (name in current) { + var next = {}; + + if (!(current[name] instanceof Array)) { + current[name] = [current[name]]; + } + + current[name].push(next); + + this.current = next; + } else { + this.current = current[name] = {}; + } + }, + + ontext: function(data) { + this.current.value = data; + }, + + onclosetag: function() { + this.current = this.stack.pop(); + }, + + onend: function() { + this.emit('complete', this.root); + } + }; + + module.exports = Base; + +}.apply( + this, + (this.Caldav) ? + [Caldav('sax/base'), Caldav] : + [module, require('../caldav')] +)); |