Home directory for Malawi's wwwroot
Duncan Ewan
2021-02-19 3e758c29e0fde36fc088efcfc88f9a3014432b64
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * This class partially implements the W3C DOM Range for browser that don't
 * support the standards (like IE):
 * http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
 */
 
var FCKW3CRange = function( parentDocument )
{
    this._Document = parentDocument ;
 
    this.startContainer    = null ;
    this.startOffset    = null ;
    this.endContainer    = null ;
    this.endOffset        = null ;
    this.collapsed        = true ;
}
 
FCKW3CRange.CreateRange = function( parentDocument )
{
    // We could opt to use the Range implementation of the browsers. The problem
    // is that every browser have different bugs on their implementations,
    // mostly related to different interpretations of the W3C specifications.
    // So, for now, let's use our implementation and pray for browsers fixings
    // soon. Otherwise will go crazy on trying to find out workarounds.
    /*
    // Get the browser implementation of the range, if available.
    if ( parentDocument.createRange )
    {
        var range = parentDocument.createRange() ;
        if ( typeof( range.startContainer ) != 'undefined' )
            return range ;
    }
    */
    return new FCKW3CRange( parentDocument ) ;
}
 
FCKW3CRange.CreateFromRange = function( parentDocument, sourceRange )
{
    var range = FCKW3CRange.CreateRange( parentDocument ) ;
    range.setStart( sourceRange.startContainer, sourceRange.startOffset ) ;
    range.setEnd( sourceRange.endContainer, sourceRange.endOffset ) ;
    return range ;
}
 
FCKW3CRange.prototype =
{
 
    _UpdateCollapsed : function()
    {
      this.collapsed = ( this.startContainer == this.endContainer && this.startOffset == this.endOffset ) ;
    },
 
    // W3C requires a check for the new position. If it is after the end
    // boundary, the range should be collapsed to the new start. It seams we
    // will not need this check for our use of this class so we can ignore it for now.
    setStart : function( refNode, offset )
    {
        this.startContainer    = refNode ;
        this.startOffset    = offset ;
 
        if ( !this.endContainer )
        {
            this.endContainer    = refNode ;
            this.endOffset        = offset ;
        }
 
        this._UpdateCollapsed() ;
    },
 
    // W3C requires a check for the new position. If it is before the start
    // boundary, the range should be collapsed to the new end. It seams we
    // will not need this check for our use of this class so we can ignore it for now.
    setEnd : function( refNode, offset )
    {
        this.endContainer    = refNode ;
        this.endOffset        = offset ;
 
        if ( !this.startContainer )
        {
            this.startContainer    = refNode ;
            this.startOffset    = offset ;
        }
 
        this._UpdateCollapsed() ;
    },
 
    setStartAfter : function( refNode )
    {
        this.setStart( refNode.parentNode, FCKDomTools.GetIndexOf( refNode ) + 1 ) ;
    },
 
    setStartBefore : function( refNode )
    {
        this.setStart( refNode.parentNode, FCKDomTools.GetIndexOf( refNode ) ) ;
    },
 
    setEndAfter : function( refNode )
    {
        this.setEnd( refNode.parentNode, FCKDomTools.GetIndexOf( refNode ) + 1 ) ;
    },
 
    setEndBefore : function( refNode )
    {
        this.setEnd( refNode.parentNode, FCKDomTools.GetIndexOf( refNode ) ) ;
    },
 
    collapse : function( toStart )
    {
        if ( toStart )
        {
            this.endContainer    = this.startContainer ;
            this.endOffset        = this.startOffset ;
        }
        else
        {
            this.startContainer    = this.endContainer ;
            this.startOffset    = this.endOffset ;
        }
 
        this.collapsed = true ;
    },
 
    selectNodeContents : function( refNode )
    {
        this.setStart( refNode, 0 ) ;
        this.setEnd( refNode, refNode.nodeType == 3 ? refNode.data.length : refNode.childNodes.length ) ;
    },
 
    insertNode : function( newNode )
    {
        var startContainer = this.startContainer ;
        var startOffset = this.startOffset ;
 
        // If we are in a text node.
        if ( startContainer.nodeType == 3 )
        {
            startContainer.splitText( startOffset ) ;
 
            // Check if it is necessary to update the end boundary.
            if ( startContainer == this.endContainer )
                this.setEnd( startContainer.nextSibling, this.endOffset - this.startOffset ) ;
 
            // Insert the new node it after the text node.
            FCKDomTools.InsertAfterNode( startContainer, newNode ) ;
 
            return ;
        }
        else
        {
            // Simply insert the new node before the current start node.
            startContainer.insertBefore( newNode, startContainer.childNodes[ startOffset ] || null ) ;
 
            // Check if it is necessary to update the end boundary.
            if ( startContainer == this.endContainer )
            {
                this.endOffset++ ;
                this.collapsed = false ;
            }
        }
    },
 
    deleteContents : function()
    {
        if ( this.collapsed )
            return ;
 
        this._ExecContentsAction( 0 ) ;
    },
 
    extractContents : function()
    {
        var docFrag = new FCKDocumentFragment( this._Document ) ;
 
        if ( !this.collapsed )
            this._ExecContentsAction( 1, docFrag ) ;
 
        return docFrag ;
    },
 
    // The selection may be lost when cloning (due to the splitText() call).
    cloneContents : function()
    {
        var docFrag = new FCKDocumentFragment( this._Document ) ;
 
        if ( !this.collapsed )
            this._ExecContentsAction( 2, docFrag ) ;
 
        return docFrag ;
    },
 
    _ExecContentsAction : function( action, docFrag )
    {
        var startNode    = this.startContainer ;
        var endNode        = this.endContainer ;
 
        var startOffset    = this.startOffset ;
        var endOffset    = this.endOffset ;
 
        var removeStartNode    = false ;
        var removeEndNode    = false ;
 
        // Check the start and end nodes and make the necessary removals or changes.
 
        // Start from the end, otherwise DOM mutations (splitText) made in the
        // start boundary may interfere on the results here.
 
        // For text containers, we must simply split the node and point to the
        // second part. The removal will be handled by the rest of the code .
        if ( endNode.nodeType == 3 )
            endNode = endNode.splitText( endOffset ) ;
        else
        {
            // If the end container has children and the offset is pointing
            // to a child, then we should start from it.
            if ( endNode.childNodes.length > 0 )
            {
                // If the offset points after the last node.
                if ( endOffset > endNode.childNodes.length - 1 )
                {
                    // Let's create a temporary node and mark it for removal.
                    endNode = FCKDomTools.InsertAfterNode( endNode.lastChild, this._Document.createTextNode('') ) ;
                    removeEndNode = true ;
                }
                else
                    endNode = endNode.childNodes[ endOffset ] ;
            }
        }
 
        // For text containers, we must simply split the node. The removal will
        // be handled by the rest of the code .
        if ( startNode.nodeType == 3 )
        {
            startNode.splitText( startOffset ) ;
 
            // In cases the end node is the same as the start node, the above
            // splitting will also split the end, so me must move the end to
            // the second part of the split.
            if ( startNode == endNode )
                endNode = startNode.nextSibling ;
        }
        else
        {
            // If the start container has children and the offset is pointing
            // to a child, then we should start from its previous sibling.
 
            // If the offset points to the first node, we don't have a
            // sibling, so let's use the first one, but mark it for removal.
            if ( startOffset == 0 )
            {
                // Let's create a temporary node and mark it for removal.
                startNode = startNode.insertBefore( this._Document.createTextNode(''), startNode.firstChild ) ;
                removeStartNode = true ;
            }
            else if ( startOffset > startNode.childNodes.length - 1 )
            {
                // Let's create a temporary node and mark it for removal.
                startNode = startNode.appendChild( this._Document.createTextNode('') ) ;
                removeStartNode = true ;
            }
            else
                startNode = startNode.childNodes[ startOffset ].previousSibling ;
        }
 
        // Get the parent nodes tree for the start and end boundaries.
        var startParents    = FCKDomTools.GetParents( startNode ) ;
        var endParents        = FCKDomTools.GetParents( endNode ) ;
 
        // Compare them, to find the top most siblings.
        var i, topStart, topEnd ;
 
        for ( i = 0 ; i < startParents.length ; i++ )
        {
            topStart    = startParents[i] ;
            topEnd        = endParents[i] ;
 
            // The compared nodes will match until we find the top most
            // siblings (different nodes that have the same parent).
            // "i" will hold the index in the parents array for the top
            // most element.
            if ( topStart != topEnd )
                break ;
        }
 
        var clone, levelStartNode, levelClone, currentNode, currentSibling ;
 
        if ( docFrag )
            clone = docFrag.RootNode ;
 
        // Remove all successive sibling nodes for every node in the
        // startParents tree.
        for ( var j = i ; j < startParents.length ; j++ )
        {
            levelStartNode = startParents[j] ;
 
            // For Extract and Clone, we must clone this level.
            if ( clone && levelStartNode != startNode )        // action = 0 = Delete
                levelClone = clone.appendChild( levelStartNode.cloneNode( levelStartNode == startNode ) ) ;
 
            currentNode = levelStartNode.nextSibling ;
 
            while( currentNode )
            {
                // Stop processing when the current node matches a node in the
                // endParents tree or if it is the endNode.
                if ( currentNode == endParents[j] || currentNode == endNode )
                    break ;
 
                // Cache the next sibling.
                currentSibling = currentNode.nextSibling ;
 
                // If cloning, just clone it.
                if ( action == 2 )    // 2 = Clone
                    clone.appendChild( currentNode.cloneNode( true ) ) ;
                else
                {
                    // Both Delete and Extract will remove the node.
                    currentNode.parentNode.removeChild( currentNode ) ;
 
                    // When Extracting, move the removed node to the docFrag.
                    if ( action == 1 )    // 1 = Extract
                        clone.appendChild( currentNode ) ;
                }
 
                currentNode = currentSibling ;
            }
 
            if ( clone )
                clone = levelClone ;
        }
 
        if ( docFrag )
            clone = docFrag.RootNode ;
 
        // Remove all previous sibling nodes for every node in the
        // endParents tree.
        for ( var k = i ; k < endParents.length ; k++ )
        {
            levelStartNode = endParents[k] ;
 
            // For Extract and Clone, we must clone this level.
            if ( action > 0 && levelStartNode != endNode )        // action = 0 = Delete
                levelClone = clone.appendChild( levelStartNode.cloneNode( levelStartNode == endNode ) ) ;
 
            // The processing of siblings may have already been done by the parent.
            if ( !startParents[k] || levelStartNode.parentNode != startParents[k].parentNode )
            {
                currentNode = levelStartNode.previousSibling ;
 
                while( currentNode )
                {
                    // Stop processing when the current node matches a node in the
                    // startParents tree or if it is the startNode.
                    if ( currentNode == startParents[k] || currentNode == startNode )
                        break ;
 
                    // Cache the next sibling.
                    currentSibling = currentNode.previousSibling ;
 
                    // If cloning, just clone it.
                    if ( action == 2 )    // 2 = Clone
                        clone.insertBefore( currentNode.cloneNode( true ), clone.firstChild ) ;
                    else
                    {
                        // Both Delete and Extract will remove the node.
                        currentNode.parentNode.removeChild( currentNode ) ;
 
                        // When Extracting, mode the removed node to the docFrag.
                        if ( action == 1 )    // 1 = Extract
                            clone.insertBefore( currentNode, clone.firstChild ) ;
                    }
 
                    currentNode = currentSibling ;
                }
            }
 
            if ( clone )
                clone = levelClone ;
        }
 
        if ( action == 2 )        // 2 = Clone.
        {
            // No changes in the DOM should be done, so fix the split text (if any).
 
            var startTextNode = this.startContainer ;
            if ( startTextNode.nodeType == 3 )
            {
                startTextNode.data += startTextNode.nextSibling.data ;
                startTextNode.parentNode.removeChild( startTextNode.nextSibling ) ;
            }
 
            var endTextNode = this.endContainer ;
            if ( endTextNode.nodeType == 3 && endTextNode.nextSibling )
            {
                endTextNode.data += endTextNode.nextSibling.data ;
                endTextNode.parentNode.removeChild( endTextNode.nextSibling ) ;
            }
        }
        else
        {
            // Collapse the range.
 
            // If a node has been partially selected, collapse the range between
            // topStart and topEnd. Otherwise, simply collapse it to the start. (W3C specs).
            if ( topStart && topEnd && ( startNode.parentNode != topStart.parentNode || endNode.parentNode != topEnd.parentNode ) )
            {
                var endIndex = FCKDomTools.GetIndexOf( topEnd ) ;
 
                // If the start node is to be removed, we must correct the
                // index to reflect the removal.
                if ( removeStartNode && topEnd.parentNode == startNode.parentNode )
                    endIndex-- ;
 
                this.setStart( topEnd.parentNode, endIndex ) ;
            }
 
            // Collapse it to the start.
            this.collapse( true ) ;
        }
 
        // Cleanup any marked node.
        if( removeStartNode )
            startNode.parentNode.removeChild( startNode ) ;
 
        if( removeEndNode && endNode.parentNode )
            endNode.parentNode.removeChild( endNode ) ;
    },
 
    cloneRange : function()
    {
        return FCKW3CRange.CreateFromRange( this._Document, this ) ;
    }
} ;