smartxml: changing TextNode text emits nodeTextChange event
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 24 Oct 2013 08:45:20 +0000 (10:45 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 24 Oct 2013 08:45:20 +0000 (10:45 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 53eefed..6e00a7d 100644 (file)
@@ -211,14 +211,22 @@ $.extend(TextNode.prototype, DocumentNode.prototype, {
 
     setText: function(text) {
         this.nativeNode.data = text;
 
     setText: function(text) {
         this.nativeNode.data = text;
+        this.triggerTextChangeEvent();
     },
 
     appendText: function(text) {
         this.nativeNode.data = this.nativeNode.data + text;
     },
 
     appendText: function(text) {
         this.nativeNode.data = this.nativeNode.data + text;
+        this.triggerTextChangeEvent();
     },
 
     prependText: function(text) {
         this.nativeNode.data = text + this.nativeNode.data;
     },
 
     prependText: function(text) {
         this.nativeNode.data = text + this.nativeNode.data;
+        this.triggerTextChangeEvent();
+    },
+
+    triggerTextChangeEvent: function() {
+        var event = new events.ChangeEvent('nodeTextChange', {node: this});
+        this.document.trigger('change', event);
     }
 });
 
     }
 });
 
index b33ef1f..15fe394 100644 (file)
@@ -166,6 +166,18 @@ describe('smartxml', function() {
             textNode.setText('Cat');
             expect(textNode.getText()).to.equal('Cat');
         });
             textNode.setText('Cat');
             expect(textNode.getText()).to.equal('Cat');
         });
+
+        it('emits nodeTextChange', function() {
+            var node = elementNodeFromXML('<div>Alice</div>'),
+                textNode = node.contents()[0],
+                spy = sinon.spy();
+
+            textNode.document.on('change', spy);
+            textNode.setText('Cat');
+
+            var event = spy.args[0][0];
+            expect(event.type).to.equal('nodeTextChange');
+        });
     });
 
     describe('Manipulations', function() {
     });
 
     describe('Manipulations', function() {