How to decode HTML entities using jQuery?

后端 未结 19 2208
忘了有多久
忘了有多久 2020-11-21 23:21

How do I use jQuery to decode HTML entities in a string?

相关标签:
19条回答
  • 2020-11-21 23:56

    You have to make custom function for html entities:

    function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g,'&gt;').replace(/"/g, '&quot;');
    }
    
    0 讨论(0)
  • 2020-11-21 23:56

    I think that is the exact opposite of the solution chosen.

    var decoded = $("<div/>").text(encodedStr).html();
    
    0 讨论(0)
  • 2020-11-21 23:57

    Without any jQuery:

    function decodeEntities(encodedString) {
      var textArea = document.createElement('textarea');
      textArea.innerHTML = encodedString;
      return textArea.value;
    }
    
    console.log(decodeEntities('1 &amp; 2')); // '1 & 2'

    This works similarly to the accepted answer, but is safe to use with untrusted user input.


    Security issues in similar approaches

    As noted by Mike Samuel, doing this with a <div> instead of a <textarea> with untrusted user input is an XSS vulnerability, even if the <div> is never added to the DOM:

    function decodeEntities(encodedString) {
      var div = document.createElement('div');
      div.innerHTML = encodedString;
      return div.textContent;
    }
    
    // Shows an alert
    decodeEntities('<img src="nonexistent_image" onerror="alert(1337)">')

    However, this attack is not possible against a <textarea> because there are no HTML elements that are permitted content of a <textarea>. Consequently, any HTML tags still present in the 'encoded' string will be automatically entity-encoded by the browser.

    function decodeEntities(encodedString) {
        var textArea = document.createElement('textarea');
        textArea.innerHTML = encodedString;
        return textArea.value;
    }
    
    // Safe, and returns the correct answer
    console.log(decodeEntities('<img src="nonexistent_image" onerror="alert(1337)">'))

    Warning: Doing this using jQuery's .html() and .val() methods instead of using .innerHTML and .value is also insecure* for some versions of jQuery, even when using a textarea. This is because older versions of jQuery would deliberately and explicitly evaluate scripts contained in the string passed to .html(). Hence code like this shows an alert in jQuery 1.8:

    //<!-- CDATA
    // Shows alert
    $("<textarea>")
    .html("<script>alert(1337);</script>")
    .text();
    
    //-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

    * Thanks to Eru Penkman for catching this vulnerability.

    0 讨论(0)
  • 2020-11-22 00:01

    The easiest way is to set a class selector to your elements an then use following code:

    $(function(){
        $('.classSelector').each(function(a, b){
            $(b).html($(b).text());
        });
    });
    

    Nothing any more needed!

    I had this problem and found this clear solution and it works fine.

    0 讨论(0)
  • 2020-11-22 00:02

    I think you're confusing the text and HTML methods. Look at this example, if you use an element's inner HTML as text, you'll get decoded HTML tags (second button). But if you use them as HTML, you'll get the HTML formatted view (first button).

    <div id="myDiv">
        here is a <b>HTML</b> content.
    </div>
    <br />
    <input value="Write as HTML" type="button" onclick="javascript:$('#resultDiv').html($('#myDiv').html());" />
    &nbsp;&nbsp;
    <input value="Write as Text" type="button" onclick="javascript:$('#resultDiv').text($('#myDiv').html());" />
    <br /><br />
    <div id="resultDiv">
        Results here !
    </div>
    

    First button writes : here is a HTML content.

    Second button writes : here is a <B>HTML</B> content.

    By the way, you can see a plug-in that I found in jQuery plugin - HTML decode and encode that encodes and decodes HTML strings.

    0 讨论(0)
  • 2020-11-22 00:02

    encode:

    $("<textarea/>").html('<a>').html();      // return '&lt;a&gt'
    

    decode:

    $("<textarea/>").html('&lt;a&gt').val()   // return '<a>'
    
    0 讨论(0)
提交回复
热议问题