From 9ebf1ada325abec0fe83789b04f6a8af576818a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 19 Nov 2013 10:09:52 +0100 Subject: [PATCH] smartxml: refactor inserting methods --- src/smartxml/smartxml.js | 46 +++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index ffbe4c2..a357977 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -11,6 +11,16 @@ define([ var TEXT_NODE = Node.TEXT_NODE; +var INSERTION = function(implementation) { + var toret = function(node) { + var insertion = this.getNodeInsertion(node); + implementation.call(this, insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; + }; + return toret; +}; + var DocumentNode = function(nativeNode, document) { if(!document) { throw new Error('undefined document for a node'); @@ -54,19 +64,13 @@ $.extend(DocumentNode.prototype, { return parents; }, - after: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.after(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + after: INSERTION(function(nativeNode) { + return this._$.after(nativeNode); + }), - before: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.before(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + before: INSERTION(function(nativeNode) { + return this._$.before(nativeNode); + }), wrapWith: function(node) { node = node instanceof ElementNode ? node : this.document.createElementNode(node); @@ -185,19 +189,13 @@ $.extend(ElementNode.prototype, { return toret; }, - append: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.append(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + append: INSERTION(function(nativeNode) { + this._$.append(nativeNode); + }), - prepend: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.prepend(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + prepend: INSERTION(function(nativeNode) { + this._$.prepend(nativeNode); + }), unwrapContent: function() { var parent = this.parent(); -- 2.20.1