#598 ✓help_request
viniholden (at hotmail)

HTML Entity Handling with setValue

Reported by viniholden (at hotmail) | March 16th, 2009 @ 06:17 PM

I have a textarea on a page which contains notes as tekt. Html encoded entities are encoded correctly for display in order to pass XHTML 1.0 strict validation (which is a company requirement).

E.G.


<textarea name="listing_summary_header-old_notes" id="listing_summary_header-old_notes" readonly="readonly" cols="50" rows="5" style="width:320px; background: #EAF5F8;  height: 80px; font-size: 0.7em;">
16/3/2009 - Vini Holden - 570 enqs - test
16/3/2009 - Vini Holden - 570 enqs - &amp;&amp;&amp;
</textarea>

Every 30 seconds a PeriodicalExecuter fires an Ajax call which checks for new notes and updates the textarea using setValue if new notes are found.


var feature_code = 'listing_summary_header'
				oResponse['listing_notes']['data'].each(function (listingNote) {

if (!listingNote) {
return;
}

var d = new Date(listingNote['timestamp'] * 1000);
sListingNotes += d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear()+" - "+listingNote['user_full_name']+" - "+listingNote['text']+"\n";

});
					
$(feature_code+'-old_notes').setValue(sListingNotes);
$(feature_code+'-new_note').setValue('');

Unfortunately after the textarea is updated and html encoded characters display as the html code. When I use developer toolbar to view the generated source it appears to be correct (no double encoding).

I routinely test in the following browsers:

Firefox 3.0.7 IE 7.0.6001.18000 Chrome 1.0.154.48 Firefox 2.0.0.20 Opera 9.52 Safari 3.1.2

The issue appears in all of the above in the same way. The encoding is done on the array once before it is sent to the javascript via ajax via JSON string.

Any thoughts?

Comments and changes to this ticket

  • viniholden (at hotmail)

    viniholden (at hotmail) March 16th, 2009 @ 06:20 PM

    tekt = text (still getting used to Dvorak :P).

  • Tobie Langel

    Tobie Langel March 16th, 2009 @ 11:53 PM

    • State changed from “new” to “help_request”

    Sorry you're having trouble.

    Lighthouse is reserved for development purposes. Please direct assistance requests to the mailing list or IRC channel.

    Thanks for your understanding.

  • viniholden (at hotmail)

    viniholden (at hotmail) March 17th, 2009 @ 01:50 AM

    This wasn't a help request it was a bug report.

    Perhaps I was mistaken and should take your advice?

  • Tobie Langel

    Tobie Langel March 17th, 2009 @ 02:08 AM

    Perhaps I was mistaken too. Currently, it's very difficult from your above report to understand what your issue is. If you think this is a bug, please have a more precise report which includes a reduced failing test case, the expected outcome and the actual outcome. If this behaviour is observed in all browsers, there's a good chance that it is not a bug, but a spec.

  • viniholden (at hotmail)

    viniholden (at hotmail) March 17th, 2009 @ 03:11 AM

    Try the following code.

    You see that when you load the page the ampersand displays correctly as &, but after clicking the button to update the textarea with the same string it displays as &.

    php file

    
    
    <?php
     // Test
     $sEncodedString = 'An ampersand: &amp;';
    ?>
    <script src="js/prototype/prototype-1.6.0.2.js" type="text/javascript"></script>
    <script src="test.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    var sEncodedString = '<?php echo $sEncodedString; ?>';
    </script>
    
    <textarea id="testTextarea">
    <?php echo $sEncodedString; ?>
    </textarea>
    
    <br>
    <button onclick="updateTextarea();">
    A Button
    </button>
    
    

    js file

    
    
    function updateTextarea() {
    	$('testTextarea').setValue(sEncodedString);
    }
    
    

    Having sought the avenues of help you suggested the solution/workaround appears to be to decode the string before using it to update the textarea again, because when you use javascript to update it, it takes the literal string you pass and thus displays & instead of &.

    e.g. sEncodedString = sEncodedString.unescapeHTML();

    I am not entirely satisfied with this. Using this method I not only have to encode html entities in php before initial display, I then need to decode them again in javascript for any subsequent display updates.

    If this is the correct expected behaviour then please close the ticket, however if you believe that an identical XHTML compliant string should display in the same way after an update as it does on load then please consider it further.

  • viniholden (at hotmail)

    viniholden (at hotmail) March 17th, 2009 @ 03:12 AM

    Oh dear, I messed up my & amp; display for this post! I hope you can still follow it.

  • Tobie Langel

    Tobie Langel March 17th, 2009 @ 12:18 PM

    A textarea displays exactly what's added to it. There's the expected behaviour. You'll indeed have to use unescapeHTML in your particular case.

  • viniholden (at hotmail)

    viniholden (at hotmail) March 17th, 2009 @ 12:21 PM

    Okay, please close this ticket.

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 01:54 AM

    • Tag changed from ajax, display, htmlentities, incorrect, javascript, setvalue, textarea, update to section:ajax

    [not-tagged:"ajax" tagged:"section:ajax" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages