observer.observe(canvas.d[0], config);
canvas.publisher('contentChanged');
- var textElement = canvas.getDocumentElement(mutation.target);
- textElement.data('wlxmlNode').setText(mutation.target.data);
+ var textElement = canvas.getDocumentElement(mutation.target),
+ toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
+
+ textElement.data('wlxmlNode').setText(toSet);
}
});
});
return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
};
+var wait = function(callback, timeout) {
+ return window.setTimeout(callback, timeout || 0.5);
+}
describe('new Canvas', function() {
it('abc', function() {
});
})
+describe('Handling empty text nodes', function() {
+ it('puts zero width space into node with about to be remove text', function(done) {
+ var c = getCanvasFromXML('<section>Alice</section>'),
+ textElement = c.doc().children()[0];
+ textElement.setText('');
+
+ /* Wait for MutationObserver to kick in. */
+ wait(function() {
+ expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
+ expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
+ done();
+ });
+ });
+});
+
describe('Cursor', function() {
var getSelection;
prependText: function(text) {
this.dom().contents()[0].data = text + this.dom().contents()[0].data;
},
- getText: function() {
- return this.dom().text().replace(utils.unicode.ZWS, '');
+ getText: function(options) {
+ options = _.extend({raw: false}, options || {});
+ var toret = this.dom().text();
+ if(!options.raw) {
+ toret = toret.replace(utils.unicode.ZWS, '');
+ }
+ return toret;
},
isEmpty: function() {
// Having at least Zero Width Space is guaranteed be Content Observer
canvasNode.detach();
},
nodeTextChange: function(event) {
- var canvasElement = utils.findCanvasElement(event.meta.node);
- canvasElement.setText(event.meta.node.getText());
+ var canvasElement = utils.findCanvasElement(event.meta.node),
+ toSet = event.meta.node.getText();
+ if(toSet === '') {
+ toSet = utils.unicode.ZWS;
+ }
+ canvasElement.setText(toSet);
}
};