Some white space in the wlxml source handling rules
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 20:33:35 +0000 (22:33 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 20:33:35 +0000 (22:33 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js

index e0c8213..4326655 100644 (file)
@@ -9,6 +9,34 @@ define([
 var Canvas = function(xml) {
     xml = $.parseXML(xml);
     this.d = xml !== null ? $(xml.childNodes[0]) : null;
 var Canvas = function(xml) {
     xml = $.parseXML(xml);
     this.d = xml !== null ? $(xml.childNodes[0]) : null;
+    if(this.d) {
+        var wrapper = $('<div>');
+        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, {
 };
 
 $.extend(Canvas.prototype, {
index 4a7c661..0ffa9d9 100644 (file)
@@ -132,6 +132,27 @@ describe('Canvas', function() {
                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
                     });
                 });
                         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('<section> Alice <span>has</span> a cat </section>');
+                    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('<span>   Alice has a cat   </span>');
+                    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('<div>Alice has  <span>a cat</span></div>');
+                    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('<div>Alice has <span>a</span>  cat</div>');
+                    expect(c.doc().children()[2].getText()).to.equal(' cat');
+                });
             });
         });
 
             });
         });