wlxml: Allow for setting non-function properties via extension
[fnpeditor.git] / src / wlxml / wlxml.js
index 4f6e356..bbd6233 100644 (file)
@@ -11,6 +11,25 @@ define([
 /* globals Node */
 
 
+var WLXMLDocumentNodeMethods =  {
+    isInside: function(query) {
+        var parent = this.getParent(query);
+        return !!parent;
+    },
+    getParent: function(query) {
+        /* globals Node */
+        var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
+            toret;
+        me.concat(this.parents()).some(function(node) {
+            if(node.is(query)) {
+                toret = node;
+                return true;
+            }
+        });
+        return toret;
+    },
+};
+
 var AttributesList = function() {};
 AttributesList.prototype = Object.create({});
 AttributesList.prototype.keys = function() {
@@ -39,7 +58,9 @@ var installObject = function(instance, klass) {
     });
     instance.object = Object.create(_.extend({}, methods, transformations));
     _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
-        instance.object[key] = _.bind(instance.object[key], instance);
+        if(_.isFunction(instance.object[key])) {
+            instance.object[key] = _.bind(instance.object[key], instance);
+        }
     });
 };
 
@@ -49,18 +70,25 @@ var WLXMLElementNode = function(nativeNode, document) {
 };
 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
 
-$.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
+$.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, {
     getClass: function() {
         return this.getAttr('class') || '';
     },
+    getClassHierarchy: function() {
+        return getClassLists(this.getClass());
+    },
     setClass: function(klass) {
         if(klass !== this.klass) {
             installObject(this, klass);
             return this.setAttr('class', klass);
         }
     },
-    is: function(klass) {
-        return this.getClass().substr(0, klass.length) === klass;
+    is: function(query) {
+        if(typeof query === 'string') {
+            query = {klass: query};
+        }
+        return (_.isUndefined(query.klass) || this.getClass().substr(0, query.klass.length) === query.klass) &&
+               (_.isUndefined(query.tagName) || this.getTagName() === query.tagName);
     },
     getMetaAttributes: function() {
         var toret = new AttributesList(),
@@ -175,6 +203,13 @@ var WLXMLDocumentNode = function() {
 };
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
+
+var WLXMLTextNode = function() {
+    smartxml.TextNode.apply(this, arguments);
+};
+WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
+$.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods);
+
 var WLXMLDocument = function(xml, options) {
     this.classMethods = {};
     this.classTransformations = {};
@@ -188,6 +223,7 @@ var formatter_prefix = '_wlxml_formatter_';
 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
 $.extend(WLXMLDocument.prototype, {
     ElementNodeFactory: WLXMLElementNode,
+    TextNodeFactory: WLXMLTextNode,
     loadXML: function(xml) {
         smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
         this.trigger('contentSet');
@@ -343,14 +379,18 @@ var wlxmlClasses = {
 
 
 return {
-    WLXMLDocumentFromXML: function(xml, options) {
+    WLXMLDocumentFromXML: function(xml, options, Factory) {
         options = _.extend({wlxmlClasses: wlxmlClasses}, options);
-        return new WLXMLDocument(xml, options);
+        Factory = Factory || WLXMLDocument;
+        return new Factory(xml, options);
     },
 
     WLXMLElementNodeFromXML: function(xml) {
         return this.WLXMLDocumentFromXML(xml).root;
-    }
+    },
+
+    WLXMLDocument: WLXMLDocument,
+    getClassHierarchy: getClassLists
 };
 
 });
\ No newline at end of file