From 232e5bd6f1b93f27e227a291b7913867e7d76be7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 22 Apr 2014 13:54:03 +0200 Subject: [PATCH] smartxml: ElementNode.getLastTextNode --- src/smartxml/smartxml.js | 16 ++++++++++++++++ src/smartxml/smartxml.test.js | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index e2cdc9b..3ee49b7 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -223,6 +223,22 @@ $.extend(ElementNode.prototype, { return node && (node.nativeNode === this.nativeNode || node._$.parents().index(this._$) !== -1); }, + getLastTextNode: function() { + var contents = this.contents(), + toret; + + contents.reverse().some(function(node) { + if(node.nodeType === Node.TEXT_NODE) { + toret = node; + return true; + } + toret = node.getLastTextNode(); + return !!toret; + }); + + return toret; + }, + toXML: function() { var wrapper = $('
'); wrapper.append(this._getXMLDOMToDump()); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 5d1ca3e..a994f05 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -219,6 +219,21 @@ describe('smartxml', function() { expect(event.meta.oldVal).to.equal('value1'); }); }); + + describe('Searching for the last child text node', function() { + [ + '
xxx
last
', + '
last
', + '
xxx
last
' + ].forEach(function(xml, i) { + var example = 'example ' + i; + it('returns last child text node ' + example + ')', function() { + var doc = getDocumentFromXML(xml), + lastTextNode = doc.root.getLastTextNode(); + expect(lastTextNode.getText()).to.equal('last', example); + }); + }); + }); }); describe('Basic TextNode properties', function() { -- 2.20.1