X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/e1acb388e008f83e89976e3779330d2a268f525e..4b2e5f26a2bf27ea2cf41c0d4818e5b85785cd67:/src/wlxml/wlxml.js?ds=inline diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 034312d..5194c19 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -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() { @@ -49,7 +68,7 @@ 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') || ''; }, @@ -59,8 +78,12 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { 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 +198,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 +218,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');