70767b5a7172b741a95127fc036afe1e617f0e99.svn-base
6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
(function()
{
/**
* Represents a list os CKEDITOR.dom.range objects, which can be easily
* iterated sequentially.
* @constructor
* @param {CKEDITOR.dom.range|Array} [ranges] The ranges contained on this list.
* Note that, if an array of ranges is specified, the range sequence
* should match its DOM order. This class will not help to sort them.
*/
CKEDITOR.dom.rangeList = function( ranges )
{
if ( ranges instanceof CKEDITOR.dom.rangeList )
return ranges;
if ( !ranges )
ranges = [];
else if ( ranges instanceof CKEDITOR.dom.range )
ranges = [ ranges ];
return CKEDITOR.tools.extend( ranges, mixins );
};
var mixins =
/** @lends CKEDITOR.dom.rangeList.prototype */
{
/**
* Creates an instance of the rangeList iterator, it should be used
* only when the ranges processing could be DOM intrusive, which
* means it may pollute and break other ranges in this list.
* Otherwise, it's enough to just iterate over this array in a for loop.
* @returns {CKEDITOR.dom.rangeListIterator}
*/
createIterator : function()
{
var rangeList = this,
bookmark = CKEDITOR.dom.walker.bookmark(),
guard = function( node ) { return ! ( node.is && node.is( 'tr' ) ); },
bookmarks = [],
current;
/**
* @lends CKEDITOR.dom.rangeListIterator.prototype
*/
return {
/**
* Retrieves the next range in the list.
* @param {Boolean} mergeConsequent Whether join two adjacent ranges into single, e.g. consequent table cells.
*/
getNextRange : function( mergeConsequent )
{
current = current == undefined ? 0 : current + 1;
var range = rangeList[ current ];
// Multiple ranges might be mangled by each other.
if ( range && rangeList.length > 1 )
{
// Bookmarking all other ranges on the first iteration,
// the range correctness after it doesn't matter since we'll
// restore them before the next iteration.
if ( !current )
{
// Make sure bookmark correctness by reverse processing.
for ( var i = rangeList.length - 1; i >= 0; i-- )
bookmarks.unshift( rangeList[ i ].createBookmark( true ) );
}
if ( mergeConsequent )
{
// Figure out how many ranges should be merged.
var mergeCount = 0;
while ( rangeList[ current + mergeCount + 1 ] )
{
var doc = range.document,
found = 0,
left = doc.getById( bookmarks[ mergeCount ].endNode ),
right = doc.getById( bookmarks[ mergeCount + 1 ].startNode ),
next;
// Check subsequent range.
while ( 1 )
{
next = left.getNextSourceNode( false );
if ( !right.equals( next ) )
{
// This could be yet another bookmark or
// walking across block boundaries.
if ( bookmark( next ) || ( next.type == CKEDITOR.NODE_ELEMENT && next.isBlockBoundary() ) )
{
left = next;
continue;
}
}
else
found = 1;
break;
}
if ( !found )
break;
mergeCount++;
}
}
range.moveToBookmark( bookmarks.shift() );
// Merge ranges finally after moving to bookmarks.
while( mergeCount-- )
{
next = rangeList[ ++current ];
next.moveToBookmark( bookmarks.shift() );
range.setEnd( next.endContainer, next.endOffset );
}
}
return range;
}
};
},
createBookmarks : function( serializable )
{
var retval = [], bookmark;
for ( var i = 0; i < this.length ; i++ )
{
retval.push( bookmark = this[ i ].createBookmark( serializable, true) );
// Updating the container & offset values for ranges
// that have been touched.
for ( var j = i + 1; j < this.length; j++ )
{
this[ j ] = updateDirtyRange( bookmark, this[ j ] );
this[ j ] = updateDirtyRange( bookmark, this[ j ], true );
}
}
return retval;
},
createBookmarks2 : function( normalized )
{
var bookmarks = [];
for ( var i = 0 ; i < this.length ; i++ )
bookmarks.push( this[ i ].createBookmark2( normalized ) );
return bookmarks;
},
/**
* Move each range in the list to the position specified by a list of bookmarks.
* @param {Array} bookmarks The list of bookmarks, each one matching a range in the list.
*/
moveToBookmarks : function( bookmarks )
{
for ( var i = 0 ; i < this.length ; i++ )
this[ i ].moveToBookmark( bookmarks[ i ] );
}
};
// Update the specified range which has been mangled by previous insertion of
// range bookmark nodes.(#3256)
function updateDirtyRange( bookmark, dirtyRange, checkEnd )
{
var serializable = bookmark.serializable,
container = dirtyRange[ checkEnd ? 'endContainer' : 'startContainer' ],
offset = checkEnd ? 'endOffset' : 'startOffset';
var bookmarkStart = serializable ?
dirtyRange.document.getById( bookmark.startNode )
: bookmark.startNode;
var bookmarkEnd = serializable ?
dirtyRange.document.getById( bookmark.endNode )
: bookmark.endNode;
if ( container.equals( bookmarkStart.getPrevious() ) )
{
dirtyRange.startOffset = dirtyRange.startOffset
- container.getLength()
- bookmarkEnd.getPrevious().getLength();
container = bookmarkEnd.getNext();
}
else if ( container.equals( bookmarkEnd.getPrevious() ) )
{
dirtyRange.startOffset = dirtyRange.startOffset - container.getLength();
container = bookmarkEnd.getNext();
}
container.equals( bookmarkStart.getParent() ) && dirtyRange[ offset ]++;
container.equals( bookmarkEnd.getParent() ) && dirtyRange[ offset ]++;
// Update and return this range.
dirtyRange[ checkEnd ? 'endContainer' : 'startContainer' ] = container;
return dirtyRange;
}
})();
/**
* (Virtual Class) Do not call this constructor. This class is not really part
* of the API. It just describes the return type of {@link CKEDITOR.dom.rangeList#createIterator}.
* @name CKEDITOR.dom.rangeListIterator
* @constructor
* @example
*/