1 | /* |
---|
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
3 | * Copyright (C) 2003-2007 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 | * FCKStyleDef Class: represents a single stylke definition. (IE specific) |
---|
22 | */ |
---|
23 | |
---|
24 | FCKStyleDef.prototype.ApplyToSelection = function() |
---|
25 | { |
---|
26 | var oSelection = FCK.ToolbarSet.CurrentInstance.EditorDocument.selection ; |
---|
27 | |
---|
28 | if ( oSelection.type == 'Text' ) |
---|
29 | { |
---|
30 | var oRange = oSelection.createRange() ; |
---|
31 | |
---|
32 | // Create the main element. |
---|
33 | var e = document.createElement( this.Element ) ; |
---|
34 | e.innerHTML = oRange.htmlText ; |
---|
35 | |
---|
36 | // Set the attributes. |
---|
37 | this._AddAttributes( e ) ; |
---|
38 | |
---|
39 | // Remove the duplicated elements. |
---|
40 | this._RemoveDuplicates( e ) ; |
---|
41 | |
---|
42 | // Replace the selection with the resulting HTML. |
---|
43 | oRange.pasteHTML( e.outerHTML ) ; |
---|
44 | } |
---|
45 | else if ( oSelection.type == 'Control' ) |
---|
46 | { |
---|
47 | var oControl = FCK.ToolbarSet.CurrentInstance.Selection.GetSelectedElement() ; |
---|
48 | if ( oControl.tagName == this.Element ) |
---|
49 | this._AddAttributes( oControl ) ; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | FCKStyleDef.prototype._AddAttributes = function( targetElement ) |
---|
54 | { |
---|
55 | for ( var a in this.Attributes ) |
---|
56 | { |
---|
57 | switch ( a.toLowerCase() ) |
---|
58 | { |
---|
59 | case 'style' : |
---|
60 | targetElement.style.cssText = this.Attributes[a] ; |
---|
61 | break ; |
---|
62 | |
---|
63 | case 'class' : |
---|
64 | targetElement.setAttribute( 'className', this.Attributes[a], 0 ) ; |
---|
65 | break ; |
---|
66 | |
---|
67 | case 'src' : |
---|
68 | targetElement.setAttribute( '_fcksavedurl', this.Attributes[a], 0 ) ; |
---|
69 | default : |
---|
70 | targetElement.setAttribute( a, this.Attributes[a], 0 ) ; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | FCKStyleDef.prototype._RemoveDuplicates = function( parent ) |
---|
76 | { |
---|
77 | for ( var i = 0 ; i < parent.children.length ; i++ ) |
---|
78 | { |
---|
79 | var oChild = parent.children[i] ; |
---|
80 | this._RemoveDuplicates( oChild ) ; |
---|
81 | |
---|
82 | if ( this.IsEqual( oChild ) ) |
---|
83 | FCKTools.RemoveOuterTags( oChild ) ; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | FCKStyleDef.prototype.IsEqual = function( e ) |
---|
88 | { |
---|
89 | if ( e.tagName != this.Element ) |
---|
90 | return false ; |
---|
91 | |
---|
92 | for ( var a in this.Attributes ) |
---|
93 | { |
---|
94 | switch ( a.toLowerCase() ) |
---|
95 | { |
---|
96 | case 'style' : |
---|
97 | if ( e.style.cssText.toLowerCase() != this.Attributes[a].toLowerCase() ) |
---|
98 | return false ; |
---|
99 | break ; |
---|
100 | case 'class' : |
---|
101 | if ( e.getAttribute( 'className', 0 ) != this.Attributes[a] ) |
---|
102 | return false ; |
---|
103 | break ; |
---|
104 | default : |
---|
105 | if ( e.getAttribute( a, 0 ) != this.Attributes[a] ) |
---|
106 | return false ; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return true ; |
---|
111 | } |
---|
112 | |
---|
113 | FCKStyleDef.prototype._RemoveMe = function( elementToCheck ) |
---|
114 | { |
---|
115 | if ( ! elementToCheck ) |
---|
116 | return ; |
---|
117 | |
---|
118 | var oParent = elementToCheck.parentElement ; |
---|
119 | |
---|
120 | if ( this.IsEqual( elementToCheck ) ) |
---|
121 | { |
---|
122 | if ( this.IsObjectElement ) |
---|
123 | { |
---|
124 | for ( var a in this.Attributes ) |
---|
125 | { |
---|
126 | switch ( a.toLowerCase() ) |
---|
127 | { |
---|
128 | case 'class' : |
---|
129 | elementToCheck.removeAttribute( 'className', 0 ) ; |
---|
130 | break ; |
---|
131 | default : |
---|
132 | elementToCheck.removeAttribute( a, 0 ) ; |
---|
133 | } |
---|
134 | } |
---|
135 | return ; |
---|
136 | } |
---|
137 | else |
---|
138 | FCKTools.RemoveOuterTags( elementToCheck ) ; |
---|
139 | } |
---|
140 | |
---|
141 | this._RemoveMe( oParent ) ; |
---|
142 | } |
---|