Merge "Revert "No longer create zero-length text nodes""

This commit is contained in:
Catrope 2012-10-12 18:04:22 +00:00 committed by Gerrit Code Review
commit 9ca5da44ee
4 changed files with 34 additions and 18 deletions

View file

@ -106,6 +106,14 @@ ve.dm.Document = function VeDmDocument( data, parentDocument ) {
throw new Error( 'Unbalanced input passed to document' );
}
if ( children.length === 0 &&
ve.dm.nodeFactory.canNodeContainContent(
currentNode.getType()
)
) {
// Content nodes cannot be childless, add a zero-length text node
children.push( new ve.dm.TextNode( 0 ) );
}
// Attach the children to the node
ve.batchSplice( currentNode, 0, 0, children );
}

View file

@ -224,26 +224,21 @@ ve.dm.TransactionProcessor.processors.replace = function ( op ) {
),
'leaves'
);
firstNode = selection[0].node;
if (
// Replacement is text only
!removeHasStructure && !insertHasStructure &&
// firstNode is a text node
firstNode.getType() === 'text' &&
// We're not blanking the text node
firstNode.getLength() + insert.length - remove.length > 0
) {
// Resize firstNode
this.synchronizer.pushResize( firstNode, insert.length - remove.length );
} else {
if ( removeHasStructure || insertHasStructure ) {
// Replacement is not exclusively text
// Rebuild all covered nodes
range = new ve.Range(
selection[0].nodeOuterRange.start, selection[selection.length - 1].nodeOuterRange.end
selection[0].nodeRange.start, selection[selection.length - 1].nodeRange.end
);
this.synchronizer.pushRebuild( range,
new ve.Range( range.start + this.adjustment,
range.end + this.adjustment + insert.length - remove.length )
);
} else {
// Text-only replacement
// Queue a resize for this node
node = selection[0].node;
this.synchronizer.pushResize( node, insert.length - remove.length );
}
// Advance the cursor
this.cursor += insert.length;
@ -275,7 +270,16 @@ ve.dm.TransactionProcessor.processors.replace = function ( op ) {
prevCursor + opRemove.length - this.adjustment
), 'siblings' );
for ( i = 0; i < selection.length; i++ ) {
affectedRanges.push( selection[i].nodeOuterRange );
// .nodeRange is the inner range, we need the
// outer range (including opening and closing)
if ( selection[i].node.isWrapped() ) {
affectedRanges.push( new ve.Range(
selection[i].nodeRange.start - 1,
selection[i].nodeRange.end + 1
) );
} else {
affectedRanges.push( selection[i].nodeRange );
}
}
}
// Walk through the remove and insert data

View file

@ -9,7 +9,7 @@ QUnit.module( 've.ce.TextNode' );
/* Tests */
QUnit.test( 'getHtml', function ( assert ) {
QUnit.test( 'getHtml', 22, function ( assert ) {
var i, len, cases;
cases = [
@ -213,6 +213,11 @@ QUnit.test( 'getHtml', function ( assert ) {
],
'html': 'abc<i><u><b>d</b></u></i><u><b>ef</b></u>ghi'
},
{
// [ ]
'data': [{ 'type': 'paragraph' },{ 'type': '/paragraph' }],
'html': ''
},
{
// [ ]
'data': [{ 'type': 'paragraph' },' ',{ 'type': '/paragraph' }],
@ -269,7 +274,6 @@ QUnit.test( 'getHtml', function ( assert ) {
'html': '&nbsp; &nbsp; &nbsp;A'
}
];
expect( cases.length );
for ( i = 0, len = cases.length; i < len; i++ ) {
ve.dm.example.preprocessAnnotations( cases[i].data );
assert.equal(

View file

@ -34,8 +34,8 @@ QUnit.test( 'constructor', 4, function ( assert ) {
doc = new ve.dm.Document( [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ] );
assert.equalNodeTree(
doc.getDocumentNode(),
new ve.dm.DocumentNode( [ new ve.dm.ParagraphNode( [ ] ) ] ),
'empty paragraph does not get a zero-length text node'
new ve.dm.DocumentNode( [ new ve.dm.ParagraphNode( [ new ve.dm.TextNode( 0 ) ] ) ] ),
'empty paragraph gets a zero-length text node'
);
} );