Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2282

Show
Ignore:
Timestamp:
09/21/05 08:11:51 (3 years ago)
Author:
madrobby
Message:

script.aculo.us: Fixed a bug with Safari and the InPlaceEditor with form submission. Add support for interpreting simple <br>s into linebreaks. [Jon Tirsen]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/scriptaculous/CHANGELOG

    r2280 r2282  
    11*SVN* 
     2 
     3* Fixed a bug with Safari and the InPlaceEditor with form submission. Add support for interpreting simple <br>s into linebreaks. [Jon Tirsen] 
    24 
    35* New Control.Slider() for horizontal and vertical sliders [Marty Haught] 
  • spinoffs/scriptaculous/src/controls.js

    r2216 r2282  
    459459        return Form.serialize(form); 
    460460      }, 
     461      handleLineBreaks: true, 
    461462      loadingText: 'Loading...', 
    462463      savingClassName: 'inplaceeditor-saving', 
     
    511512    this.element.parentNode.insertBefore(this.form, this.element); 
    512513    Field.focus(this.editField); 
     514    // stop the event to avoid a page refresh in Safari 
     515    if (arguments.length > 1) { 
     516      Event.stop(arguments[0]); 
     517    } 
    513518  }, 
    514519  getForm: function() { 
     
    537542    return form; 
    538543  }, 
     544  hasHTMLLineBreaks: function(string) { 
     545    if (!this.options.handleLineBreaks) return false; 
     546    return string.match(/<br/i) || string.match(/<p>/i); 
     547  }, 
     548  convertHTMLLineBreaks: function(string) { 
     549    return string.replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n").replace(/<\/p>/gi, "\n").replace(/<p>/gi, ""); 
     550  }, 
    539551  createEditField: function(form) { 
    540     if (this.options.rows == 1) { 
     552    if (this.options.rows == 1 && !this.hasHTMLLineBreaks(this.getText())) { 
    541553      this.options.textarea = false; 
    542554      var textField = document.createElement("input"); 
     
    554566      var textArea = document.createElement("textarea"); 
    555567      textArea.name = "value"; 
    556       textArea.value = this.getText(); 
     568      textArea.value = this.convertHTMLLineBreaks(this.getText()); 
    557569      textArea.rows = this.options.rows; 
    558570      textArea.cols = this.options.cols || 40; 
     
    610622    ); 
    611623    this.onLoading(); 
     624    // stop the event to avoid a page refresh in Safari 
     625    if (arguments.length > 1) { 
     626      Event.stop(arguments[0]); 
     627    } 
    612628    return false; 
    613629  }, 
  • spinoffs/scriptaculous/test/unit/ajax_inplaceeditor_test.html

    r2063 r2282  
    2222<a id="tobeeditedEditControl" href="#">edit</a> 
    2323 
     24<div id="tobeeditedMultiLine">First line<br/>Second line<br/>Third line</div> 
    2425 
    2526<!-- Tests follow --> 
     
    3233      inPlaceEditor = new Ajax.InPlaceEditor($('tobeedited'), '_ajax_inplaceeditor_result.html', { 
    3334        externalControl: $('tobeeditedEditControl'), 
     35        ajaxOptions: {method: 'get'} //override so we can use a static for the result 
     36      }); 
     37      inPlaceEditorMultiLine = new Ajax.InPlaceEditor($('tobeeditedMultiLine'), '_ajax_inplaceeditor_result.html', { 
    3438        ajaxOptions: {method: 'get'} //override so we can use a static for the result 
    3539      }); 
     
    167171      Event.simulateMouse(document.forms[0].childNodes[2],'click'); 
    168172      assertVisible($('tobeeditedEditControl')); 
     173    }}, 
     174     
     175    testHasLineBreaksDetectsHTMLLineBreaks: function() { with(this) { 
     176      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br/>Line 2")); 
     177      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br>Line 2")); 
     178      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<p>Line 1</p>Line 2")); 
     179      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR/>Line 2")); 
     180      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR>Line 2")); 
     181      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1</P>Line 2")); 
     182      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1<P>Line 2")); 
     183      assert(!inPlaceEditorMultiLine.hasHTMLLineBreaks("One line")); 
     184    }}, 
     185     
     186    testConvertsHTMLLineBreaksIntoNewLines: function() { with(this) { 
     187      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br/>Line 2")); 
     188      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br>Line 2")); 
     189      // <p> not supported (too hard) 
     190      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<p>Line 1</p>Line 2")); 
     191      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR/>Line 2")); 
     192      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR>Line 2")); 
     193      // <p> not supported (too hard) 
     194      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2")); 
     195      // unclosed <P>s not supported yet 
     196      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1<P>Line 2")); 
     197      // <p> not supported (too hard) 
     198      //assertEqual("Line 1\nLine 2\nLine 3\nLine 4", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2<br>Line 3<p>Line 4</P>")); 
     199      assertEqual("One line", inPlaceEditorMultiLine.convertHTMLLineBreaks("One line")); 
     200    }}, 
     201     
     202    testConvertsParagraphsAndBRsIntoLineBreaks: function() { with(this) { 
     203      inPlaceEditorMultiLine.enterEditMode(); 
     204      assertEqual("TEXTAREA", document.forms[0].firstChild.tagName); 
     205      assertEqual("First line\nSecond line\nThird line", document.forms[0].firstChild.value) 
     206      // doesn't automatically determine size yet 
     207      //assertEqual(3, document.forms[0].firstChild.rows); 
    169208    }} 
    170209