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' );
|
2013-09-17 23:36:49 +00:00
|
|
|
assert.deepEqual( fragment.getRange(), new ve.Range( 1, 1 ), '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' );
|
2013-06-10 23:05:42 +00:00
|
|
|
assert.equal( fragment.getRange().to, doc.data.getLength(), '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
|
|
|
} );
|
|
|
|
|
2013-04-23 16:05:48 +00:00
|
|
|
QUnit.test( 'update', 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 ),
|
2013-04-23 16:05:48 +00:00
|
|
|
fragment1 = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) ),
|
|
|
|
fragment2 = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) ),
|
|
|
|
fragment3 = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) );
|
|
|
|
fragment1.wrapNodes(
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
|
|
|
|
);
|
2012-08-17 17:48:40 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
fragment2.getRange(),
|
2013-04-23 16:05:48 +00:00
|
|
|
new ve.Range( 55, 69 ),
|
|
|
|
'fragment range changes after wrapNodes'
|
2013-04-22 11:20:26 +00:00
|
|
|
);
|
|
|
|
surface.undo();
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment3.getRange(),
|
2013-04-23 16:05:48 +00:00
|
|
|
new ve.Range( 55, 61 ),
|
|
|
|
'fragment range restored after undo'
|
2012-08-17 17:48:40 +00:00
|
|
|
);
|
2013-04-23 16:05:48 +00:00
|
|
|
|
|
|
|
fragment1 = new ve.dm.SurfaceFragment( surface, new ve.Range( 1, 1 ) );
|
|
|
|
surface.breakpoint();
|
|
|
|
|
|
|
|
fragment1.insertContent( '01' );
|
|
|
|
surface.breakpoint();
|
|
|
|
|
|
|
|
fragment1 = fragment1.collapseRangeToEnd();
|
|
|
|
fragment1.insertContent( '234' );
|
|
|
|
fragment2 = fragment1.adjustRange();
|
|
|
|
|
|
|
|
surface.undo();
|
|
|
|
fragment1.insertContent( '5678' );
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment2.getRange(),
|
|
|
|
new ve.Range( 3, 7 ),
|
|
|
|
'Range created during truncated undo point still translates correctly'
|
|
|
|
);
|
|
|
|
|
2012-08-17 17:48:40 +00:00
|
|
|
} );
|
|
|
|
|
2013-04-23 16:05:48 +00:00
|
|
|
QUnit.test( 'adjustRange', 4, 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' );
|
2013-04-23 16:05:48 +00:00
|
|
|
|
|
|
|
adjustedFragment = fragment.adjustRange();
|
|
|
|
assert.deepEqual( adjustedFragment, fragment, 'fragment is clone if no parameters supplied' );
|
2012-08-17 17:48:40 +00:00
|
|
|
} );
|
|
|
|
|
2013-04-23 16:05:48 +00:00
|
|
|
QUnit.test( 'collapseRangeToStart/End', 6, 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 ) ),
|
2013-04-23 16:05:48 +00:00
|
|
|
collapsedFragment = fragment.collapseRangeToStart();
|
|
|
|
assert.ok( fragment !== collapsedFragment, 'collapseRangeToStart produces a new fragment' );
|
2012-08-17 17:48:40 +00:00
|
|
|
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-04-23 16:05:48 +00:00
|
|
|
|
|
|
|
collapsedFragment = fragment.collapseRangeToEnd();
|
|
|
|
assert.ok( fragment !== collapsedFragment, 'collapseRangeToEnd 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( 21, 21 ), 'range is at end when collapseToEnd is set' );
|
2012-08-17 17:48:40 +00:00
|
|
|
} );
|
|
|
|
|
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 ),
|
Death and/or destruction
So. It turns out that the design of SurfaceFragment is a little -
shall we say - wonky.
One of the best things about ve.dm.SurfaceFragment is its magical
ability to retain the intention of its range, even as transactions
are being processed. This ability is granted by each fragment
listening to the surface's change event, and responding by using
translateRange for each transaction that gets processed. Surface
fragments also have these clever methods that allow you to get a
fragment based on another, which makes adjusting the range easy to do
inline without having to manually store multiple fragments or
modifying the original.
This sounded good, and we seemed to all be convinced it was well
designed. But if you add a console.log( 'hello' ); to the first line
of ve.dm.SurfaceFragment.prototype.onTransact, and then start using
the bold tool on various selections of text, you will find that there
may indeed be a flaw. What you will probably realize is that the
number of times that particular line of code is being called is
disturbingly large, and increases each time you do just about anything
in the editor. What's going on? How did we get here? Read on…
It turns out that fragments are immortal. We create them, they listen
to the surface's transact event, we are done with them, but the
surface keeps on emitting events to the now long forgotten about
fragments. They continue to build up over time, never go out of scope,
and bloat the hell out of our program.
The same ended up being true of toolbars - and each time the context
menu fired up a new one the old one was left in limbo, still
responding to events, still taking up memory, but not being visible to
the user.
All of this immortality was causing strange and difficult to track
down problems. This patch fixes this by introducing a destroy method.
This method unbinds events, allowing the object to finally fall out of
scope and die - and more importantly stop receiving notifications of
changes.
This is a hack, but Ed will no doubt get this situation sorted out
properly by making fragments lazy-evaluate their selections by only
storing an identifier of the most recent transaction they were based
on, see bug 47343.
Change-Id: I18bb986001a44732a7871b9d79dc3015eedfb168
2013-04-18 18:44:40 +00:00
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 20, 21 ) ),
|
|
|
|
exapandedFragment = fragment.expandRange( 'closest', 'invalid type' );
|
2012-08-17 17:48:40 +00:00
|
|
|
assert.strictEqual(
|
Death and/or destruction
So. It turns out that the design of SurfaceFragment is a little -
shall we say - wonky.
One of the best things about ve.dm.SurfaceFragment is its magical
ability to retain the intention of its range, even as transactions
are being processed. This ability is granted by each fragment
listening to the surface's change event, and responding by using
translateRange for each transaction that gets processed. Surface
fragments also have these clever methods that allow you to get a
fragment based on another, which makes adjusting the range easy to do
inline without having to manually store multiple fragments or
modifying the original.
This sounded good, and we seemed to all be convinced it was well
designed. But if you add a console.log( 'hello' ); to the first line
of ve.dm.SurfaceFragment.prototype.onTransact, and then start using
the bold tool on various selections of text, you will find that there
may indeed be a flaw. What you will probably realize is that the
number of times that particular line of code is being called is
disturbingly large, and increases each time you do just about anything
in the editor. What's going on? How did we get here? Read on…
It turns out that fragments are immortal. We create them, they listen
to the surface's transact event, we are done with them, but the
surface keeps on emitting events to the now long forgotten about
fragments. They continue to build up over time, never go out of scope,
and bloat the hell out of our program.
The same ended up being true of toolbars - and each time the context
menu fired up a new one the old one was left in limbo, still
responding to events, still taking up memory, but not being visible to
the user.
All of this immortality was causing strange and difficult to track
down problems. This patch fixes this by introducing a destroy method.
This method unbinds events, allowing the object to finally fall out of
scope and die - and more importantly stop receiving notifications of
changes.
This is a hack, but Ed will no doubt get this situation sorted out
properly by making fragments lazy-evaluate their selections by only
storing an identifier of the most recent transaction they were based
on, see bug 47343.
Change-Id: I18bb986001a44732a7871b9d79dc3015eedfb168
2013-04-18 18:44:40 +00:00
|
|
|
exapandedFragment.isNull(),
|
2012-08-17 17:48:40 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
];
|
2013-12-06 02:34:44 +00:00
|
|
|
QUnit.expect( cases.length * 2 );
|
2013-03-18 11:31:14 +00:00
|
|
|
for ( i = 0; i < cases.length; i++ ) {
|
2013-06-24 17:51:59 +00:00
|
|
|
doc = new ve.dm.Document( cases[i].phrase.split( '' ) );
|
2013-03-18 11:31:14 +00:00
|
|
|
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' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2013-04-23 16:05:48 +00:00
|
|
|
QUnit.test( 'removeContent', 6, function ( assert ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
2013-04-23 16:05:48 +00:00
|
|
|
originalDoc = ve.dm.example.createExampleDocument(),
|
|
|
|
expectedDoc = 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 ) ),
|
2013-07-28 20:51:32 +00:00
|
|
|
expectedData = ve.copy( expectedDoc.data.slice( 0, 1 ) )
|
|
|
|
.concat( ve.copy( expectedDoc.data.slice( 4, 5 ) ) )
|
|
|
|
.concat( ve.copy( expectedDoc.data.slice( 55 ) ) );
|
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'
|
|
|
|
);
|
2013-04-23 16:05:48 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 1, 3 ),
|
|
|
|
'removing content results in a fragment covering just remaining structure'
|
|
|
|
);
|
|
|
|
surface.undo();
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData(),
|
|
|
|
originalDoc.getData(),
|
|
|
|
'content restored after undo'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 1, 56 ),
|
|
|
|
'range restored after undo'
|
|
|
|
);
|
|
|
|
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 1, 4 ) );
|
|
|
|
fragment.removeContent();
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 0, 2 ) ),
|
|
|
|
[
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'heading', 'attributes': { 'level': 1 } },
|
2013-12-06 02:34:44 +00:00
|
|
|
{ 'type': '/heading' }
|
2013-04-23 16:05:48 +00:00
|
|
|
],
|
|
|
|
'removing content empties node'
|
|
|
|
);
|
2012-08-24 22:24:23 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 1, 1 ),
|
2013-04-23 16:05:48 +00:00
|
|
|
'removing content collapses range'
|
2012-08-24 22:24:23 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2013-04-23 16:05:48 +00:00
|
|
|
QUnit.test( 'insertContent', 4, 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(),
|
2013-04-23 16:05:48 +00:00
|
|
|
new ve.Range( 1, 4 ),
|
|
|
|
'inserting content results in range around content'
|
2012-08-24 22:24:23 +00:00
|
|
|
);
|
2013-04-23 16:05:48 +00:00
|
|
|
|
|
|
|
surface.breakpoint();
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 4 ) );
|
2012-08-24 22:24:23 +00:00
|
|
|
fragment.insertContent( '321' );
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 4, 7 ) ),
|
|
|
|
['3', '2', '1'],
|
|
|
|
'strings get converted into data when inserting content'
|
|
|
|
);
|
2013-04-23 16:05:48 +00:00
|
|
|
|
|
|
|
surface.undo();
|
|
|
|
fragment.getRange();
|
|
|
|
assert.deepEqual(
|
|
|
|
fragment.getRange(),
|
|
|
|
new ve.Range( 4 ),
|
|
|
|
'range restored after undo'
|
|
|
|
);
|
2012-08-24 22:24:23 +00:00
|
|
|
} );
|
2012-08-24 22:25:37 +00:00
|
|
|
|
2013-06-14 19:07:55 +00:00
|
|
|
QUnit.test( 'changeAttributes', 1, function ( assert ) {
|
|
|
|
var doc = ve.dm.example.createExampleDocument(),
|
|
|
|
surface = new ve.dm.Surface( doc ),
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 0, 5 ) );
|
|
|
|
fragment.changeAttributes( { 'level': 3 } );
|
|
|
|
assert.deepEqual(
|
|
|
|
doc.getData( new ve.Range( 0, 1 ) ),
|
|
|
|
[ { 'type': 'heading', 'attributes': { 'level': 3 } } ],
|
|
|
|
'changing attributes affects covered nodes'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
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',
|
2013-02-16 00:27:03 +00:00
|
|
|
'attributes': { 'style': 'bullet' }
|
2012-10-23 00:53:58 +00:00
|
|
|
},
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'listItem' },
|
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',
|
2013-02-16 00:27:03 +00:00
|
|
|
'attributes': { 'style': 'bullet' }
|
2012-10-23 00:53:58 +00:00
|
|
|
},
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'listItem' },
|
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',
|
2013-02-16 00:27:03 +00:00
|
|
|
'attributes': { 'style': 'bullet' }
|
2012-10-23 00:53:58 +00:00
|
|
|
},
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'listItem' },
|
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 ) ),
|
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
|
2013-07-28 20:51:32 +00:00
|
|
|
expectedData = ve.copy( doc.getData() );
|
2013-03-01 01:20:59 +00:00
|
|
|
|
|
|
|
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 59, 65 ) );
|
|
|
|
fragment.rewrapNodes( 1, [ { 'type': 'heading', 'attributes': { 'level': 1 } } ] );
|
|
|
|
|
2013-02-16 00:27:03 +00:00
|
|
|
expectedData.splice( 59, 1, { 'type': 'heading', 'attributes': { 'level': 1 } } );
|
2013-03-01 01:20:59 +00:00
|
|
|
expectedData.splice( 61, 1, { 'type': '/heading' } );
|
2013-02-16 00:27:03 +00:00
|
|
|
expectedData.splice( 62, 1, { 'type': 'heading', 'attributes': { 'level': 1 } } );
|
2013-03-01 01:20:59 +00:00
|
|
|
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 ) ),
|
2013-07-28 20:51:32 +00:00
|
|
|
expectedData = ve.copy( doc.getData() );
|
2013-03-01 01:20:59 +00:00
|
|
|
|
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',
|
2013-02-16 00:27:03 +00:00
|
|
|
'attributes': { 'style': 'bullet' }
|
2012-10-23 00:53:58 +00:00
|
|
|
},
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'listItem' },
|
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',
|
2013-02-16 00:27:03 +00:00
|
|
|
'attributes': { 'style': 'bullet' }
|
2012-10-23 00:53:58 +00:00
|
|
|
},
|
2013-02-16 00:27:03 +00:00
|
|
|
{ 'type': 'listItem' },
|
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 ) );
|
|
|
|
|
2013-05-06 11:34:32 +00:00
|
|
|
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 ),
|
2013-02-16 00:27:03 +00:00
|
|
|
expectedFragment = new ve.dm.SurfaceFragment( expectedSurface, new ve.Range( 5, 37 ) );
|
2013-02-25 22:32:29 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
[
|
2013-06-05 10:47:47 +00:00
|
|
|
{ 'type': 'table' },
|
2013-02-25 22:32:29 +00:00
|
|
|
{ '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
|
|
|
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' } ] );
|
|
|
|
|
2013-02-16 00:27:03 +00:00
|
|
|
expectedData.splice( 0, 1, { 'type': 'paragraph' } );
|
2013-02-25 22:32:29 +00:00
|
|
|
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-07-03 01:30:10 +00:00
|
|
|
QUnit.test( 'isolateAndUnwrap', 1, function ( assert ) {
|
|
|
|
ve.test.utils.runIsolateTest( assert, 'heading', new ve.Range( 12, 20 ), function ( data ) {
|
2013-03-01 01:20:59 +00:00
|
|
|
data.splice( 11, 0, { 'type': 'listItem' } );
|
|
|
|
data.splice( 12, 1 );
|
2013-06-24 17:51:59 +00:00
|
|
|
data.splice( 20, 1, { 'type': '/listItem' } );
|
|
|
|
}, 'isolating paragraph in list item "Item 2" for heading' );
|
2013-02-21 19:20:43 +00:00
|
|
|
} );
|