From 02b19f337f0a045fe3ba1c8b21e8ca016763c8b8 Mon Sep 17 00:00:00 2001 From: Catrope Date: Wed, 6 Jun 2012 15:33:42 -0700 Subject: [PATCH] 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 --- modules/ve2/dm/ve.dm.Document.js | 9 +++++++++ tests/ve2/dm/ve.dm.Document.test.js | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/ve2/dm/ve.dm.Document.js b/modules/ve2/dm/ve.dm.Document.js index f66d19d254..6e2553ecf2 100644 --- a/modules/ve2/dm/ve.dm.Document.js +++ b/modules/ve2/dm/ve.dm.Document.js @@ -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 ); } diff --git a/tests/ve2/dm/ve.dm.Document.test.js b/tests/ve2/dm/ve.dm.Document.test.js index 611b362836..c0b3469346 100644 --- a/tests/ve2/dm/ve.dm.Document.test.js +++ b/tests/ve2/dm/ve.dm.Document.test.js @@ -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() {