From 7a55de7fa64363dcc4bdcf31dabe2e155018597a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 17 Jun 2014 13:29:50 +0200 Subject: [PATCH] wlxml: context root --- src/wlxml/wlxml.js | 11 +++++++++++ src/wlxml/wlxml.test.js | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 5925ee6..c38810d 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -29,6 +29,17 @@ var WLXMLDocumentNodeMethods = { }); return toret; }, + isContextRoot: function() { + var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [], + toret = false; + me.concat(this.parents()).some(function(node) { + if(_.isFunction(node.object.isContextRoot) && node.object.isContextRoot(this)) { + toret = true; + return true; + } + }.bind(this)); + return toret; + } }; var getClassLists = function(klassName) { diff --git a/src/wlxml/wlxml.test.js b/src/wlxml/wlxml.test.js index 2b495a8..cf8aa74 100644 --- a/src/wlxml/wlxml.test.js +++ b/src/wlxml/wlxml.test.js @@ -239,6 +239,25 @@ describe('WLXMLDocument', function() { expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '2'); }); }); + + describe('Declaring context roots', function() { + it('allows extensions declaring a node as a context root', function() { + var doc = getDocumentFromXML('
'); + doc.registerExtension({wlxmlClass: {a: {methods: { + isContextRoot: function(node) { + return node.getClass() === 'b'; + } + }}}}); + + var divA = doc.root.contents()[0], + divB = divA.contents()[0], + divC = divB.contents()[0]; + + expect(divC.isContextRoot()).to.equal(false, 'c is not a context root'); + expect(divB.isContextRoot()).to.equal(true, 'b is a context root'); + expect(divA.isContextRoot()).to.equal(false, 'a is not a context root'); + }); + }); }); }); \ No newline at end of file -- 2.20.1