Add zero-length text nodes to empty content nodes

This is needed to make the results of certain transactions' tree sync
round-trip cleanly through the ve.dm.Document constructor

Change-Id: I2ab0758ec6bd7afba5b6645c7330f9fa2d45205d
This commit is contained in:
Catrope 2012-06-06 15:33:42 -07:00
parent ae3d31d447
commit 02b19f337f
2 changed files with 17 additions and 1 deletions

View file

@ -119,6 +119,15 @@ ve.dm.Document = function( data, parentDocument ) {
// This can only happen if we got unbalanced data
throw '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

@ -2,7 +2,7 @@ module( 've.dm.Document' );
/* Tests */
test( 'constructor', 3, function() {
test( 'constructor', 4, function() {
var doc = new ve.dm.Document( ve.dm.example.data );
deepEqual(
ve.example.getNodeTreeSummary( doc.getDocumentNode() ),
@ -27,6 +27,13 @@ test( 'constructor', 3, function() {
ve.example.getNodeTreeSummary( new ve.dm.DocumentNode( [ new ve.dm.TextNode( 4 ) ] ) ),
'plain text input is handled correctly'
);
doc = new ve.dm.Document( [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ] );
deepEqual(
ve.example.getNodeTreeSummary( doc.getDocumentNode() ),
ve.example.getNodeTreeSummary( new ve.dm.DocumentNode( [ new ve.dm.ParagraphNode( [ new ve.dm.TextNode( 0 ) ] ) ] ) ),
'empty paragraph gets a zero-length text node'
);
} );
test( 'getData', 1, function() {