smartxml/wlxml: Merge in document node instances caching
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 18 Jul 2014 09:55:13 +0000 (11:55 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 18 Jul 2014 09:55:13 +0000 (11:55 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js
src/wlxml/wlxml.test.js

index a5a1abc..c0b3c42 100644 (file)
@@ -12,6 +12,8 @@ define([
 /* globals Node */
 
 
+var privateKey = '_smartxml';
+
 var DocumentNode = function(nativeNode, document) {
     if(!document) {
         throw new Error('undefined document for a node');
@@ -48,7 +50,7 @@ $.extend(DocumentNode.prototype, {
         clone.find('*').addBack().each(function() {
             var el = this,
                 clonedData = $(this).data();
-
+            $(el).removeData(privateKey);
             _.pairs(clonedData).forEach(function(pair) {
                 var key = pair[0],
                     value = pair[1];
@@ -180,6 +182,7 @@ $.extend(DocumentNode.prototype, {
 
 var ElementNode = function(nativeNode, document) {
     DocumentNode.call(this, nativeNode, document);
+    $(nativeNode).data(privateKey, {node: this});
 };
 ElementNode.prototype = Object.create(DocumentNode.prototype);
 
@@ -203,7 +206,9 @@ $.extend(ElementNode.prototype, {
         if(key) {
             return this._$.data(key);
         }
-        return this._$.data();
+        var toret = _.clone(this._$.data());
+        delete toret[privateKey];
+        return toret;
     },
 
     getTagName: function() {
@@ -275,6 +280,7 @@ $.extend(ElementNode.prototype, {
 
 var TextNode = function(nativeNode, document) {
     DocumentNode.call(this, nativeNode, document);
+    nativeNode.__smartxmlTextNodeInstance = this;
 };
 TextNode.prototype = Object.create(DocumentNode.prototype);
 
@@ -352,7 +358,15 @@ $.extend(Document.prototype, Backbone.Events, fragments, {
     TextNodeFactory: TextNode,
 
     createDocumentNode: function(from) {
-        if(!(from instanceof Node)) {
+        var cached;
+
+        if(from instanceof Node) {
+            /* globals Text */
+            cached = from instanceof Text ? from.__smartxmlTextNodeInstance : ($(from).data(privateKey) || {}).node;
+            if(cached instanceof DocumentNode) {
+                return cached;
+            }
+        } else {
             if(typeof from === 'string') {
                 from = parseXML(from);
                 this.normalizeXML(from);
index 524c976..1be4cc1 100644 (file)
@@ -620,9 +620,7 @@ describe('smartxml', function() {
         });
 
         it('removes parent-describing sibling nodes of unwrapped node', function() {
-            var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>'),
-                div = doc.root.contents()[0],
-                x = div.contents()[1];
+            var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>');
 
             doc.registerExtension({documentNode: {methods: {
                 object: {
@@ -632,6 +630,9 @@ describe('smartxml', function() {
                 }
             }}});
 
+            var div = doc.root.contents()[0],
+                x = div.contents()[1];
+
             div.unwrapContent();
             expect(doc.root.contents().length).to.equal(2);
             expect(x.isInDocument()).to.be.false;
@@ -691,10 +692,7 @@ describe('smartxml', function() {
             });
 
             it('keeps parent-describing nodes in place', function() {
-                var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>'),
-                    root = doc.root,
-                    x = root.contents()[1],
-                    y = root.contents()[3];
+                var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>');
 
                 doc.registerExtension({documentNode: {methods: {
                     object: {
@@ -705,6 +703,10 @@ describe('smartxml', function() {
                     }
                 }}});
 
+                var root = doc.root,
+                    x = root.contents()[1],
+                    y = root.contents()[3];
+
                 root.wrapText({
                     _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
                     offsetStart: 1,
@@ -765,10 +767,7 @@ describe('smartxml', function() {
             });
 
             it('keeps parent-describing nodes in place', function() {
-                var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>'),
-                    aliceText = section.contents()[0],
-                    x = section.contents()[1],
-                    lastDiv = section.contents()[2];
+                var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>');
 
                 section.document.registerExtension({documentNode: {methods: {
                     object: {
@@ -778,6 +777,10 @@ describe('smartxml', function() {
                     }
                 }}});
 
+                var aliceText = section.contents()[0],
+                    x = section.contents()[1],
+                    lastDiv = section.contents()[2];
+
                 section.document.wrapNodes({
                         node1: aliceText,
                         node2: lastDiv,
@@ -1348,19 +1351,6 @@ describe('smartxml', function() {
 
         beforeEach(function() {
             doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
-            elementNode = doc.root;
-            textNode = doc.root.contents()[0];
-            extension = {};
-            
-            expect(elementNode.testTransformation).to.be.undefined;
-            expect(textNode.testTransformation).to.be.undefined;
-            expect(doc.testTransformation).to.be.undefined;
-            
-            expect(doc.testMethod).to.be.undefined;
-            expect(elementNode.testMethod).to.be.undefined;
-            expect(textNode.testMethod).to.be.undefined;
-            expect(elementNode.elementTestMethod).to.be.undefined;
-            expect(textNode.textTestMethod).to.be.undefined;
         });
 
         it('allows adding method to a document', function() {
@@ -1404,7 +1394,6 @@ describe('smartxml', function() {
 
             doc.registerExtension(extension);
 
-            /* refresh */
             elementNode = doc.root;
             textNode = doc.root.contents()[0];
 
@@ -1440,7 +1429,6 @@ describe('smartxml', function() {
             
             doc.registerExtension(extension);
 
-            /* refresh */
             elementNode = doc.root;
             textNode = doc.root.contents()[0];
             
index 7c69bdb..2b495a8 100644 (file)
@@ -185,17 +185,10 @@ describe('WLXMLDocument', function() {
     });
 
     describe('Extension', function() {
-        var doc, extension, elementNode, textNode, testClassNode;
+        var doc, extension, testClassNode;
 
         beforeEach(function() {
             doc = getDocumentFromXML('<section>Alice<div class="test_class"></div><div class="test_class.a"></div></section>');
-            elementNode = doc.root;
-            textNode = doc.root.contents()[0];
-            testClassNode = doc.root.contents('.test_class');
-            extension = {};
-            
-            expect(testClassNode.object).to.be.undefined;
-
         });
 
         it('allows adding method to an ElementNode of specific class', function() {