From: Aleksander Ɓukasz Date: Mon, 25 Nov 2013 14:03:48 +0000 (+0100) Subject: smartxml: cloning a node X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/0032858d24ed6dba11f8943072cd6db475a2dd17?hp=65bc0821986afa202fcb6b7e8e77661f951b808d smartxml: cloning a node --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 6f4677b..a583848 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -36,6 +36,10 @@ $.extend(DocumentNode.prototype, { this._$ = $(nativeNode); }, + clone: function() { + return this.document.createDocumentNode(this._$.clone(true, true)[0]); + }, + isRoot: function() { return this.document.root.sameNode(this); }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 822243d..e4dd1a7 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -61,6 +61,22 @@ describe('smartxml', function() { }); }); + describe('DocumentNode', function() { + it('can be cloned', function() { + var doc = getDocumentFromXML('
Alice
'), + text = doc.root.contents()[0], + clone, suffix; + + [doc.root, text].forEach(function(node) { + suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')'; + clone = node.clone(); + expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix); + expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix); + expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix); + }); + }); + }); + describe('Basic ElementNode properties', function() { it('exposes node contents', function() { var node = elementNodeFromXML('Sometextis here'),