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. (Gecko specific)
22 */
23
24FCKStyleDef.prototype.ApplyToSelection = function()
25{
26	if ( FCKSelection.GetType() == 'Text' && !this.IsObjectElement )
27	{
28		var oSelection = FCK.ToolbarSet.CurrentInstance.EditorWindow.getSelection() ;
29
30		// Create the main element.
31		var e = FCK.ToolbarSet.CurrentInstance.EditorDocument.createElement( this.Element ) ;
32
33		for ( var i = 0 ; i < oSelection.rangeCount ; i++ )
34		{
35			e.appendChild( oSelection.getRangeAt(i).extractContents() ) ;
36		}
37
38		// Set the attributes.
39		this._AddAttributes( e ) ;
40
41		// Remove the duplicated elements.
42		this._RemoveDuplicates( e ) ;
43
44		var oRange = oSelection.getRangeAt(0) ;
45		oRange.insertNode( e ) ;
46	}
47	else
48	{
49		var oControl = FCK.ToolbarSet.CurrentInstance.Selection.GetSelectedElement() ;
50		if ( oControl.tagName == this.Element )
51			this._AddAttributes( oControl ) ;
52	}
53}
54
55FCKStyleDef.prototype._AddAttributes = function( targetElement )
56{
57	for ( var a in this.Attributes )
58	{
59		switch ( a.toLowerCase() )
60		{
61			case 'src' :
62				targetElement.setAttribute( '_fcksavedurl', this.Attributes[a], 0 ) ;
63			default :
64				targetElement.setAttribute( a, this.Attributes[a], 0 ) ;
65		}
66	}
67}
68
69FCKStyleDef.prototype._RemoveDuplicates = function( parent )
70{
71	for ( var i = 0 ; i < parent.childNodes.length ; i++ )
72	{
73		var oChild = parent.childNodes[i] ;
74
75		if ( oChild.nodeType != 1 )
76			continue ;
77
78		this._RemoveDuplicates( oChild ) ;
79
80		if ( this.IsEqual( oChild ) )
81			FCKTools.RemoveOuterTags( oChild ) ;
82	}
83}
84
85FCKStyleDef.prototype.IsEqual = function( e )
86{
87	if ( e.tagName != this.Element )
88		return false ;
89
90	for ( var a in this.Attributes )
91	{
92		if ( e.getAttribute( a ) != this.Attributes[a] )
93			return false ;
94	}
95
96	return true ;
97}
98
99FCKStyleDef.prototype._RemoveMe = function( elementToCheck )
100{
101	if ( ! elementToCheck )
102		return ;
103
104	var oParent = elementToCheck.parentNode ;
105
106	if ( elementToCheck.nodeType == 1 && this.IsEqual( elementToCheck ) )
107	{
108		if ( this.IsObjectElement )
109		{
110			for ( var a in this.Attributes )
111				elementToCheck.removeAttribute( a, 0 ) ;
112			return ;
113		}
114		else
115			FCKTools.RemoveOuterTags( elementToCheck ) ;
116	}
117
118	this._RemoveMe( oParent ) ;
119}