Changeset 1504

Show
Ignore:
Timestamp:
2008-02-11 19:14:35 (6 months ago)
Author:
alfonsoml
Message:

Fix #676 and #738, form elements in IE are quite special when you create them.

Location:
FCKeditor/trunk
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • FCKeditor/trunk/editor/dialog/common/fck_dialog_common.js

    r1398 r1504  
    207207                window.open( url, 'FCKBrowseWindow', sOptions ) ; 
    208208} 
     209 
     210/** 
     211 Utility function to create/update an element with a name attribute in IE, so it behaves properly when moved around 
     212 It also allows to change the name or other special attributes in an existing node 
     213        oEditor : instance of FCKeditor where the element will be created 
     214        oOriginal : current element being edited or null if it has to be created 
     215        nodeName : string with the name of the element to create 
     216        oAttributes : Hash object with the attributes that must be set at creation time in IE 
     217                                                                Those attributes will be set also after the element has been 
     218                                                                created for any other browser to avoid redudant code 
     219*/ 
     220function CreateNamedElement( oEditor, oOriginal, nodeName, oAttributes ) 
     221{ 
     222        var oNewNode ; 
     223 
     224        // IE doesn't allow easily to change properties of an existing object,  
     225        // so remove the old and force the creation of a new one. 
     226        var oldNode = null ; 
     227        if ( oOriginal && oEditor.FCKBrowserInfo.IsIE ) 
     228        { 
     229                // Force the creation only if some of the special attributes have changed: 
     230                var bChanged = false; 
     231                for( var attName in oAttributes ) 
     232                        bChanged |= ( oOriginal.getAttribute( attName, 2) != oAttributes[attName] ) ;    
     233 
     234                if ( bChanged ) 
     235                { 
     236                        oldNode = oOriginal ; 
     237                        oOriginal = null ; 
     238                } 
     239        } 
     240 
     241        // If the node existed (and it's not IE), then we just have to update its attributes 
     242        if ( oOriginal ) 
     243        { 
     244                oNewNode = oOriginal ; 
     245        } 
     246        else 
     247        { 
     248                // #676, IE doesn't play nice with the name or type attribute 
     249                if ( oEditor.FCKBrowserInfo.IsIE ) 
     250                { 
     251                        var sbHTML = [] ; 
     252                        sbHTML.push( '<' + nodeName ) ; 
     253                        for( var prop in oAttributes ) 
     254                        { 
     255                                sbHTML.push( ' ' + prop + '="' + oAttributes[prop] + '"' ) ; 
     256                        } 
     257                        sbHTML.push( '>' ) ; 
     258                        if ( !oEditor.FCKListsLib.EmptyElements[nodeName.toLowerCase()] ) 
     259                                sbHTML.push( '</' + nodeName + '>' ) ; 
     260 
     261                        oNewNode = oEditor.FCK.EditorDocument.createElement( sbHTML.join('') ) ; 
     262                        // Check if we are just changing the properties of an existing node: copy its properties 
     263                        if ( oldNode ) 
     264                        { 
     265                                CopyAttributes( oldNode, oNewNode, oAttributes ) ; 
     266                                MoveContents( oldNode, oNewNode ) ; 
     267                                oldNode.parentNode.removeChild( oldNode ) ; 
     268                                oldNode = null ; 
     269 
     270                                if ( oEditor.FCKDialog.SelectionData ) 
     271                                { 
     272                                        // Trick to refresh the selection object and avoid error in fckdialog.html Selection.EnsureSelection 
     273                                        var oSel = oEditor.FCK.EditorDocument.selection ; 
     274                                        oEditor.FCKDialog.SelectionData = oSel.createRange() ; // Now oSel.type will be 'None' reflecting the real situation 
     275                                } 
     276                        } 
     277                        oNewNode = oEditor.FCK.InsertElement( oNewNode ) ; 
     278 
     279                        // FCKDialog.SelectionData is broken by now since we've deleted the previously selected element. 
     280                        // So we need to reassign it. 
     281                        if ( oEditor.FCKDialog.SelectionData ) 
     282                        { 
     283                                var range = oEditor.FCK.EditorDocument.body.createControlRange() ; 
     284                                range.add( oNewNode ) ; 
     285                                oEditor.FCKDialog.SelectionData = range ; 
     286                        } 
     287                } 
     288                else 
     289                { 
     290                        oNewNode = oEditor.FCK.InsertElement( nodeName ) ; 
     291                } 
     292        }        
     293 
     294        // Set the basic attributes 
     295        for( var attName in oAttributes ) 
     296                oNewNode.setAttribute( attName, oAttributes[attName], 0 ) ;     // 0 : Case Insensitive 
     297 
     298        return oNewNode ; 
     299} 
     300 
     301// Copy all the attributes from one node to the other, kinda like a clone 
     302// But oSkipAttributes is an object with the attributes that must NOT be copied 
     303function CopyAttributes( oSource, oDest, oSkipAttributes ) 
     304{ 
     305        var aAttributes = oSource.attributes ; 
     306 
     307        for ( var n = 0 ; n < aAttributes.length ; n++ ) 
     308        { 
     309                var oAttribute = aAttributes[n] ; 
     310 
     311                if ( oAttribute.specified ) 
     312                { 
     313                        var sAttName = oAttribute.nodeName ; 
     314                        // We can set the type only once, so do it with the proper value, not copying it. 
     315                        if ( sAttName in oSkipAttributes ) 
     316                                continue ; 
     317 
     318                        var sAttValue = oSource.getAttribute( sAttName, 2 ) ; 
     319                        if ( sAttValue == null ) 
     320                                sAttValue = oAttribute.nodeValue ; 
     321 
     322                        oDest.setAttribute( sAttName, sAttValue, 0 ) ;  // 0 : Case Insensitive 
     323                } 
     324        } 
     325        // The style: 
     326        oDest.style.cssText = oSource.style.cssText ; 
     327} 
     328 
     329// Move the contents from one node to the other 
     330function MoveContents( oSource, oDest ) 
     331{ 
     332        while ( oSource.firstChild ) 
     333        { 
     334                var oNode = oSource.removeChild( oSource.firstChild ) ; 
     335                oDest.appendChild( oNode ) ; 
     336        } 
     337} 
  • FCKeditor/trunk/editor/dialog/fck_button.html

    r1398 r1504  
    4848                GetE('txtValue').value  = oActiveEl.value ; 
    4949                GetE('txtType').value   = oActiveEl.type ; 
    50  
    51                 GetE('txtType').disabled = true ; 
    5250        } 
    5351        else 
     
    6361        oEditor.FCKUndo.SaveUndoStep() ; 
    6462         
    65         if ( !oActiveEl ) 
    66         { 
    67                 oActiveEl = oEditor.FCK.EditorDocument.createElement( 'INPUT' ) ; 
    68                 oActiveEl.type = GetE('txtType').value ; 
    69                 oActiveEl = oEditor.FCK.InsertElement( oActiveEl ) ; 
    70         } 
     63        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'INPUT', {name: GetE('txtName').value, type: GetE('txtType').value } ) ; 
    7164 
    72         oActiveEl.name = GetE('txtName').value ; 
    7365        SetAttribute( oActiveEl, 'value', GetE('txtValue').value ) ; 
    7466 
  • FCKeditor/trunk/editor/dialog/fck_checkbox.html

    r1398 r1504  
    6161        oEditor.FCKUndo.SaveUndoStep() ; 
    6262         
    63         if ( !oActiveEl ) 
    64         { 
    65                 oActiveEl = oEditor.FCK.EditorDocument.createElement( 'INPUT' ) ; 
    66                 oActiveEl.type = 'checkbox' ; 
    67                 oActiveEl = oEditor.FCK.InsertElement( oActiveEl ) ; 
    68         } 
    69  
    70         if ( GetE('txtName').value.length > 0 ) 
    71                 oActiveEl.name = GetE('txtName').value ; 
     63        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'INPUT', {name: GetE('txtName').value, type: 'checkbox' } ) ; 
    7264 
    7365        if ( oEditor.FCKBrowserInfo.IsIE ) 
  • FCKeditor/trunk/editor/dialog/fck_hiddenfield.html

    r1398 r1504  
    7171        oEditor.FCKUndo.SaveUndoStep() ; 
    7272         
    73         if ( !oActiveEl ) 
    74         { 
    75                 oActiveEl = FCK.EditorDocument.createElement( 'INPUT' ) ; 
    76                 oActiveEl.type = 'hidden' ; 
     73        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'INPUT', {name: GetE('txtName').value, type: 'hidden' } ) ; 
    7774 
    78                 oFakeImage = null ; 
    79         } 
    80  
    81         oActiveEl.name = GetE('txtName').value ; 
    8275        SetAttribute( oActiveEl, 'value', GetE('txtValue').value ) ; 
    8376 
     
    8679                oFakeImage      = oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__InputHidden', oActiveEl ) ; 
    8780                oFakeImage.setAttribute( '_fckinputhidden', 'true', 0 ) ; 
    88                 oFakeImage      = FCK.InsertElement( oFakeImage ) ; 
     81 
     82                oActiveEl.parentNode.insertBefore( oFakeImage, oActiveEl ) ; 
     83                oActiveEl.parentNode.removeChild( oActiveEl ) ; 
    8984        } 
    9085        else 
    9186                oEditor.FCKUndo.SaveUndoStep() ; 
    92  
    93         oEditor.FCKFlashProcessor.RefreshView( oFakeImage, oActiveEl ) ; 
    9487 
    9588        return true ; 
  • FCKeditor/trunk/editor/dialog/fck_radiobutton.html

    r1398 r1504  
    6161        oEditor.FCKUndo.SaveUndoStep() ; 
    6262         
    63         if ( !oActiveEl ) 
    64         { 
    65                 oActiveEl = oEditor.FCK.EditorDocument.createElement( 'INPUT' ) ; 
    66                 oActiveEl.type = 'radio' ; 
    67                 oActiveEl = oEditor.FCK.InsertElement( oActiveEl ) ; 
    68         } 
    69  
    70         if ( GetE('txtName').value.length > 0 ) 
    71                 oActiveEl.name = GetE('txtName').value ; 
     63        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'INPUT', {name: GetE('txtName').value, type: 'radio' } ) ; 
    7264 
    7365        if ( oEditor.FCKBrowserInfo.IsIE ) 
  • FCKeditor/trunk/editor/dialog/fck_select.html

    r1398 r1504  
    8383                sSize = '' ; 
    8484 
    85         if ( !oActiveEl ) 
    86         { 
    87                 oActiveEl = oEditor.FCK.InsertElement( 'select' ) ; 
    88         } 
     85        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'SELECT', {name: GetE('txtName').value} ) ; 
    8986 
    90         SetAttribute( oActiveEl, 'name' , GetE('txtName').value ) ; 
    9187        SetAttribute( oActiveEl, 'size' , sSize ) ; 
    9288        oActiveEl.multiple = ( sSize.length > 0 && GetE('chkMultiple').checked ) ; 
  • FCKeditor/trunk/editor/dialog/fck_textarea.html

    r1398 r1504  
    6060{ 
    6161        oEditor.FCKUndo.SaveUndoStep() ; 
    62          
    63         if ( !oActiveEl ) 
    64         { 
    65                 oActiveEl = oEditor.FCK.InsertElement( 'textarea' ) ; 
    66         } 
    6762 
    68         oActiveEl.name = GetE('txtName').value ; 
     63        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'TEXTAREA', {name: GetE('txtName').value} ) ; 
     64 
    6965        SetAttribute( oActiveEl, 'cols', GetE('txtCols').value ) ; 
    7066        SetAttribute( oActiveEl, 'rows', GetE('txtRows').value ) ; 
  • FCKeditor/trunk/editor/dialog/fck_textfield.html

    r1398 r1504  
    5050                GetE('txtMax').value    = GetAttribute( oActiveEl, 'maxLength' ) ; 
    5151                GetE('txtType').value   = oActiveEl.type ; 
    52  
    53                 GetE('txtType').disabled = true ; 
    5452        } 
    5553        else 
     
    7674        } 
    7775 
    78         if ( !oActiveEl ) 
    79         { 
    80                 oActiveEl = oEditor.FCK.EditorDocument.createElement( 'INPUT' ) ; 
    81                 oActiveEl.type = GetE('txtType').value ; 
    82                 oEditor.FCKUndo.SaveUndoStep() ; 
    83                 oActiveEl = oEditor.FCK.InsertElement( oActiveEl ) ; 
    84         } 
     76        oEditor.FCKUndo.SaveUndoStep() ; 
    8577 
    86         oActiveEl.name = GetE('txtName').value ; 
     78        oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'INPUT', {name: GetE('txtName').value, type: GetE('txtType').value } ) ; 
     79 
    8780        SetAttribute( oActiveEl, 'value'        , GetE('txtValue').value ) ; 
    8881        SetAttribute( oActiveEl, 'size'         , GetE('txtSize').value ) ; 
  • FCKeditor/trunk/_whatsnew.html

    r1503 r1504  
    8585                <li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/1613">#1613</a>] The editor was surrounded 
    8686                        by a &lt;div&gt; element that wasn't really needed.</li> 
     87                <li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/676">#676</a>] If a form control was moved 
     88                        in IE after creating it, then it did lose its name.</li> 
     89                <li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/738">#738</a>] It wasn't possible to change 
     90                        the type of an existing button.</li> 
    8791        </ul> 
    8892        <p>