1 | /*
|
---|
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net
|
---|
3 | * Copyright (C) 2003-2009 Frederico Caldeira Knabben
|
---|
4 | *
|
---|
5 | * == BEGIN LICENSE ==
|
---|
6 | *
|
---|
7 | * Licensed under the terms of any of the following licenses at your
|
---|
8 | * choice:
|
---|
9 | *
|
---|
10 | * - GNU General Public License Version 2 or later (the "GPL")
|
---|
11 | * http://www.gnu.org/licenses/gpl.html
|
---|
12 | *
|
---|
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
|
---|
14 | * http://www.gnu.org/licenses/lgpl.html
|
---|
15 | *
|
---|
16 | * - Mozilla Public License Version 1.1 or later (the "MPL")
|
---|
17 | * http://www.mozilla.org/MPL/MPL-1.1.html
|
---|
18 | *
|
---|
19 | * == END LICENSE ==
|
---|
20 | *
|
---|
21 | * Class for working with a selection range, much like the W3C DOM Range, but
|
---|
22 | * it is not intended to be an implementation of the W3C interface.
|
---|
23 | * (Gecko Implementation)
|
---|
24 | */
|
---|
25 |
|
---|
26 | FCKDomRange.prototype.MoveToSelection = function()
|
---|
27 | {
|
---|
28 | this.Release( true ) ;
|
---|
29 |
|
---|
30 | var oSel = this.Window.getSelection() ;
|
---|
31 |
|
---|
32 | if ( oSel && oSel.rangeCount > 0 )
|
---|
33 | {
|
---|
34 | this._Range = FCKW3CRange.CreateFromRange( this.Window.document, oSel.getRangeAt(0) ) ;
|
---|
35 | this._UpdateElementInfo() ;
|
---|
36 | }
|
---|
37 | else
|
---|
38 | if ( this.Window.document )
|
---|
39 | this.MoveToElementStart( this.Window.document.body ) ;
|
---|
40 | }
|
---|
41 |
|
---|
42 | FCKDomRange.prototype.Select = function()
|
---|
43 | {
|
---|
44 | var oRange = this._Range ;
|
---|
45 | if ( oRange )
|
---|
46 | {
|
---|
47 | var startContainer = oRange.startContainer ;
|
---|
48 |
|
---|
49 | // If we have a collapsed range, inside an empty element, we must add
|
---|
50 | // something to it, otherwise the caret will not be visible.
|
---|
51 | if ( oRange.collapsed && startContainer.nodeType == 1 && startContainer.childNodes.length == 0 )
|
---|
52 | startContainer.appendChild( oRange._Document.createTextNode('') ) ;
|
---|
53 |
|
---|
54 | var oDocRange = this.Window.document.createRange() ;
|
---|
55 | oDocRange.setStart( startContainer, oRange.startOffset ) ;
|
---|
56 |
|
---|
57 | try
|
---|
58 | {
|
---|
59 | oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;
|
---|
60 | }
|
---|
61 | catch ( e )
|
---|
62 | {
|
---|
63 | // There is a bug in Firefox implementation (it would be too easy
|
---|
64 | // otherwise). The new start can't be after the end (W3C says it can).
|
---|
65 | // So, let's create a new range and collapse it to the desired point.
|
---|
66 | if ( e.toString().Contains( 'NS_ERROR_ILLEGAL_VALUE' ) )
|
---|
67 | {
|
---|
68 | oRange.collapse( true ) ;
|
---|
69 | oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;
|
---|
70 | }
|
---|
71 | else
|
---|
72 | throw( e ) ;
|
---|
73 | }
|
---|
74 |
|
---|
75 | var oSel = this.Window.getSelection() ;
|
---|
76 | oSel.removeAllRanges() ;
|
---|
77 |
|
---|
78 | // We must add a clone otherwise Firefox will have rendering issues.
|
---|
79 | oSel.addRange( oDocRange ) ;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Not compatible with bookmark created with CreateBookmark2.
|
---|
84 | // The bookmark nodes will be deleted from the document.
|
---|
85 | FCKDomRange.prototype.SelectBookmark = function( bookmark )
|
---|
86 | {
|
---|
87 | var domRange = this.Window.document.createRange() ;
|
---|
88 |
|
---|
89 | var startNode = this.GetBookmarkNode( bookmark, true ) ;
|
---|
90 | var endNode = this.GetBookmarkNode( bookmark, false ) ;
|
---|
91 |
|
---|
92 | domRange.setStart( startNode.parentNode, FCKDomTools.GetIndexOf( startNode ) ) ;
|
---|
93 | FCKDomTools.RemoveNode( startNode ) ;
|
---|
94 |
|
---|
95 | if ( endNode )
|
---|
96 | {
|
---|
97 | domRange.setEnd( endNode.parentNode, FCKDomTools.GetIndexOf( endNode ) ) ;
|
---|
98 | FCKDomTools.RemoveNode( endNode ) ;
|
---|
99 | }
|
---|
100 |
|
---|
101 | var selection = this.Window.getSelection() ;
|
---|
102 | selection.removeAllRanges() ;
|
---|
103 | selection.addRange( domRange ) ;
|
---|
104 | }
|
---|