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, {
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');
+ });
});
});