smartxml: Better check for document being successfulfy parsed
[fnpeditor.git] / src / smartxml / smartxml.js
index cfae7c7..b449d82 100644 (file)
@@ -17,12 +17,21 @@ var DocumentNode = function(nativeNode, document) {
         throw new Error('undefined document for a node');
     }
     this.document = document;
+    this.object = {};
     this._setNativeNode(nativeNode);
 
 };
 
 $.extend(DocumentNode.prototype, {
 
+    getProperty: function(propName) {
+        var toret = this.object[propName];
+        if(toret && _.isFunction(toret)) {
+            toret = toret.call(this);
+        }
+        return toret;
+    },
+
     transform: function(Transformation, args) {
         var transformation = new Transformation(this.document, this, args);
         return this.document.transform(transformation);
@@ -153,10 +162,14 @@ $.extend(DocumentNode.prototype, {
     },
 
     getIndex: function() {
+        var parent;
+
         if(this.isRoot()) {
             return 0;
         }
-        return this.parent().indexOf(this);
+
+        parent = this.parent();
+        return parent ? parent.indexOf(this) : undefined;
     },
 
     getNearestElementNode: function() {
@@ -286,7 +299,7 @@ $.extend(TextNode.prototype, {
 
 var parseXML = function(xml) {
     var toret = $($.trim(xml));
-    if(!toret.length) {
+    if(toret.length !== 1) {
         throw new Error('Unable to parse XML: ' + xml);
     }
     return toret[0];