2012-04-27 22:10:43 +00:00
|
|
|
module( 've.dm.DocumentFragment' );
|
2012-04-27 21:59:52 +00:00
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2012-05-15 07:25:27 +00:00
|
|
|
test( 'constructor', 2, function() {
|
2012-04-30 20:23:46 +00:00
|
|
|
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data );
|
2012-05-15 07:25:27 +00:00
|
|
|
deepEqual(
|
|
|
|
ve.example.getNodeTreeSummary( fragment.getDocumentNode() ),
|
|
|
|
ve.example.getNodeTreeSummary( ve.dm.example.tree ),
|
2012-05-15 05:36:06 +00:00
|
|
|
'node tree matches example data'
|
|
|
|
);
|
2012-05-11 02:28:37 +00:00
|
|
|
raises(
|
|
|
|
function() {
|
|
|
|
fragment = new ve.dm.DocumentFragment( [
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': 'paragraph' }
|
|
|
|
] );
|
|
|
|
},
|
|
|
|
/^Unbalanced input passed to DocumentFragment$/,
|
|
|
|
'unbalanced input causes exception'
|
|
|
|
);
|
2012-04-30 20:23:46 +00:00
|
|
|
} );
|
|
|
|
|
2012-04-27 21:59:52 +00:00
|
|
|
test( 'getData', 1, function() {
|
2012-04-28 00:16:29 +00:00
|
|
|
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data );
|
2012-04-27 21:59:52 +00:00
|
|
|
deepEqual( fragment.getData(), ve.dm.example.data );
|
|
|
|
} );
|
|
|
|
|
2012-05-16 20:33:27 +00:00
|
|
|
test( 'getOffsetMap', 61, function() {
|
2012-04-28 00:16:29 +00:00
|
|
|
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data ),
|
|
|
|
actual = fragment.getOffsetMap(),
|
2012-04-30 18:37:48 +00:00
|
|
|
expected = ve.dm.example.getOffsetMap( fragment.getDocumentNode() );
|
2012-04-28 00:16:29 +00:00
|
|
|
ok( actual.length === expected.length, 'offset map lengths match' );
|
|
|
|
for ( var i = 0; i < actual.length; i++ ) {
|
|
|
|
ok( actual[i] === expected[i], 'reference at offset ' + i );
|
|
|
|
}
|
2012-04-27 21:59:52 +00:00
|
|
|
} );
|
|
|
|
|
2012-05-16 20:33:27 +00:00
|
|
|
test( 'getNodeFromOffset', 60, function() {
|
2012-04-30 18:37:48 +00:00
|
|
|
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data ),
|
|
|
|
root = fragment.getDocumentNode().getRoot(),
|
|
|
|
node,
|
|
|
|
expected = [
|
|
|
|
[], // 0 - document
|
|
|
|
[0], // 1 - heading
|
|
|
|
[0], // 2 - heading
|
|
|
|
[0], // 3 - heading
|
|
|
|
[0], // 4 - heading
|
|
|
|
[], // 5 - document
|
|
|
|
[1], // 6 - table
|
|
|
|
[1, 0], // 7 - tableRow
|
|
|
|
[1, 0, 0], // 8 - tableCell
|
|
|
|
[1, 0, 0, 0], // 9 - paragraph
|
|
|
|
[1, 0, 0, 0], // 10 - paragraph
|
|
|
|
[1, 0, 0], // 11 - tableCell
|
|
|
|
[1, 0, 0, 1], // 12 - list
|
|
|
|
[1, 0, 0, 1, 0], // 13 - listItem
|
|
|
|
[1, 0, 0, 1, 0, 0], // 14 - paragraph
|
|
|
|
[1, 0, 0, 1, 0, 0], // 15 - paragraph
|
|
|
|
[1, 0, 0, 1, 0], // 16 - listItem
|
|
|
|
[1, 0, 0, 1, 0, 1], // 17 - list
|
|
|
|
[1, 0, 0, 1, 0, 1, 0], // 18 - listItem
|
|
|
|
[1, 0, 0, 1, 0, 1, 0, 0], // 19 - paragraph
|
|
|
|
[1, 0, 0, 1, 0, 1, 0, 0], // 20 - paragraph
|
|
|
|
[1, 0, 0, 1, 0, 1, 0], // 21 - listItem
|
|
|
|
[1, 0, 0, 1, 0, 1], // 22 - list
|
|
|
|
[1, 0, 0, 1, 0], // 23 - listItem
|
|
|
|
[1, 0, 0, 1], // 24 - list
|
|
|
|
[1, 0, 0], // 25 - tableCell
|
|
|
|
[1, 0, 0, 2], // 26 - list
|
|
|
|
[1, 0, 0, 2, 0], // 27 - listItem
|
|
|
|
[1, 0, 0, 2, 0, 0], // 28 - paragraph
|
|
|
|
[1, 0, 0, 2, 0, 0], // 29 - paragraph
|
|
|
|
[1, 0, 0, 2, 0], // 30 - listItem
|
|
|
|
[1, 0, 0, 2], // 31 - list
|
|
|
|
[1, 0, 0], // 32 - tableCell
|
|
|
|
[1, 0], // 33 - tableRow
|
|
|
|
[1], // 34 - table
|
|
|
|
[], // 35- document
|
|
|
|
[2], // 36 - preformatted
|
|
|
|
[2], // 37 - preformatted
|
|
|
|
[2], // 38 - preformatted
|
|
|
|
[2], // 39 - preformatted
|
|
|
|
[2], // 40 - preformatted
|
2012-05-16 20:33:27 +00:00
|
|
|
[], // 41 - document
|
|
|
|
[3], // 42 - definitionList
|
|
|
|
[3, 0], // 43 - definitionListItem
|
|
|
|
[3, 0, 0], // 44 - paragraph
|
|
|
|
[3, 0, 0], // 45 - paragraph
|
|
|
|
[3, 0], // 46 - definitionListItem
|
|
|
|
[3], // 47 - definitionList
|
|
|
|
[3, 1], // 48 - definitionListItem
|
|
|
|
[3, 1, 0], // 49 - paragraph
|
|
|
|
[3, 1, 0], // 50 - paragraph
|
|
|
|
[3, 1], // 51 - definitionListItem
|
|
|
|
[3], // 52 - definitionList
|
|
|
|
[], // 53 - document
|
|
|
|
[4], // 54 - paragraph
|
|
|
|
[4], // 55 - paragraph
|
|
|
|
[], // 56 - document
|
|
|
|
[5], // 57 - paragraph
|
|
|
|
[5], // 58 - paragraph
|
|
|
|
[] // 59 - document
|
2012-04-30 18:37:48 +00:00
|
|
|
];
|
|
|
|
for ( var i = 0; i < expected.length; i++ ) {
|
|
|
|
node = root;
|
|
|
|
for ( var j = 0; j < expected[i].length; j++ ) {
|
|
|
|
node = node.children[expected[i][j]];
|
|
|
|
}
|
|
|
|
ok( node === fragment.getNodeFromOffset( i ), 'reference at offset ' + i );
|
|
|
|
}
|
2012-04-27 21:59:52 +00:00
|
|
|
} );
|
2012-05-03 20:11:36 +00:00
|
|
|
|
|
|
|
test( 'getDataFromNode', 3, function() {
|
|
|
|
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data );
|
|
|
|
deepEqual(
|
|
|
|
fragment.getDataFromNode( fragment.getDocumentNode().getChildren()[0] ),
|
|
|
|
ve.dm.example.data.slice( 1, 4 ),
|
|
|
|
'branch with leaf children'
|
|
|
|
);
|
|
|
|
deepEqual(
|
|
|
|
fragment.getDataFromNode( fragment.getDocumentNode().getChildren()[1] ),
|
|
|
|
ve.dm.example.data.slice( 6, 34 ),
|
|
|
|
'branch with branch children'
|
|
|
|
);
|
|
|
|
deepEqual(
|
|
|
|
fragment.getDataFromNode( fragment.getDocumentNode().getChildren()[2].getChildren()[1] ),
|
|
|
|
[],
|
|
|
|
'leaf without children'
|
|
|
|
);
|
|
|
|
} );
|
2012-05-09 23:31:56 +00:00
|
|
|
|
2012-05-11 17:29:09 +00:00
|
|
|
test( 'getAnnotationsFromOffset', 1, function() {
|
|
|
|
var fragment,
|
|
|
|
range,
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
'msg': ['bold #1', 'bold #2'],
|
2012-05-11 18:37:45 +00:00
|
|
|
'data': [
|
|
|
|
['a', { '{"type:"bold"}': { 'type': 'bold' } }],
|
|
|
|
['b', { '{"type:"bold"}': { 'type': 'bold' } }]
|
|
|
|
],
|
|
|
|
'expected': [
|
2012-05-14 17:47:48 +00:00
|
|
|
[{ 'type': 'bold' }],
|
|
|
|
[{ 'type': 'bold' }]
|
2012-05-11 18:37:45 +00:00
|
|
|
]
|
2012-05-11 17:29:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'msg': ['bold #3', 'italic #1'],
|
2012-05-11 18:37:45 +00:00
|
|
|
'data': [
|
|
|
|
['a', { '{"type:"bold"}': { 'type': 'bold' } }],
|
|
|
|
['b', { '{"type:"italic"}': { 'type': 'italic' } }]
|
|
|
|
],
|
|
|
|
'expected': [
|
2012-05-14 17:47:48 +00:00
|
|
|
[{ 'type': 'bold' }],
|
|
|
|
[{ 'type': 'italic' }]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'msg': ['bold, italic & underline'],
|
|
|
|
'data': [
|
|
|
|
['a',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'},
|
|
|
|
'{"type":"underline"}': { 'type': 'underline'}
|
|
|
|
}]
|
|
|
|
],
|
|
|
|
'expected': [
|
|
|
|
[{ 'type': 'bold' }, { 'type': 'italic' }, { 'type': 'underline' }]
|
2012-05-11 18:37:45 +00:00
|
|
|
]
|
2012-05-11 17:29:09 +00:00
|
|
|
}
|
2012-05-14 17:47:48 +00:00
|
|
|
|
2012-05-11 17:29:09 +00:00
|
|
|
];
|
|
|
|
var expectCount = 0;
|
|
|
|
|
|
|
|
for (var c = 0; c < cases.length; c++) {
|
|
|
|
expectCount += cases[c].data.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect ( expectCount );
|
|
|
|
|
|
|
|
for (var i=0; i<cases.length; i++) {
|
|
|
|
fragment = new ve.dm.DocumentFragment ( cases[i].data );
|
|
|
|
for (var j=0; j<fragment.getData().length;j++) {
|
|
|
|
annotations = fragment.getAnnotationsFromOffset( j );
|
2012-05-14 16:58:30 +00:00
|
|
|
deepEqual(
|
|
|
|
annotations, cases[i].expected[j], cases[i].msg[j]
|
2012-05-11 17:29:09 +00:00
|
|
|
);
|
2012-05-09 23:31:56 +00:00
|
|
|
}
|
2012-05-11 17:29:09 +00:00
|
|
|
|
2012-05-09 23:31:56 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-05-11 17:29:09 +00:00
|
|
|
|
2012-05-11 23:14:46 +00:00
|
|
|
test( 'getAnnotationsFromRange', 1, function() {
|
|
|
|
var fragment,
|
|
|
|
range,
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
'msg': 'all bold',
|
|
|
|
'data': [
|
|
|
|
['a', { '{"type:"bold"}': { 'type': 'bold' } } ],
|
|
|
|
['b', { '{"type:"bold"}': { 'type': 'bold' } } ]
|
|
|
|
],
|
|
|
|
'expected': [ { 'type': 'bold' } ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'msg': 'bold and italic',
|
|
|
|
'data': [
|
2012-05-11 23:33:38 +00:00
|
|
|
['a',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['b',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'}
|
|
|
|
}
|
|
|
|
]
|
2012-05-11 23:14:46 +00:00
|
|
|
],
|
|
|
|
'expected': [ { 'type': 'bold' }, { 'type': 'italic' } ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'msg': 'bold and italic',
|
|
|
|
'data': [
|
2012-05-11 23:33:38 +00:00
|
|
|
['a',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['b',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'},
|
|
|
|
'{"type":"underline"}': { 'type': 'underline'}
|
|
|
|
}
|
|
|
|
]
|
2012-05-11 23:14:46 +00:00
|
|
|
],
|
|
|
|
'expected': [ { 'type': 'bold' }, { 'type': 'italic' } ]
|
|
|
|
},
|
2012-05-14 23:40:49 +00:00
|
|
|
{
|
2012-05-15 00:24:11 +00:00
|
|
|
'msg': 'none common, non annotated character at end',
|
2012-05-14 23:40:49 +00:00
|
|
|
'data': [
|
|
|
|
['a',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['b',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'},
|
|
|
|
'{"type":"underline"}': { 'type': 'underline'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['c']
|
|
|
|
],
|
|
|
|
'expected': []
|
|
|
|
},
|
2012-05-15 00:22:16 +00:00
|
|
|
{
|
|
|
|
'msg': 'none common, reverse of previous',
|
|
|
|
'data': [
|
|
|
|
['a'],
|
|
|
|
['b',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'},
|
|
|
|
'{"type":"underline"}': { 'type': 'underline'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['c',
|
|
|
|
{
|
|
|
|
'{"type":"bold"}': { 'type': 'bold' },
|
|
|
|
'{"type":"italic"}': { 'type': 'italic'}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected': []
|
|
|
|
},
|
2012-05-11 23:14:46 +00:00
|
|
|
{
|
|
|
|
'msg': 'all different',
|
|
|
|
'data': [
|
|
|
|
['a', { '{"type:"bold"}': { 'type': 'bold' } } ],
|
|
|
|
['b', { '{"type:"italic"}': { 'type': 'italic' } } ]
|
|
|
|
],
|
|
|
|
'expected': []
|
|
|
|
},
|
|
|
|
{
|
2012-05-15 00:24:11 +00:00
|
|
|
'msg': 'no annotations',
|
2012-05-11 23:14:46 +00:00
|
|
|
'data': ['a', 'b'],
|
|
|
|
'expected': []
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
expect( cases.length );
|
|
|
|
|
|
|
|
for ( var i=0; i<cases.length; i++ ) {
|
|
|
|
fragment = new ve.dm.DocumentFragment ( cases[i].data );
|
|
|
|
range = new ve.Range( 0, fragment.getData().length );
|
|
|
|
annotations = fragment.getAnnotationsFromRange( range );
|
|
|
|
deepEqual(
|
|
|
|
annotations, cases[i].expected, cases[i].msg
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|