From 9fc9629a92ebc396c5f3f3cbe3dc8a7acc53f9c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 8 Jul 2013 22:33:35 +0200 Subject: [PATCH 1/1] Some white space in the wlxml source handling rules --- modules/documentCanvas/canvas/canvas.js | 28 +++++++++++++++++++ modules/documentCanvas/canvas/canvas.test3.js | 21 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index e0c8213..4326655 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -9,6 +9,34 @@ define([ var Canvas = function(xml) { xml = $.parseXML(xml); this.d = xml !== null ? $(xml.childNodes[0]) : null; + if(this.d) { + var wrapper = $('
'); + wrapper.append(this.d); + wrapper.find(':not(iframe)').addBack().contents() + .filter(function() {return this.nodeType === Node.TEXT_NODE}) + .each(function() { + + var el = $(this); + + // TODO: use DocumentElement API + var spanParent = el.parent().prop('tagName') === 'span', + spanBefore = el.prev().length > 0 && $(el.prev()[0]).prop('tagName') === 'span', + spanAfter = el.next().length > 0 && $(el.next()[0]).prop('tagName') === 'span'; + + if(spanParent || spanBefore || spanAfter) { + var startSpace = /\s/g.test(this.data.substr(0,1)); + var endSpace = /\s/g.test(this.data.substr(-1)) && this.data.length > 1; + var trimmed = $.trim(this.data); + this.data = (startSpace && (spanParent || spanBefore) ? ' ' : '') + + trimmed + + (endSpace && (spanParent || spanAfter) ? ' ' : ''); + + } else { + this.data = $.trim(this.data); + } + }); + this.d.unwrap(); + }; }; $.extend(Canvas.prototype, { diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 4a7c661..0ffa9d9 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -132,6 +132,27 @@ describe('Canvas', function() { expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement); }); }); + + it('trims white space from the beginning and the end of the block elements', function() { + var c = canvas.fromXML('
Alice has a cat
'); + expect(c.doc().children()[0].getText()).to.equal('Alice '); + expect(c.doc().children()[2].getText()).to.equal(' a cat'); + }); + + it('normalizes string of white characters to one space at the inline element boundries', function() { + var c = canvas.fromXML(' Alice has a cat '); + expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat '); + }); + + it('normalizes string of white characters to one space before inline element', function() { + var c = canvas.fromXML('
Alice has a cat
'); + expect(c.doc().children()[0].getText()).to.equal('Alice has '); + }); + + it('normalizes string of white characters to one space after inline element', function() { + var c = canvas.fromXML('
Alice has a cat
'); + expect(c.doc().children()[2].getText()).to.equal(' cat'); + }); }); }); -- 2.20.1