From 0032858d24ed6dba11f8943072cd6db475a2dd17 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 25 Nov 2013 15:03:48 +0100 Subject: [PATCH 1/1] smartxml: cloning a node --- src/smartxml/smartxml.js | 4 ++++ src/smartxml/smartxml.test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) 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'), -- 2.20.1