From: Aleksander Ɓukasz Date: Tue, 22 Apr 2014 11:54:03 +0000 (+0200) Subject: smartxml: ElementNode.getLastTextNode X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/232e5bd6f1b93f27e227a291b7913867e7d76be7?ds=sidebyside;hp=471df249233a0064cdd3c4efe890536d8b304037 smartxml: ElementNode.getLastTextNode --- 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() {