wlxml: Node.hasChild(query)
[fnpeditor.git] / src / wlxml / wlxml.js
index 4e1ddc6..f370223 100644 (file)
@@ -3,8 +3,9 @@ define([
     'libs/underscore',
     'smartxml/smartxml',
     'smartxml/transformations',
-    'wlxml/extensions/metadata/metadata'
-], function($, _, smartxml, transformations, metadataExtension) {
+    'wlxml/extensions/metadata/metadata',
+    'wlxml/extensions/comments/comments'
+], function($, _, smartxml, transformations, metadataExtension, commentExtension) {
     
 'use strict';
 
@@ -12,16 +13,16 @@ define([
 
 
 var WLXMLDocumentNodeMethods =  {
-    isInside: function(klass) {
-        var parent = this.getParent(klass);
+    isInside: function(query) {
+        var parent = this.getParent(query);
         return !!parent;
     },
-    getParent: function(klass) {
+    getParent: function(query) {
         /* globals Node */
         var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
             toret;
         me.concat(this.parents()).some(function(node) {
-            if(node.is(klass)) {
+            if(node.is(query)) {
                 toret = node;
                 return true;
             }
@@ -58,7 +59,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);
+        }
     });
 };
 
@@ -72,14 +75,26 @@ $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementN
     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);
+    },
+    hasChild: function(query) {
+        return this.contents().some(function(child) {
+            return child.is(query);
+        }.bind(this));
     },
     getMetaAttributes: function() {
         var toret = new AttributesList(),
@@ -199,12 +214,14 @@ var WLXMLTextNode = function() {
     smartxml.TextNode.apply(this, arguments);
 };
 WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
-$.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods);
+$.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods, {
+    is: function() { return false; }
+});
 
 var WLXMLDocument = function(xml, options) {
     this.classMethods = {};
     this.classTransformations = {};
-    smartxml.Document.call(this, xml, [metadataExtension]);
+    smartxml.Document.call(this, xml, [metadataExtension, commentExtension]);
     this.options = options;
 };
 
@@ -380,7 +397,8 @@ return {
         return this.WLXMLDocumentFromXML(xml).root;
     },
 
-    WLXMLDocument: WLXMLDocument
+    WLXMLDocument: WLXMLDocument,
+    getClassHierarchy: getClassLists
 };
 
 });
\ No newline at end of file