From 7f5e612121a78074e505ad6cf593671ca60e889c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 17 Feb 2014 10:22:34 +0100 Subject: [PATCH] wlxml: methods for testing for existence and fetching ancestors with a specific class --- src/wlxml/wlxml.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 034312d..4e1ddc6 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -11,6 +11,25 @@ define([ /* globals Node */ +var WLXMLDocumentNodeMethods = { + isInside: function(klass) { + var parent = this.getParent(klass); + return !!parent; + }, + getParent: function(klass) { + /* globals Node */ + var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [], + toret; + me.concat(this.parents()).some(function(node) { + if(node.is(klass)) { + 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') || ''; }, @@ -175,6 +194,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 +214,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'); -- 2.20.1