Add ve.dm.Document.isContentData()

Some of the replacement code was assuming that "does not contain
elements" and "is content" were the same. They're not any more, because
we have content nodes (like image) now, so I need a separate function
to distinguish between these cases.

Change-Id: I206ccdf082b7baddf99d382eb3cdd77ea34fb479
This commit is contained in:
Catrope 2012-05-31 06:19:34 -07:00
parent e92db86046
commit f6ca37926d
2 changed files with 54 additions and 0 deletions

View file

@ -356,6 +356,29 @@ ve.dm.Document.containsElementData = function( data ) {
return false;
};
/**
* Checks for non-content elements in document data.
*
* This method assumes that any value that has a type property that's a string is an element object.
* Elements are discovered by iterating through the entire data array.
*
* @static
* @method
* @param {Array} data Document data
* @returns {Boolean} True if all elements in data are content elements
*/
ve.dm.Document.isContentData = function( data ) {
for ( var i = 0, len = data.length; i < len; i++ ) {
if ( data[i].type !== undefined &&
data[i].type.charAt( 0 ) !== '/' &&
!ve.dm.factory.isNodeContent( data[i].type )
) {
return false;
}
}
return true;
};
/* Methods */
/**

View file

@ -759,6 +759,37 @@ test( 'containsElementData', 1, function() {
}
} );
test( 'isContentData', 1, function() {
var cases = [
{
'msg': 'simple paragraph',
'data': [{ 'type': 'paragraph' }, 'a', { 'type': '/paragraph' }],
'expected': false
},
{
'msg': 'plain text',
'data': ['a', 'b', 'c'],
'expected': true
},
{
'msg': 'annotated text',
'data': [['a', { '{"type:"bold"}': { 'type': 'bold' } } ]],
'expected': true
},
{
'msg': 'non-text leaf',
'data': ['a', { 'type': 'image' }, { 'type': '/image' }, 'c'],
'expected': true
}
];
expect( cases.length );
for ( var i = 0; i < cases.length; i++ ) {
strictEqual(
ve.dm.Document.isContentData( cases[i].data ), cases[i].expected, cases[i].msg
);
}
} );
test( 'rebuildNodes', function() {
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) ),
documentNode = doc.getDocumentNode();