spanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span',
spanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span';
+ var oldText = this.data,
+ oldLength = this.data.length;
+ var parent = el.parent(),
+ parentContents = parent.contents(),
+ idx = parentContents.index(this),
+ next = idx < parentContents.length - 1 ? parentContents[idx+1] : null;
+
+ var addInfo = function() {
+ if(next) {
+ $(next).data('orig-before', oldText);
+ } else {
+ parent.data('orig-append', oldText);
+ }
+ }
+
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) ? ' ' : '')
+ var newText = (startSpace && (spanParent || spanBefore) ? ' ' : '')
+ trimmed
+ (endSpace && (spanParent || spanAfter) ? ' ' : '');
-
+ if(newText !== oldText) {
+ this.data = newText;
+ addInfo();
+ }
} else {
- var oldText = this.data,
- oldLength = this.data.length;
+
this.data = $.trim(this.data);
if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1)
this.data = ' ';
if(this.data.length === 0) {
- var parent = $(this).parent(),
- parentContents = parent.contents(),
- idx = parentContents.index(this),
- next = idx < parentContents.length - 1 ? parentContents[idx+1] : null;
-
- if(next) {
- $(next).data('orig-before', oldText);
- } else {
- parent.data('orig-append', oldText);
- }
- $(this).remove();
+ addInfo();
+ el.remove();
return true; // continue
}
expect(partsIn).to.deep.equal(partsOut);
});
- it('nests new children elements', function() {
+ it('keeps white space between XML nodes - inline case', function() {
+ var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
+ c = canvas.fromXML(xmlIn),
+ xmlOut = c.toXML();
+
+ var partsIn = xmlIn.split('\n\n\n'),
+ partsOut = xmlOut.split('\n\n\n');
+
+ expect(partsIn).to.deep.equal(partsOut);
+ });
+
+ it('nests new children block elements', function() {
var c = canvas.fromXML('<section></section>');
c.doc().append({tag: 'header'});
expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
});
+
+ it('doesn\'t nest new children inline elements', function() {
+ var c = canvas.fromXML('<section></section>');
+
+ c.doc().append({tag: 'span'});
+
+ var xmlOut = c.toXML();
+ expect(xmlOut).to.equal('<section><span></span></section>');
+ });
+
})
})
});
if(this.data('orig-before') !== undefined) {
toret.append(document.createTextNode(this.data('orig-before')));
- } else if(level) {
+ } else if(level && this.getWlxmlTag() !== 'span') {
toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
}
if(this.data('orig-append') !== undefined) {
node.append(this.data('orig-append'));
//toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
- } else {
+ } else if(this.getWlxmlTag() !== 'span'){
node.append('\n' + (new Array(level * 2 + 1)).join(' '));
}
return toret.contents();
},
append: function(params) {
- this.data('orig-append', undefined);
+ if(params.tag !== 'span')
+ this.data('orig-append', undefined);
return manipulate(this, params, 'append');
},
before: function(params) {