2012-05-10 04:11:09 +00:00
|
|
|
/* Static Members */
|
|
|
|
|
|
|
|
ve.example = {};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.example.getSelectNodesCases = function( doc ) {
|
|
|
|
var lookup = ve.example.lookupNode,
|
|
|
|
documentNode = doc.getDocumentNode();
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 0, 3 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// heading/text - partial leaf results have ranges with global offsets
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0, 0 ),
|
|
|
|
'range': new ve.Range( 1, 3 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
}
|
2012-05-10 04:11:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 0, 10 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// heading/text - full coverage leaf nodes do not have ranges
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0, 0 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// table/tableRow/tableCell/paragraph/text - leaf nodes from different levels
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 1, 0, 0, 0, 0 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 9, 10 )
|
|
|
|
}
|
2012-05-10 04:11:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 28, 41 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// table/tableRow/tableCell/list/listItem/paragraph/text
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 1, 0, 0, 2, 0, 0, 0 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 28, 29 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// preformatted/text
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 2, 0 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 36, 37 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// preformatted/image - leaf nodes that are not text nodes
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 2, 1 ),
|
|
|
|
'index': 1,
|
|
|
|
'nodeRange': new ve.Range( 38, 38 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// preformatted/text
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 2, 2 ),
|
|
|
|
'index': 2,
|
|
|
|
'nodeRange': new ve.Range( 39, 40 )
|
|
|
|
}
|
2012-05-10 04:11:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 2, 15 ), 'siblings' ),
|
|
|
|
'expected': [
|
|
|
|
// heading
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0 ),
|
|
|
|
'range': new ve.Range( 2, 4 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// table
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 1 ),
|
|
|
|
'range': new ve.Range( 6, 15 ),
|
|
|
|
'index': 1,
|
|
|
|
'nodeRange': new ve.Range( 6, 34 )
|
|
|
|
}
|
2012-05-10 04:11:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 2, 49 ), 'siblings' ),
|
|
|
|
'expected': [
|
|
|
|
// heading
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0 ),
|
|
|
|
'range': new ve.Range( 2, 4 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// table
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 1 ),
|
|
|
|
'index': 1,
|
|
|
|
'nodeRange': new ve.Range( 6, 34 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// preformatted
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 2 ),
|
|
|
|
'index': 2,
|
|
|
|
'nodeRange': new ve.Range( 36, 40 )
|
|
|
|
},
|
2012-05-10 04:11:09 +00:00
|
|
|
// definitionList
|
2012-05-10 22:03:23 +00:00
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 3 ),
|
|
|
|
'range': new ve.Range( 42, 49 ),
|
|
|
|
'index': 3,
|
|
|
|
'nodeRange': new ve.Range( 42, 52 )
|
|
|
|
}
|
2012-05-10 04:11:09 +00:00
|
|
|
]
|
2012-05-10 23:08:15 +00:00
|
|
|
},
|
|
|
|
// Zero-length range at the edge of a text node returns that text node rather than
|
|
|
|
// its parent
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 1, 1 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// heading/text
|
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0, 0 ),
|
|
|
|
'range': new ve.Range( 1, 1 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
}
|
|
|
|
]
|
2012-05-10 23:42:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 4, 4 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// heading/text
|
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0, 0 ),
|
|
|
|
'range': new ve.Range( 4, 4 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
// Range entirely within one leaf node
|
|
|
|
{
|
|
|
|
'actual': doc.selectNodes( new ve.Range( 2, 3 ), 'leaves' ),
|
|
|
|
'expected': [
|
|
|
|
// heading/text
|
|
|
|
{
|
|
|
|
'node': lookup( documentNode, 0, 0 ),
|
|
|
|
'range': new ve.Range( 2, 3 ),
|
|
|
|
'index': 0,
|
|
|
|
'nodeRange': new ve.Range( 1, 4 )
|
|
|
|
}
|
|
|
|
]
|
2012-05-10 04:11:09 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-05-10 22:02:11 +00:00
|
|
|
* Asserts that two node trees are equivalent.
|
2012-05-10 04:11:09 +00:00
|
|
|
*
|
|
|
|
* This will perform 4 assertions on each branch node and 3 assertions on each leaf node.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.example.nodeTreeEqual = function( a, b ) {
|
|
|
|
equal( a.getType(), b.getType(), 'type match (' + a.getType() + ')' );
|
|
|
|
equal( a.getLength(), b.getLength(), 'length match' );
|
|
|
|
equal( a.getOuterLength(), b.getOuterLength(), 'outer length match' );
|
|
|
|
if ( a.children ) {
|
|
|
|
equal( a.children.length, b.children.length, 'children count match' );
|
|
|
|
for ( var i = 0; i < a.children.length; i++ ) {
|
|
|
|
ve.example.nodeTreeEqual( a.children[i], b.children[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-05-10 22:02:11 +00:00
|
|
|
* Asserts that two node selections are equivalent.
|
2012-05-10 04:11:09 +00:00
|
|
|
*
|
|
|
|
* This will perform 1 assertion to check the number of results in the selection and then 2
|
|
|
|
* assertions on each result
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.example.nodeSelectionEqual = function( a, b ) {
|
2012-05-10 23:42:16 +00:00
|
|
|
var minLength = a.length < b.length ? a.length : b.length;
|
2012-05-10 04:11:09 +00:00
|
|
|
equal( a.length, b.length, 'length match' );
|
2012-05-10 23:42:16 +00:00
|
|
|
for ( var i = 0; i < minLength; i++ ) {
|
2012-05-10 04:11:09 +00:00
|
|
|
ok( a[i].node === b[i].node, 'node match' );
|
|
|
|
if ( a[i].range && b[i].range ) {
|
|
|
|
deepEqual( a[i].range, b[i].range, 'range match' );
|
|
|
|
} else {
|
|
|
|
strictEqual( 'range' in a[i], 'range' in b[i], 'range existence match' );
|
|
|
|
}
|
2012-05-10 22:03:23 +00:00
|
|
|
deepEqual( a[i].index, b[i].index, 'index match' );
|
|
|
|
deepEqual( a[i].nodeRange, b[i].nodeRange, 'nodeRange match' );
|
2012-05-10 04:11:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks up a value in a node tree.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Node} root Root node to lookup from
|
|
|
|
* @param {Integer} [...] Index path
|
|
|
|
* @param {ve.Node} Node at given path
|
|
|
|
*/
|
|
|
|
ve.example.lookupNode = function( root ) {
|
|
|
|
var node = root;
|
|
|
|
for ( var i = 1; i < arguments.length; i++ ) {
|
|
|
|
node = node.children[arguments[i]];
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|