JSDuck: Generated code documentation!
See CODING.md for how to run it.
Mistakes fixed:
* Warning: Unknown type function
-> Function
* Warning: Unknown type DOMElement
-> HTMLElement
* Warning: Unknown type DOM Node
-> HTMLElement
* Warning: Unknown type Integer
-> Mixed
* Warning: Unknown type Command
-> ve.Command
* Warning: Unknown type any
-> number
* Warning: Unknown type ve.Transaction
-> ve.dm.Transaction
* Warning: Unknown type ve.dm.AnnotationSet
-> ve.AnnotationSet
* Warning: Unknown type false
-> boolean
* Warning: Unknown type ve.dm.AlienNode
ve.dm doesn't have a generic AlienNode like ve.ce
-> Unknown type ve.dm.AlienInlineNode|ve.dm.AlienBlockNode
* Warning: Unknown type ve.ve.Surface
-> ve.ce.Surface
* ve.example.lookupNode:
-> Last @param should be @return
* ve.dm.Transaction.prototype.pushReplace:
-> @param {Array] should be @param {Array}
* Warning: ve.BranchNode.js:27: {@link ve.Node#hasChildren} links to non-existing member
-> (removed)
* Warning: ve.LeafNode.js:21: {@link ve.Node#hasChildren} links to non-existing member
-> (removed)
Differences fixed:
* Variadic arguments are like @param {Type...} [name]
instead of @param {Type} [name...]
* Convert all file headers from /** to /*! because JSDuck tries
to parse all /** blocks and fails to parse with all sorts of
errors for "Global property", "Unnamed property", and
"Duplicate property".
Find: \/\*\*([^@]+)(@copyright)
Replace: /*!$1$2
* Indented blocks are considered code examples.
A few methods had documentation with numbered lists that were
indented, which have now been updated to not be intended.
* The free-form text descriptions are parsed with Markdown,
which requires lists to be separated from paragraphs by an
empty line.
And we should use `backticks` instead of {braces} for inline
code in text paragraphs.
* Doc blocks for classes and their constructor have to be
in the correct order (@constructor, @param, @return must be
before @class, @abstract, @extends etc.)
* `@extends Class` must not have Class {wrapped}
* @throws must start with a {Type}
* @example means something else. It is used for an inline demo
iframe, not code block. For that simply indent with spaces.
* @member means something else.
Non-function properties are marked with @property, not @member.
* To create a link to a class or member, in most cases the name
is enough to create a link. E.g. Foo, Foo.bar, Foo.bar#quux,
where a hash stands for "instance member", so Foo.bar#quux,
links to Foo.bar.prototype.quux (the is not supported, as
"prototype" is considered an implementation detail, it only
indexes class name and method name).
If the magic linker doesn't work for some case, the
verbose syntax is {@link #target label}.
* @property can't have sub-properties (nested @param and @return
values are supported, only @static @property can't be nested).
We only have one case of this, which can be worked around by
moving those in a new virtual class. The code is unaltered
(only moved down so that it isn't with the scope of the main
@class block). ve.dm.TransactionProcessor.processors.
New:
* @mixins: Classes mixed into the current class.
* @event: Events that can be emitted by a class. These are also
inherited by subclasses. (+ @param, @return and @preventable).
So ve.Node#event-attach is inherited to ve.dm.BreakNode,
just like @method is.
* @singleton: Plain objects such as ve, ve.dm, ve.ce were missing
documentation causing a tree error. Documented those as a
JSDuck singleton, which they but just weren't documented yet.
NB: Members of @singleton don't need @static (if present,
triggers a compiler warning).
* @chainable: Shorthand for "@return this". We were using
"@return {classname}" which is ambiguous (returns the same
instance or another instance?), @chainable is specifically
for "@return this". Creates proper labels in the generated
HTML pages.
Removed:
* @mixin: (not to be confused with @mixins). Not supported by
JSDuck. Every class is standalone anyway. Where needed marked
them @class + @abstract instead.
Change-Id: I6a7c9e8ee8f995731bc205d666167874eb2ebe23
2013-01-04 08:54:17 +00:00
|
|
|
/*!
|
2013-01-15 23:38:49 +00:00
|
|
|
* VisualEditor DataModel SurfaceFragment tests.
|
2012-08-17 17:48:40 +00:00
|
|
|
*
|
2013-02-19 23:37:34 +00:00
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
2012-08-17 17:48:40 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
QUnit.module( 've.dm.SurfaceFragment' );
|
|
|
|
|
2013-02-21 19:20:43 +00:00
|
|
|
/* Tests */
|
2012-08-17 17:48:40 +00:00
|
|
|
|
|
|
|
QUnit.test( 'constructor', 8, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-17 17:48:40 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface );
|
|
|
|
// Default range and autoSelect
|
|
|
|
assert.strictEqual( fragment.getSurface(), surface, 'surface reference is stored' );
|
|
|
|
assert.strictEqual( fragment.getDocument(), doc, 'document reference is stored' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 0, 0 ), 'range is taken from surface' );
|
2012-10-13 00:29:32 +00:00
|
|
|
assert.strictEqual( fragment.willAutoSelect(), true, 'auto select by default' );
|
2012-08-17 17:48:40 +00:00
|
|
|
assert.strictEqual( fragment.isNull(), false, 'valid fragment is not null' );
|
|
|
|
// Invalid range and autoSelect
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( -100, 100 ), 'truthy' );
|
|
|
|
assert.equal( fragment.getRange().from, 0, 'range is clamped between 0 and document length' );
|
|
|
|
assert.equal( fragment.getRange().to, 61, 'range is clamped between 0 and document length' );
|
2012-10-13 00:29:32 +00:00
|
|
|
assert.strictEqual( fragment.willAutoSelect(), false, 'noAutoSelect values are boolean' );
|
2012-08-17 17:48:40 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'onTransact', 1, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-17 17:48:40 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment1 = new ve.dm.SurfaceFragment( surface, new ve.Range( 1, 56 ) ),
|
|
|
|
fragment2 = new ve.dm.SurfaceFragment( surface, new ve.Range( 2, 4 ) );
|
|
|
|
fragment1.removeContent();
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment2.getRange(),
|
|
|
|
new ve.Range( 1, 1 ),
|
|
|
|
'fragment ranges are auto-translated when transactions are processed'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'adjustRange', 3, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-17 17:48:40 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 20, 21 ) ),
|
|
|
|
adjustedFragment = fragment.adjustRange( -19, 35 );
|
|
|
|
assert.ok( fragment !== adjustedFragment, 'adjustRange produces a new fragment' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 20, 21 ), 'old fragment is not changed' );
|
|
|
|
assert.deepEqual( adjustedFragment.getRange(), new ve.Range( 1, 56 ), 'new range is used' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'collapseRange', 3, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-17 17:48:40 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 20, 21 ) ),
|
|
|
|
collapsedFragment = fragment.collapseRange();
|
|
|
|
assert.ok( fragment !== collapsedFragment, 'collapseRange produces a new fragment' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 20, 21 ), 'old fragment is not changed' );
|
|
|
|
assert.deepEqual( collapsedFragment.getRange(), new ve.Range( 20, 20 ), 'new range is used' );
|
|
|
|
} );
|
|
|
|
|
2013-03-18 11:31:14 +00:00
|
|
|
QUnit.test( 'expandRange (closest)', 1, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-17 17:48:40 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 20, 21 ) );
|
|
|
|
assert.strictEqual(
|
|
|
|
fragment.expandRange( 'closest', 'invalid type' ).isNull(),
|
|
|
|
true,
|
|
|
|
'closest with invalid type results in null fragment'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2013-03-18 11:31:14 +00:00
|
|
|
QUnit.test( 'expandRange (word)', 1, function ( assert ) {
|
|
|
|
var i, doc, surface, fragment, newFragment, range, word, cases = [
|
|
|
|
{
|
|
|
|
phrase: 'the quick brown fox',
|
|
|
|
range: new ve.Range( 6, 13 ),
|
|
|
|
expected: 'quick brown',
|
|
|
|
msg: 'range starting and ending in latin words'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
phrase: 'the quick brown fox',
|
|
|
|
range: new ve.Range( 18, 12 ),
|
|
|
|
expected: 'brown fox',
|
|
|
|
msg: 'backwards range starting and ending in latin words'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
phrase: 'the quick brown fox',
|
|
|
|
range: new ve.Range( 7, 7 ),
|
|
|
|
expected: 'quick',
|
|
|
|
msg: 'zero-length range'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
QUnit.expect( cases.length*2 );
|
|
|
|
for ( i = 0; i < cases.length; i++ ) {
|
|
|
|
doc = new ve.dm.Document( cases[i].phrase.split('') );
|
|
|
|
surface = new ve.dm.Surface( doc );
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, cases[i].range );
|
|
|
|
newFragment = fragment.expandRange( 'word' );
|
|
|
|
range = newFragment.getRange();
|
|
|
|
word = cases[i].phrase.substring( range.start, range.end );
|
|
|
|
assert.strictEqual( word, cases[i].expected, cases[i].msg + ': text' );
|
|
|
|
assert.strictEqual( cases[i].range.isBackwards(), range.isBackwards(), cases[i].msg + ': range direction' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-08-24 22:24:23 +00:00
|
|
|
QUnit.test( 'removeContent', 2, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-24 22:24:23 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
2012-10-23 00:53:58 +00:00
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 1, 56 ) ),
|
|
|
|
expectedData = ve.copyArray( ve.dm.example.data.slice( 0, 1 ) )
|
|
|
|
.concat( ve.copyArray( ve.dm.example.data.slice( 4, 5 ) ) )
|
|
|
|
.concat( ve.copyArray( ve.dm.example.data.slice( 55 ) ) );
|
|
|
|
ve.setProp( expectedData[0], 'internal', 'changed', 'content', 1 );
|
2012-08-24 22:24:23 +00:00
|
|
|
fragment.removeContent();
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
2012-10-23 00:53:58 +00:00
|
|
|
expectedData,
|
2012-08-24 22:24:23 +00:00
|
|
|
'removing content drops fully covered nodes and strips partially covered ones'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 1, 1 ),
|
|
|
|
'removing content results in a zero-length fragment'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'insertContent', 3, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-24 22:24:23 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 1, 4 ) );
|
|
|
|
fragment.insertContent( ['1', '2', '3'] );
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 1, 4 ) ),
|
|
|
|
['1', '2', '3'],
|
|
|
|
'inserting content replaces selection with new content'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 4, 4 ),
|
|
|
|
'inserting content results in a zero-length fragment'
|
|
|
|
);
|
|
|
|
fragment.insertContent( '321' );
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 4, 7 ) ),
|
|
|
|
['3', '2', '1'],
|
|
|
|
'strings get converted into data when inserting content'
|
|
|
|
);
|
|
|
|
} );
|
2012-08-24 22:25:37 +00:00
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
QUnit.test( 'wrapNodes/unwrapNodes', 10, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
|
|
|
originalDoc = ve.dm.example.createExampleDocument(),
|
2012-08-24 22:25:37 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) );
|
2013-03-01 01:20:59 +00:00
|
|
|
|
2012-08-24 22:25:37 +00:00
|
|
|
// Make 2 paragraphs into 2 lists of 1 item each
|
|
|
|
fragment.wrapNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 55, 69 ) ),
|
|
|
|
[
|
2012-10-23 00:53:58 +00:00
|
|
|
{
|
|
|
|
'type': 'list',
|
|
|
|
'attributes': { 'style': 'bullet' },
|
|
|
|
'internal': { 'changed': { 'created': 1 } }
|
|
|
|
},
|
|
|
|
{ 'type': 'listItem', 'internal': { 'changed': { 'created': 1 } } },
|
2012-08-24 22:25:37 +00:00
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'l',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': '/listItem' },
|
|
|
|
{ 'type': '/list' },
|
2012-10-23 00:53:58 +00:00
|
|
|
{
|
|
|
|
'type': 'list',
|
|
|
|
'attributes': { 'style': 'bullet' },
|
|
|
|
'internal': { 'changed': { 'created': 1 } }
|
|
|
|
},
|
|
|
|
{ 'type': 'listItem', 'internal': { 'changed': { 'created': 1 } } },
|
2012-08-24 22:25:37 +00:00
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'm',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': '/listItem' },
|
|
|
|
{ 'type': '/list' }
|
|
|
|
],
|
|
|
|
'wrapping nodes can add multiple levels of wrapping to multiple elements'
|
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 69 ), 'new range contains wrapping elements' );
|
|
|
|
|
|
|
|
fragment.unwrapNodes( 0, 2 );
|
2013-03-20 22:35:05 +00:00
|
|
|
assert.deepEqual( doc.getData(), originalDoc.getData(), 'unwrapping 2 levels restores document to original state' );
|
2013-03-01 01:20:59 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 61 ), 'range after unwrapping is same as original range' );
|
|
|
|
|
2012-08-24 22:25:37 +00:00
|
|
|
// Make a 1 paragraph into 1 list with 1 item
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 9, 12 ) );
|
|
|
|
fragment.wrapNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 9, 16 ) ),
|
|
|
|
[
|
2012-10-23 00:53:58 +00:00
|
|
|
{
|
|
|
|
'type': 'list',
|
|
|
|
'attributes': { 'style': 'bullet' },
|
|
|
|
'internal': { 'changed': { 'created': 1 } }
|
|
|
|
},
|
|
|
|
{ 'type': 'listItem', 'internal': { 'changed': { 'created': 1 } } },
|
2012-08-24 22:25:37 +00:00
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'd',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': '/listItem' },
|
|
|
|
{ 'type': '/list' }
|
|
|
|
],
|
|
|
|
'wrapping nodes can add multiple levels of wrapping to a single element'
|
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 16 ), 'new range contains wrapping elements' );
|
|
|
|
|
|
|
|
fragment.unwrapNodes( 0, 2 );
|
2013-03-20 22:35:05 +00:00
|
|
|
assert.deepEqual( doc.getData(), originalDoc.getData(), 'unwrapping 2 levels restores document to original state' );
|
2013-03-01 01:20:59 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 12 ), 'range after unwrapping is same as original range' );
|
|
|
|
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 8, 34 ) );
|
|
|
|
fragment.unwrapNodes( 3, 1 );
|
|
|
|
assert.deepEqual( fragment.getData(), doc.getData( new ve.Range( 5, 29 ) ), 'unwrapping multiple outer nodes and an inner node' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 5, 29 ), 'new range contains inner elements' );
|
|
|
|
|
2012-08-24 22:25:37 +00:00
|
|
|
} );
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
QUnit.test( 'rewrapNodes', 4, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2012-08-24 22:25:37 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 43, 55 ) ),
|
2013-03-20 22:35:05 +00:00
|
|
|
expectedDoc = ve.dm.example.createExampleDocument(),
|
2013-03-01 01:20:59 +00:00
|
|
|
expectedSurface = new ve.dm.Surface( expectedDoc ),
|
|
|
|
expectedFragment = new ve.dm.SurfaceFragment( expectedSurface, new ve.Range( 43, 55 ) ),
|
|
|
|
created = { 'changed': { 'created': 1 } },
|
2013-02-22 20:49:13 +00:00
|
|
|
expectedData;
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
// set up wrapped nodes in example document
|
|
|
|
fragment.wrapNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
expectedFragment.wrapNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
// range is now 43, 59
|
|
|
|
|
|
|
|
// Compare a rewrap operation with its equivalent unwrap + wrap
|
|
|
|
// This type of test can only exist if the intermediate state is valid
|
|
|
|
fragment.rewrapNodes(
|
|
|
|
2,
|
|
|
|
[{ 'type': 'definitionList' }, { 'type': 'definitionListItem', 'attributes': { 'style': 'term' } }]
|
|
|
|
);
|
|
|
|
expectedFragment.unwrapNodes( 0, 2 );
|
|
|
|
expectedFragment.wrapNodes(
|
|
|
|
[{ 'type': 'definitionList' }, { 'type': 'definitionListItem', 'attributes': { 'style': 'term' } }]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
|
|
|
expectedDoc.getData(),
|
|
|
|
'rewrapping multiple nodes via a valid intermediate state produces the same document as unwrapping then wrapping'
|
|
|
|
);
|
|
|
|
assert.deepEqual( fragment.getRange(), expectedFragment.getRange(), 'new range contains rewrapping elements' );
|
|
|
|
|
|
|
|
// Rewrap paragrphs as headings
|
|
|
|
// The intermediate stage (plain text attached to the document) would be invalid
|
|
|
|
// if performed as an unwrap and a wrap
|
|
|
|
expectedData = ve.copyArray( doc.getData() );
|
|
|
|
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 59, 65 ) );
|
|
|
|
fragment.rewrapNodes( 1, [ { 'type': 'heading', 'attributes': { 'level': 1 } } ] );
|
|
|
|
|
|
|
|
expectedData.splice( 59, 1, { 'type': 'heading', 'attributes': { 'level': 1 }, 'internal': created } );
|
|
|
|
expectedData.splice( 61, 1, { 'type': '/heading' } );
|
|
|
|
expectedData.splice( 62, 1, { 'type': 'heading', 'attributes': { 'level': 1 }, 'internal': created } );
|
|
|
|
expectedData.splice( 64, 1, { 'type': '/heading' } );
|
|
|
|
|
|
|
|
assert.deepEqual( doc.getData(), expectedData, 'rewrapping paragraphs as headings' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 59, 65 ), 'new range contains rewrapping elements' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'wrapAllNodes', 10, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
|
|
|
originalDoc = ve.dm.example.createExampleDocument(),
|
2013-03-01 01:20:59 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) ),
|
|
|
|
expectedData = ve.copyArray( doc.getData() );
|
|
|
|
|
2012-08-24 22:25:37 +00:00
|
|
|
// Make 2 paragraphs into 1 lists of 1 item with 2 paragraphs
|
|
|
|
fragment.wrapAllNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 55, 65 ) ),
|
|
|
|
[
|
2012-10-23 00:53:58 +00:00
|
|
|
{
|
|
|
|
'type': 'list',
|
|
|
|
'attributes': { 'style': 'bullet' },
|
|
|
|
'internal': { 'changed': { 'created': 1 } }
|
|
|
|
},
|
|
|
|
{ 'type': 'listItem', 'internal': { 'changed': { 'created': 1 } } },
|
2012-08-24 22:25:37 +00:00
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'l',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'm',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': '/listItem' },
|
|
|
|
{ 'type': '/list' }
|
|
|
|
],
|
|
|
|
'wrapping nodes can add multiple levels of wrapping to multiple elements'
|
|
|
|
);
|
2013-02-22 20:49:13 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 65 ), 'new range contains wrapping elements' );
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment.unwrapNodes( 0, 2 );
|
2013-03-20 22:35:05 +00:00
|
|
|
assert.deepEqual( doc.getData(), originalDoc.getData(), 'unwrapping 2 levels restores document to original state' );
|
2013-02-22 20:49:13 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 61 ), 'range after unwrapping is same as original range' );
|
|
|
|
|
2012-08-24 22:25:37 +00:00
|
|
|
// Make a 1 paragraph into 1 list with 1 item
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 9, 12 ) );
|
|
|
|
fragment.wrapAllNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 9, 16 ) ),
|
|
|
|
[
|
2012-10-23 00:53:58 +00:00
|
|
|
{
|
|
|
|
'type': 'list',
|
|
|
|
'attributes': { 'style': 'bullet' },
|
|
|
|
'internal': { 'changed': { 'created': 1 } }
|
|
|
|
},
|
|
|
|
{ 'type': 'listItem', 'internal': { 'changed': { 'created': 1 } } },
|
2012-08-24 22:25:37 +00:00
|
|
|
{ 'type': 'paragraph' },
|
|
|
|
'd',
|
|
|
|
{ 'type': '/paragraph' },
|
|
|
|
{ 'type': '/listItem' },
|
|
|
|
{ 'type': '/list' }
|
|
|
|
],
|
|
|
|
'wrapping nodes can add multiple levels of wrapping to a single element'
|
|
|
|
);
|
2013-02-22 20:49:13 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 16 ), 'new range contains wrapping elements' );
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment.unwrapNodes( 0, 2 );
|
2013-03-20 22:35:05 +00:00
|
|
|
assert.deepEqual( doc.getData(), originalDoc.getData(), 'unwrapping 2 levels restores document to original state' );
|
2013-02-22 20:49:13 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 12 ), 'range after unwrapping is same as original range' );
|
|
|
|
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 5, 37 ) );
|
|
|
|
|
|
|
|
assert.throws( function() {
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment.unwrapNodes( 0, 20 );
|
2013-02-22 20:49:13 +00:00
|
|
|
}, /cannot unwrap by greater depth/, 'error thrown trying to unwrap more nodes that it is possible to contain' );
|
|
|
|
|
|
|
|
expectedData.splice( 5, 4 );
|
|
|
|
expectedData.splice( 29, 4 );
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment.unwrapNodes( 0, 4 );
|
2013-02-22 20:49:13 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
|
|
|
expectedData,
|
|
|
|
'unwrapping 4 levels (table, tableSection, tableRow and tableCell)'
|
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
} );
|
2013-02-21 19:20:43 +00:00
|
|
|
|
2013-02-25 22:32:29 +00:00
|
|
|
QUnit.test( 'rewrapAllNodes', 6, function ( assert ) {
|
|
|
|
var expectedData,
|
2013-03-20 22:35:05 +00:00
|
|
|
doc = ve.dm.example.createExampleDocument(),
|
|
|
|
originalDoc = ve.dm.example.createExampleDocument(),
|
2013-02-25 22:32:29 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 5, 37 ) ),
|
2013-03-20 22:35:05 +00:00
|
|
|
expectedDoc = ve.dm.example.createExampleDocument(),
|
2013-02-25 22:32:29 +00:00
|
|
|
expectedSurface = new ve.dm.Surface( expectedDoc ),
|
|
|
|
expectedFragment = new ve.dm.SurfaceFragment( expectedSurface, new ve.Range( 5, 37 ) ),
|
|
|
|
created = { 'changed': { 'created' : 1 } };
|
|
|
|
|
|
|
|
// Compare a rewrap operation with its equivalent unwrap + wrap
|
|
|
|
// This type of test can only exist if the intermediate state is valid
|
|
|
|
fragment.rewrapAllNodes(
|
|
|
|
4,
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
expectedFragment.unwrapNodes( 0, 4 );
|
2013-02-25 22:32:29 +00:00
|
|
|
expectedFragment.wrapAllNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
|
|
|
expectedDoc.getData(),
|
|
|
|
'rewrapping multiple nodes via a valid intermediate state produces the same document as unwrapping then wrapping'
|
|
|
|
);
|
|
|
|
assert.deepEqual( fragment.getRange(), expectedFragment.getRange(), 'new range contains rewrapping elements' );
|
|
|
|
|
|
|
|
// Reverse of first test
|
|
|
|
fragment.rewrapAllNodes(
|
|
|
|
2,
|
|
|
|
[
|
|
|
|
{ 'type': 'table', },
|
|
|
|
{ 'type': 'tableSection', 'attributes': { 'style': 'body' } },
|
|
|
|
{ 'type': 'tableRow' },
|
|
|
|
{ 'type': 'tableCell', 'attributes': { 'style': 'data' } }
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
expectedData = originalDoc.getData();
|
2013-02-25 22:32:29 +00:00
|
|
|
expectedData[5].internal = created;
|
|
|
|
expectedData[6].internal = created;
|
|
|
|
expectedData[7].internal = created;
|
|
|
|
expectedData[8].internal = created;
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
|
|
|
expectedData,
|
|
|
|
'rewrapping multiple nodes via a valid intermediate state produces the same document as unwrapping then wrapping'
|
|
|
|
);
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 5, 37 ), 'new range contains rewrapping elements' );
|
|
|
|
|
|
|
|
// Rewrap a heading as a paragraph
|
|
|
|
// The intermediate stage (plain text attached to the document) would be invalid
|
|
|
|
// if performed as an unwrap and a wrap
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 0, 5 ) );
|
|
|
|
fragment.rewrapAllNodes( 1, [ { 'type': 'paragraph' } ] );
|
|
|
|
|
|
|
|
expectedData.splice( 0, 1, { 'type': 'paragraph', 'internal': created } );
|
|
|
|
expectedData.splice( 4, 1, { 'type': '/paragraph' } );
|
|
|
|
|
|
|
|
assert.deepEqual( doc.getData(), expectedData, 'rewrapping a heading as a paragraph' );
|
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 0, 5 ), 'new range contains rewrapping elements' );
|
2013-03-01 01:20:59 +00:00
|
|
|
} );
|
2013-02-25 22:32:29 +00:00
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
function runIsolateTest( assert, type, range, expected, label ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument( 'isolationData' ),
|
2013-02-21 19:20:43 +00:00
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, range ),
|
|
|
|
data;
|
|
|
|
|
|
|
|
data = ve.copyArray( doc.getFullData() );
|
2013-03-01 01:20:59 +00:00
|
|
|
fragment.isolateAndUnwrap( type );
|
2013-02-21 19:20:43 +00:00
|
|
|
expected( data );
|
|
|
|
|
|
|
|
assert.deepEqual( doc.getFullData(), data, label );
|
|
|
|
}
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
QUnit.test( 'isolateAndUnwrap', 4, function ( assert ) {
|
2013-02-21 19:20:43 +00:00
|
|
|
var rebuilt = { 'changed': { 'rebuilt': 1 } },
|
|
|
|
createdAndRebuilt = { 'changed': { 'created': 1, 'rebuilt': 1 } };
|
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
runIsolateTest( assert, 'MWheading', new ve.Range( 12, 20 ), function( data ) {
|
2013-02-21 19:20:43 +00:00
|
|
|
data[0].internal = rebuilt;
|
2013-03-01 01:20:59 +00:00
|
|
|
data.splice( 11, 0, { 'type': '/list' } );
|
|
|
|
data.splice( 12, 1 );
|
|
|
|
data.splice( 20, 1, { 'type': 'list', 'attributes': { 'style': 'bullet' }, 'internal': createdAndRebuilt });
|
|
|
|
}, 'isolating paragraph in list item "Item 2" for MWheading');
|
2013-02-21 19:20:43 +00:00
|
|
|
|
2013-03-01 01:20:59 +00:00
|
|
|
runIsolateTest( assert, 'heading', new ve.Range( 12, 20 ), function( data ) {
|
|
|
|
data.splice( 11, 0, { 'type': 'listItem' } );
|
|
|
|
data.splice( 12, 1 );
|
|
|
|
data.splice( 20, 1, { 'type': '/listItem' });
|
|
|
|
}, 'isolating paragraph in list item "Item 2" for heading');
|
|
|
|
|
|
|
|
runIsolateTest( assert, 'MWheading', new ve.Range( 89, 97 ), function( data ) {
|
2013-02-21 19:20:43 +00:00
|
|
|
data[75].internal = rebuilt;
|
|
|
|
data[76].internal = rebuilt;
|
|
|
|
data[77].internal = rebuilt;
|
2013-03-01 01:20:59 +00:00
|
|
|
data.splice( 88, 1,
|
2013-02-21 19:20:43 +00:00
|
|
|
{ 'type': '/tableRow' },
|
|
|
|
{ 'type': '/tableSection' },
|
2013-03-01 01:20:59 +00:00
|
|
|
{ 'type': '/table' }
|
|
|
|
);
|
|
|
|
data.splice( 99, 1,
|
2013-02-21 19:20:43 +00:00
|
|
|
{ 'type': 'table', 'internal': createdAndRebuilt },
|
|
|
|
{ 'type': 'tableSection', 'attributes': { 'style': 'body' }, 'internal': createdAndRebuilt },
|
2013-03-01 01:20:59 +00:00
|
|
|
{ 'type': 'tableRow', 'internal': createdAndRebuilt }
|
2013-02-21 19:20:43 +00:00
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
}, 'isolating "Cell 2" for MWheading');
|
|
|
|
|
|
|
|
runIsolateTest( assert, 'MWheading', new ve.Range( 202, 212 ), function( data ) {
|
|
|
|
data[186].internal = rebuilt;
|
|
|
|
data[187].internal = rebuilt;
|
|
|
|
data[188].internal = rebuilt;
|
|
|
|
data.splice( 201, 1,
|
|
|
|
{ 'type': '/list' }, { 'type': '/listItem' }, { 'type': '/list' }
|
|
|
|
);
|
|
|
|
data.splice( 214, 1,
|
|
|
|
{ 'type': 'list', 'attributes': { 'style': 'bullet' }, 'internal': createdAndRebuilt },
|
|
|
|
{ 'type': 'listItem', 'internal': createdAndRebuilt },
|
|
|
|
{ 'type': 'list', 'attributes': { 'style': 'number' }, 'internal': createdAndRebuilt }
|
2013-02-21 19:20:43 +00:00
|
|
|
);
|
2013-03-01 01:20:59 +00:00
|
|
|
}, 'isolating paragraph in list item "Nested 2" for MWheading');
|
2013-02-21 19:20:43 +00:00
|
|
|
} );
|