Skip to content

Commit

Permalink
Added major tests package for node getNextSourceNode/getPreviousSourc…
Browse files Browse the repository at this point in the history
…eNode methods.
  • Loading branch information
mlewand committed Jul 14, 2014
1 parent 6a7af8c commit 66fe5e6
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 71 deletions.
34 changes: 21 additions & 13 deletions tests/core/dom/node.html
Expand Up @@ -14,19 +14,19 @@ <h1>Title</h1>
<div><span><span><span><span><i id="getAddress2"></i></span></span></span></span></div>
<iframe id="getDocument"></iframe>
<div id="getNSN">
<i id="getNSN1">1</i>
<i id="getNSN2">2</i>
<i id="getNSN3">3</i>
<!--comment1-->
<!--comment2-->
<i id="getNSN4">4</i>
<i id="getNSN5">5
<span id="getNSN5.1">
5.1
<!--comment3-->
</span></i>
<!--comment4-->
<i id="getNSN6">6</i>
<i id="getNSN1">1</i>
<i id="getNSN2">2</i>
<i id="getNSN3">3</i>
<!--comment1-->
<!--comment2-->
<i id="getNSN4">4</i>
<i id="getNSN5">5
<span id="getNSN5.1">
5.1
<!--comment3-->
</span></i>
<!--comment4-->
<i id="getNSN6">6</i>
</div>
<div id="getNSN_Samples">
<p id="paragraph1">
Expand All @@ -39,6 +39,14 @@ <h1>Title</h1>
second paragraph
</p>
</div>
<!--
b#b1
<!-comment->
b#b2
b#b2.1
b#b3
-->
<div id ="nodeTraversing"><b id="b1">1</b><!--comment--><b id="b2">2<b id="b2.1">2.1</b></b><b id="b3">3</b></div>
<div id="move"><p id="move1"></p><p id="move2"></p><p id="move3"></p><p id="move4"></p></div>
<div id="remove"><p id="remove1"><i></i>text<!--comment--></p></div>
<div id="replace">1<p id="replace1"></p>2<p id="replace2"></p>3</div>
Expand Down
264 changes: 206 additions & 58 deletions tests/core/dom/node.js
Expand Up @@ -550,83 +550,231 @@
assert.areEqual( 1, node2.getIndex( true ) );
},

test_getNextSourceNode: function() {
var node = $( 'getNSN1' );
'test getNextSourceNode - empty node with no parent': function() {
// Asserts that no exception is thrown when no parent is available.
var node = new CKEDITOR.dom.element( 'a' );
assert.isNull( node.getNextSourceNode() );
},

'test getNextSourceNode - next node': function() {
var node = $( 'b1' );
assert.areSame( node.getFirst(), node.getNextSourceNode(), 'Text node is first following node' );
},

'test getNextSourceNode - sibling node': function() {
var node = $( 'b2' ).getFirst(); // A Text node, which has a sibling element.
assert.areSame( node.getNext(), node.getNextSourceNode() );
},

'test getNextSourceNode - next node placed outside of parent': function() {
var node = $( 'b1' ).getFirst(); // A text node in b#b1.
// Since b#b1 does not have any more children, it should return node next to the b#b1.
assert.areSame( $( 'b1' ).getNext(), node.getNextSourceNode(), 'Result is a next node to the #b1' );
},

'test getNextSourceNode - start from sibling - next node': function() {
var node = $( 'b2' ).getPrevious(); // A comment node.
assert.areSame( $( 'b2' ), node.getNextSourceNode( true ) );
},

'test getNextSourceNode - filtering - element node': function() {
var node = $( 'b1' );
// We'll expect closest element node which is b#b2.
assert.areEqual( $( 'b2' ), node.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) );
},

'test getNextSourceNode - filtering - comment node': function() {
var node = $( 'b1' ),
expectedComment = $( 'b1' ).getNext();
// We'll expect closest comment node which is next to the b#b1.
assert.areEqual( expectedComment, node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT ) );
},

'test getNextSourceNode - guard as a function': function() {
var node = $( 'b1' ),
falseGuard = function() {
return false;
},
trueGuard = function() {
return true;
};

assert.isNull( node.getNextSourceNode( false, null, falseGuard ), 'Guard returns false, no result is returned' );
assert.areSame( node.getFirst(), node.getNextSourceNode( false, null, trueGuard ), 'Guard returns true, first child is returned' );
},

'test getNextSourceNode - guard': function() {
// Guard can be both function and node. We will use both modes, as an equivalent calls,
// and ensure that results are the same.
var node = $( 'b1' ),
guardNode = node.getFirst(),
guardFunction = function( el ) {
assert.isInstanceOf( CKEDITOR.dom.node, el, 'Guard argument must be a node instance' );
return !el.equals( guardNode );
};

assert.isNull( node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, guardNode ), 'By node - Guard node is not included in the result' );
assert.isNull( node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, guardFunction ), 'By function - Guard node is not included in the result' );
},

'test getNextSourceNode - guard by the node itself (edge case)': function() {
// Same as 'test getNextSourceNode - guard', but we'll use node itself as its guard.
// That should return null.
// Why should return null? For consitency with 'test getNextSourceNode - guard - boundary'
var node = $( 'b1' ),
guardNode = node,
guardFunction = function( el ) {
return !el.equals( guardNode );
};

assert.isNull( node.getNextSourceNode( false, null, guardNode ), 'By node - Guard node is not included in the result' );
assert.isNull( node.getNextSourceNode( false, null, guardFunction ), 'By function - Guard node is not included in the result' );

// Now the same thing but starting from next sibling (and changing guard to that sibling).
node = node.getNext();
guardNode = node;
// No need to update guardFunction, because guardNode is updated.
assert.isNull( node.getNextSourceNode( false, null, guardNode ), 'By node - from next sibling' );
assert.isNull( node.getNextSourceNode( false, null, guardFunction ), 'By function - from next sibling' );
},

'test getNextSourceNode - guard after expected node': function() {
var node = $( 'b1' ),
expectedNode = node.getNext(),
guardNode = node.getNext().getNext(),
guardFunction = function( el ) {
return !el.equals( guardNode );
};

assert.areSame( expectedNode, node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, guardNode ), 'By node' );
assert.areSame( expectedNode, node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, guardFunction ), 'By function' );
},

'test getPreviousSourceNode - empty node with no parent': function() {
// Asserts that no exception is thrown when no parent is available.
var node = new CKEDITOR.dom.element( 'a' );

assert.isNull( node.getPreviousSourceNode() );
},

'test getPreviousSourceNode - prev node': function() {
var node = $( 'b2' ),
// It can be simply previous, because it's a comment node, so it does not have children.
expectedNode = node.getPrevious();

assert.areSame( expectedNode, node.getPreviousSourceNode(), 'Returns the previous, sibling node' );
},

'test getPreviousSourceNode - parent node': function() {
var node = $( 'b2' ).getFirst();

assert.areSame( $( 'b2' ), node.getPreviousSourceNode(), 'Returns the parent node' );
},

'test getPreviousSourceNode - prev sibling last node': function() {
// In this case the very last node of preceeding element should be returned.
var node = $( 'b3' ),
// The very last node in b#b2 is text within b#b2.1 element.
expectedNode = $( 'b2.1' ).getLast();

assert.areSame( expectedNode, node.getPreviousSourceNode(), 'Returns the parent node' );
},

'test getPreviousSourceNode - start from sibling - prev node': function() {
var node = $( 'b2' ),
// Should be the last child from b#b1, because we should start from prev sibling
// which is a comment node.
expectedNode = $( 'b1' ).getLast();

assert.areSame( expectedNode, node.getPreviousSourceNode( true ) );
},

'test getPreviousSourceNode - filtering - element node': function() {
var node = $( 'b3' );
// We'll expect closest (moving up the DOM tree) element node which is b#b2.1.
assert.areEqual( $( 'b2.1' ), node.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) );
},

'test getPreviousSourceNode - filtering - comment node': function() {
var node = $( 'b3' ),
expectedComment = $( 'b1' ).getNext();
// We'll expect closest comment node which is next to b#b1.
assert.areEqual( expectedComment, node.getPreviousSourceNode( false, CKEDITOR.NODE_COMMENT ) );
},

assert.areSame( $( 'getNSN1' ).getFirst(), node.getNextSourceNode() );

// These asserts use startFromSibling.
assert.areSame( CKEDITOR.NODE_TEXT, node.getNextSourceNode( true ).type );
assert.areSame( $( 'getNSN2' ), node.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ) );
assert.areSame( 'comment1', node.getNextSourceNode( true, CKEDITOR.NODE_COMMENT ).$.nodeValue );
assert.isNull( node.getNextSourceNode( true, CKEDITOR.NODE_COMMENT, $( 'getNSN3' ) ) );

// Fetching nested nodes.
node = $( 'getNSN5' );
assert.areSame( node.getFirst(), node.getNextSourceNode() );
assert.areSame( $( 'getNSN5.1' ), node.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) );
assert.areSame( 'comment3', node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT ).$.nodeValue );

// Tests what will happen if we have no next sibling.
// Should return next item after its parent, here it's a text node after i#getNSN5.
node = $( 'getNSN5.1' );
assert.areSame( $( 'getNSN5' ).getNext(), node.getNextSourceNode( true ) );

// Test guard parameter.
assert.isNull( node.getNextSourceNode( false, null, function( matchingNode ) {
// We should stop in the same node that we start.
return matchingNode.equals( node );
} ), 'shouldn\'t find anything' );

node = $( 'getNSN5' );
var comment4Node = $( 'getNSN5.1' ).getFirst().getNext(),
// This guard will stop on comment 4 node.
comment4guard = function( matchingNode ) {
return !matchingNode.equals( comment4Node );
'test getPreviousSourceNode - guard as a function': function() {
var node = $( 'b1' ),
falseGuard = function() {
return false;
},
trueGuard = function() {
return true;
};

// Boundary case:
assert.areSame( node.getFirst(), node.getNextSourceNode( false, null, function( el ) {
return !el.equals( node );
} ) );
assert.areSame( node.getFirst(), node.getNextSourceNode( false, null, node ) );
assert.isNull( node.getPreviousSourceNode( false, null, falseGuard ), 'Guard returns false, no result is returned' );
assert.areSame( node.getFirst(), node.getPreviousSourceNode( false, null, trueGuard ), 'Guard returns true, prev element is returned' );
},

'test getPreviousSourceNode - guard': function() {
// Guard can be both function and node. We will use both modes, as an equivalent calls,
// and ensure that results are the same.
// In this case no result should be returned because we use previous node as a guard.
var node = $( 'b2' ),
guardNode = node.getPrevious(),
guardFunction = function( el ) {
assert.isInstanceOf( CKEDITOR.dom.node, el, 'Guard argument must be a node instance' );
return !el.equals( guardNode );
};

assert.isNull( node.getPreviousSourceNode( false, CKEDITOR.NODE_COMMENT, guardNode ), 'By node - Guard node is not included in the result' );
assert.isNull( node.getPreviousSourceNode( false, CKEDITOR.NODE_COMMENT, guardFunction ), 'By function - Guard node is not included in the result' );
},

'test getPreviousSourceNode - guard by the node itself (edge case)': function() {
// Same as 'test getPreviousSourceNode - guard', but we'll use node itself as its guard.
// That should return null.
// Why should return null? For consitency with 'test getPreviousSourceNode - guard - boundary'
var node = $( 'b1' ),
guardNode = node,
guardFunction = function( el ) {
return !el.equals( guardNode );
};

assert.isNull( node.getPreviousSourceNode( false, null, guardNode ), 'By node - Guard node is not included in the result' );
assert.isNull( node.getPreviousSourceNode( false, null, guardFunction ), 'By function - Guard node is not included in the result' );

// Looking for text node in given fragment.
assert.areSame( node.getFirst(), node.getNextSourceNode( false, CKEDITOR.NODE_TEXT, comment4Node ), 'guard obj' );
assert.areSame( node.getFirst(), node.getNextSourceNode( false, CKEDITOR.NODE_TEXT, comment4guard ), 'guard function' );
// Now the same thing but starting from previous sibling (and changing guard to that sibling).
node = node.getPrevious();
guardNode = node;
// No need to update guardFunction, because guardNode is updated.
assert.isNull( node.getPreviousSourceNode( false, null, guardNode ), 'By node - from previous sibling' );
assert.isNull( node.getPreviousSourceNode( false, null, guardFunction ), 'By function - from previous sibling' );
},

// This function won't search anything at all.
assert.isNull( node.getNextSourceNode( false, CKEDITOR.NODE_TEXT, function() {
return false;
} ), 'guard function' );
'test getPreviousSourceNode - guard after expected node': function() {
var node = $( 'b2' ),
expectedNode = node.getPrevious(),
guardNode = $( 'b1' ),
guardFunction = function( el ) {
return !el.equals( guardNode );
};

// Looking for comment, which is `guard` element so should not be found.
assert.isNull( node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, comment4Node ), 'guard obj' );
assert.isNull( node.getNextSourceNode( false, CKEDITOR.NODE_COMMENT, comment4guard ), 'guard function' );
assert.areSame( expectedNode, node.getPreviousSourceNode( false, CKEDITOR.NODE_COMMENT, guardNode ), 'By node' );
assert.areSame( expectedNode, node.getPreviousSourceNode( false, CKEDITOR.NODE_COMMENT, guardFunction ), 'By function' );
},

'test getNextSourceNode docs sample': function() {
var paragraph1 = CKEDITOR.document.getById( 'paragraph1' ),
paragraph2 = CKEDITOR.document.getById( 'paragraph2' );

assert.areSame( paragraph1.getFirst(), paragraph1.getNextSourceNode() );
assert.areSame( paragraph1.getChildren().getItem( 1 ), paragraph1.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) );
assert.areSame( paragraph1.getFirst(), paragraph1.getNextSourceNode() );
assert.areSame( paragraph1.getChildren().getItem( 3 ), paragraph1.getNextSourceNode( false, CKEDITOR.NODE_COMMENT ) );
assert.areSame( paragraph2, paragraph1.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ) );
assert.areSame( paragraph1.getNext(), paragraph1.getNextSourceNode( true ) );
assert.areSame( null, paragraph1.getNextSourceNode( false, null, paragraph1.getFirst() ) );
},

test_getPreviousSourceNode: function() {
var node = $( 'getNSN6' );
assert.areSame( CKEDITOR.NODE_TEXT, node.getPreviousSourceNode( true ).type );
assert.areSame( $( 'getNSN5' ), node.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) );
assert.areSame( 'comment4', node.getPreviousSourceNode( true, CKEDITOR.NODE_COMMENT ).$.nodeValue );

node = $( 'getNSN5.1' );
assert.isNull( node.getPreviousSourceNode( true, CKEDITOR.NODE_COMMENT, $( 'getNSN4' ) ) );
},

test_getParent: function() {
var node = $( 'getNSN1' );
assert.areSame( $( 'getNSN' ), node.getParent() );
Expand Down

0 comments on commit 66fe5e6

Please sign in to comment.