smartxml: Doument.findSiblingParents
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 18 Nov 2013 14:59:50 +0000 (15:59 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 18 Nov 2013 14:59:50 +0000 (15:59 +0100)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 9a402cf..ffbe4c2 100644 (file)
@@ -399,6 +399,25 @@ $.extend(Document.prototype, Backbone.Events, {
         return wrapper;
     },
 
+    getSiblingParents: function(params) {
+        var parents1 = [params.node1].concat(params.node1.parents()).reverse(),
+            parents2 = [params.node2].concat(params.node2.parents()).reverse(),
+            noSiblingParents = null;
+
+        if(parents1.length === 0 || parents2.length === 0 || !(parents1[0].sameNode(parents2[0]))) {
+            return noSiblingParents;
+        }
+
+        var i;
+        for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
+            if(parents1[i].sameNode(parents2[i])) {
+                continue;
+            }
+            break;
+        }
+        return {node1: parents1[i], node2: parents2[i]};
+    },
+
     _wrapText: function(params) {
         params = _.extend({textNodeIdx: 0}, params);
         if(typeof params.textNodeIdx === 'number') {
@@ -447,6 +466,11 @@ $.extend(Document.prototype, Backbone.Events, {
             wrapperElement.after({text: suffixOutside});
         }
         return wrapperElement;
+    },
+
+    trigger: function() {
+        //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : ''));
+        Backbone.Events.trigger.apply(this, arguments);
     }
 });
 
index 00c3ee9..db9777f 100644 (file)
@@ -506,6 +506,32 @@ describe('smartxml', function() {
                 expect(parents).to.eql([b,a]);
             });
         });
+
+        describe('finding sibling parents of two elements', function() {
+            it('returns elements themself if they have direct common parent', function() {
+                var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
+                    wrappingDiv = doc.root.contents()[0],
+                    divA = wrappingDiv.contents()[0],
+                    divB = wrappingDiv.contents()[1];
+
+                var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
+
+                expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
+                expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
+            });
+
+            it('returns sibling parents - example 1', function() {
+                var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
+                    aliceText = doc.root.contents()[0],
+                    span = doc.root.contents()[1],
+                    spanText = span.contents()[0];
+
+                var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
+
+                expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
+                expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
+            });
+        });
     });
 
     describe('Serializing document to WLXML', function() {