From: Aleksander Ɓukasz Date: Wed, 20 Nov 2013 15:16:55 +0000 (+0100) Subject: Fix setting zero width space X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/7a3c737d962ff2b2e2653fd8ed0108e92c7fe0d5?ds=inline;hp=b95e2e87d96a747f503990f6f315ade26fe88acc Fix setting zero width space --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index b0d461b..48f03de 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -65,8 +65,10 @@ $.extend(Canvas.prototype, { 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); } }); }); diff --git a/src/editor/modules/documentCanvas/canvas/canvas.test.js b/src/editor/modules/documentCanvas/canvas/canvas.test.js index 5c36f90..10a2376 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.test.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.test.js @@ -19,6 +19,9 @@ var getCanvasFromXML = function(xml) { 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() { @@ -29,6 +32,21 @@ describe('new Canvas', 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('
Alice
'), + 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; diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index cd6b3d8..89ffce9 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -527,8 +527,13 @@ $.extend(DocumentTextElement.prototype, { 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 diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js index 8bd05a5..9269ce1 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js @@ -63,8 +63,12 @@ var handlers = { 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); } };