Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How to encode/decode HTML-Entities? [2.0]

  1. #1
    Ext User
    Join Date
    Jul 2007
    Posts
    22
    Vote Rating
    0
      0  

    Question How to encode/decode HTML-Entities? [2.0]

    Hi,

    selecting nodes from a tree, I noticed by logging their text's via console.log(), special characters like '&' are encoded to HTML-Entities (in that case '& # 38;').

    To convert that code back to a character I tried Ext.util.Format.htmlDecode() which did exactly nothing for me.

    Example: node.text is "Me & #38 ; Myself" and should be converted to "Me & Myself". I tried

    Code:
    console.log(Ext.util.Format.htmlDecode(node.text));
    No effect at all

  2. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,175
    Vote Rating
    995
      0  

    Default

    htmlEncode/decode doesn't handle that case.

    Only the basic ampersand, less than, greater than and quotes.

  3. #3
    Ext User
    Join Date
    Jul 2007
    Posts
    22
    Vote Rating
    0
      0  

    Default

    According to the docs, the '&' Character should be handled:

    htmlDecode( String value ) : String
    Convert certain characters (&, <, >, and ') from their HTML character equivalents.

  4. #4
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,175
    Vote Rating
    995
      0  

    Default

    Yes, it means literally just ampersand, not &#code;.

    Check out the source, you'll see what I mean.

  5. #5
    Ext User
    Join Date
    Jul 2007
    Posts
    22
    Vote Rating
    0
      0  

    Default

    yep got it. is there any other way to do this quickly ?

  6. #6
    Ext JS Premium Member dj's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    573
    Vote Rating
    3
      0  

    Default

    you let the browser do the hard work:
    Code:
    var temp_div = document.createElement('div');
    temp_div.innerHTML = '& #38;';
    console.log(temp_div.firstChild.nodeValue);
    The other way round would be
    Code:
    var temp_div = document.createElement('div');
    temp_div.innerHTML = "&nbsp;";
    temp_div.firstChild.nodeValue = '&';
    console.log(temp_div.innerHTML);
    temp_div can of course be reused.

    BTW: Is there a reason, why Ext.util.Format.htmlEncode/Decode doesn't use this?

  7. #7
    Ext User
    Join Date
    Jul 2007
    Posts
    22
    Vote Rating
    0
      0  

    Default

    That code fragment was okay if i just wanted to view the character but i really need it as string as i need to set it as value of a textfield (to be precise as text of an Ext.form.TriggerField)

  8. #8
    Ext JS Premium Member dj's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    573
    Vote Rating
    3
      0  

    Default

    ?

    so you do
    Code:
    myTextField.setValue(temp_div.firstChild.nodeValue);
    where is the problem?

  9. #9
    Ext JS Premium Member dj's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    573
    Vote Rating
    3
      0  

    Default

    Quote Originally Posted by dj View Post
    BTW: Is there a reason, why Ext.util.Format.htmlEncode/Decode doesn't use this?
    To answer my own question: yes there is.

    You can only use this innerHTML/firstChild.nodeValue method if you first ensure, that there are no tags (<>) in the string. So you have to encode these special cases before you can decode the whole string. That's a overhead one normally don't need.

  10. #10
    Ext User
    Join Date
    Jul 2007
    Posts
    22
    Vote Rating
    0
      0  

    Default

    Thanks guys,

    I built following method from the info you gave me:

    Code:
    repairString : function(str) {
       var temp_div = document.createElement('div');
       temp_div.innerHTML = str;
       return temp_div.firstChild.nodeValue;
    }
    That works fine for me, even with tags. I tried the string

    Code:
    <b><"Me & Myself"></b>
    and it was converted correctly

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •