Merge "Organise Flat/ElementLinearData methods and tests"

This commit is contained in:
jenkins-bot 2013-12-10 17:56:45 +00:00 committed by Gerrit Code Review
commit 04377b5cac
6 changed files with 131 additions and 103 deletions

View file

@ -550,6 +550,7 @@ class VisualEditorHooks {
've/test/dm/ve.dm.ModelRegistry.test.js',
've/test/dm/ve.dm.MetaList.test.js',
've/test/dm/ve.dm.Model.test.js',
've/test/dm/lineardata/ve.dm.FlatLinearData.test.js',
've/test/dm/lineardata/ve.dm.ElementLinearData.test.js',
've/test/dm/lineardata/ve.dm.MetaLinearData.test.js',
've-mw/test/dm/ve.dm.mwExample.js',

View file

@ -239,25 +239,6 @@ ve.dm.ElementLinearData.prototype.isStructuralOffset = function ( offset, unrest
);
};
/**
* Check for 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 (backwards).
*
* @method
* @returns {boolean} At least one elements exists in data
*/
ve.dm.ElementLinearData.prototype.containsElementData = function () {
var i = this.getLength();
while ( i-- ) {
if ( this.getData( i ).type !== undefined ) {
return true;
}
}
return false;
};
/**
* Check for non-content elements in data.
*

View file

@ -46,6 +46,25 @@ ve.dm.FlatLinearData.prototype.isElementData = function ( offset ) {
return ve.dm.LinearData.static.isElementData( this.getData( offset ) );
};
/**
* Check for elements in 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 (backwards).
*
* @method
* @returns {boolean} At least one elements exists in data
*/
ve.dm.FlatLinearData.prototype.containsElementData = function () {
var i = this.getLength();
while ( i-- ) {
if ( this.isElementData( i ) ) {
return true;
}
}
return false;
};
/**
* Checks if data at a given offset is an open element.
* @method

View file

@ -664,90 +664,6 @@ QUnit.test( 'isStructuralOffset', function ( assert ) {
}
} );
QUnit.test( 'isElementData', 1, function ( assert ) {
var i,
data = new ve.dm.ElementLinearData( new ve.dm.IndexValueStore(), [
{ 'type': 'heading' },
'a',
{ 'type': 'image' },
{ 'type': '/image' },
'b',
'c',
{ 'type': '/heading' },
{ 'type': 'paragraph' },
{ 'type': '/paragraph' },
{ 'type': 'preformatted' },
{ 'type': 'image' },
{ 'type': '/image' },
{ 'type': '/preformatted' },
{ 'type': 'list' },
{ 'type': 'listItem' },
{ 'type': '/listItem' },
{ 'type': '/list' },
{ 'type': 'alienBlock' },
{ 'type': '/alienBlock' }
] ),
cases = [
{ 'msg': 'left of document', 'expected': true },
{ 'msg': 'begining of content branch', 'expected': false },
{ 'msg': 'left of non-text inline leaf', 'expected': true },
{ 'msg': 'inside non-text inline leaf', 'expected': true },
{ 'msg': 'right of non-text inline leaf', 'expected': false },
{ 'msg': 'between characters', 'expected': false },
{ 'msg': 'end of content branch', 'expected': true },
{ 'msg': 'between content branches', 'expected': true },
{ 'msg': 'inside emtpy content branch', 'expected': true },
{ 'msg': 'between content branches', 'expected': true },
{ 'msg': 'begining of content branch, left of inline leaf', 'expected': true },
{ 'msg': 'inside content branch with non-text leaf', 'expected': true },
{ 'msg': 'end of content branch, right of inline leaf', 'expected': true },
{ 'msg': 'between content, non-content branches', 'expected': true },
{ 'msg': 'between parent, child branches, descending', 'expected': true },
{ 'msg': 'inside empty non-content branch', 'expected': true },
{ 'msg': 'between parent, child branches, ascending', 'expected': true },
{ 'msg': 'between non-content branch, non-content leaf', 'expected': true },
{ 'msg': 'inside non-content leaf', 'expected': true },
{ 'msg': 'right of document', 'expected': false }
];
QUnit.expect( data.getLength() + 1 );
for ( i = 0; i < cases.length; i++ ) {
assert.strictEqual( data.isElementData( i ), cases[i].expected, cases[i].msg );
}
} );
QUnit.test( 'containsElementData', 1, function ( assert ) {
var i, data,
cases = [
{
'msg': 'simple paragraph',
'data': [{ 'type': 'paragraph' }, 'a', { 'type': '/paragraph' }],
'expected': true
},
{
'msg': 'plain text',
'data': ['a', 'b', 'c'],
'expected': false
},
{
'msg': 'annotated text',
'data': [['a', { '{"type:"bold"}': { 'type': 'bold' } } ]],
'expected': false
},
{
'msg': 'non-text leaf',
'data': ['a', { 'type': 'image' }, { 'type': '/image' }, 'c'],
'expected': true
}
];
QUnit.expect( cases.length );
for ( i = 0; i < cases.length; i++ ) {
data = new ve.dm.ElementLinearData( new ve.dm.IndexValueStore(), cases[i].data );
assert.strictEqual(
data.containsElementData(), cases[i].expected, cases[i].msg
);
}
} );
QUnit.test( 'isContentData', 1, function ( assert ) {
var i, data,
cases = [
@ -1480,3 +1396,15 @@ QUnit.test( 'sanitize', function ( assert ) {
}
}
} );
// TODO: ve.dm.ElementLinearData.static.compareUnannotated
// TODO: ve.dm.ElementLinearData#getAnnotationIndexesFromOffset
// TODO: ve.dm.ElementLinearData#setAnnotationsAtOffset
// TODO: ve.dm.ElementLinearData#getCharacterData
// TODO: ve.dm.ElementLinearData#getAnnotatedRangeFromSelection
// TODO: ve.dm.ElementLinearData#getNearestContentOffset
// TODO: ve.dm.ElementLinearData#getUsedStoreValues
// TODO: ve.dm.ElementLinearData#remapStoreIndexes
// TODO: ve.dm.ElementLinearData#remapInternalListIndexes
// TODO: ve.dm.ElementLinearData#remapInternalListKeys
// TODO: ve.dm.ElementLinearData#cloneElements

View file

@ -0,0 +1,98 @@
/*!
* VisualEditor FlatLinearData tests.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.dm.FlatLinearData' );
/* Tests */
QUnit.test( 'isElementData', 1, function ( assert ) {
var i,
data = new ve.dm.FlatLinearData( new ve.dm.IndexValueStore(), [
{ 'type': 'heading' },
'a',
{ 'type': 'image' },
{ 'type': '/image' },
'b',
'c',
{ 'type': '/heading' },
{ 'type': 'paragraph' },
{ 'type': '/paragraph' },
{ 'type': 'preformatted' },
{ 'type': 'image' },
{ 'type': '/image' },
{ 'type': '/preformatted' },
{ 'type': 'list' },
{ 'type': 'listItem' },
{ 'type': '/listItem' },
{ 'type': '/list' },
{ 'type': 'alienBlock' },
{ 'type': '/alienBlock' }
] ),
cases = [
{ 'msg': 'left of document', 'expected': true },
{ 'msg': 'begining of content branch', 'expected': false },
{ 'msg': 'left of non-text inline leaf', 'expected': true },
{ 'msg': 'inside non-text inline leaf', 'expected': true },
{ 'msg': 'right of non-text inline leaf', 'expected': false },
{ 'msg': 'between characters', 'expected': false },
{ 'msg': 'end of content branch', 'expected': true },
{ 'msg': 'between content branches', 'expected': true },
{ 'msg': 'inside emtpy content branch', 'expected': true },
{ 'msg': 'between content branches', 'expected': true },
{ 'msg': 'begining of content branch, left of inline leaf', 'expected': true },
{ 'msg': 'inside content branch with non-text leaf', 'expected': true },
{ 'msg': 'end of content branch, right of inline leaf', 'expected': true },
{ 'msg': 'between content, non-content branches', 'expected': true },
{ 'msg': 'between parent, child branches, descending', 'expected': true },
{ 'msg': 'inside empty non-content branch', 'expected': true },
{ 'msg': 'between parent, child branches, ascending', 'expected': true },
{ 'msg': 'between non-content branch, non-content leaf', 'expected': true },
{ 'msg': 'inside non-content leaf', 'expected': true },
{ 'msg': 'right of document', 'expected': false }
];
QUnit.expect( data.getLength() + 1 );
for ( i = 0; i < cases.length; i++ ) {
assert.strictEqual( data.isElementData( i ), cases[i].expected, cases[i].msg );
}
} );
QUnit.test( 'containsElementData', 1, function ( assert ) {
var i, data,
cases = [
{
'msg': 'simple paragraph',
'data': [{ 'type': 'paragraph' }, 'a', { 'type': '/paragraph' }],
'expected': true
},
{
'msg': 'plain text',
'data': ['a', 'b', 'c'],
'expected': false
},
{
'msg': 'annotated text',
'data': [['a', { '{"type:"bold"}': { 'type': 'bold' } } ]],
'expected': false
},
{
'msg': 'non-text leaf',
'data': ['a', { 'type': 'image' }, { 'type': '/image' }, 'c'],
'expected': true
}
];
QUnit.expect( cases.length );
for ( i = 0; i < cases.length; i++ ) {
data = new ve.dm.FlatLinearData( new ve.dm.IndexValueStore(), cases[i].data );
assert.strictEqual(
data.containsElementData(), cases[i].expected, cases[i].msg
);
}
} );
// TODO: ve.dm.FlatLinearData#getType
// TODO: ve.dm.FlatLinearData#isOpenElementData
// TODO: ve.dm.FlatLinearData#isCloseElementData

View file

@ -248,6 +248,7 @@
<script src="dm/ve.dm.ModelRegistry.test.js"></script>
<script src="dm/ve.dm.MetaList.test.js"></script>
<script src="dm/ve.dm.Model.test.js"></script>
<script src="dm/lineardata/ve.dm.FlatLinearData.test.js"></script>
<script src="dm/lineardata/ve.dm.ElementLinearData.test.js"></script>
<script src="dm/lineardata/ve.dm.MetaLinearData.test.js"></script>
<script src="ce/ve.ce.test.js"></script>