mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
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:
parent
e92db86046
commit
f6ca37926d
|
@ -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 */
|
||||
|
||||
/**
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue