Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode HTML entities using jQuery?

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

like image 630
EddyR Avatar asked Jul 18 '09 11:07

EddyR


People also ask

How do you decode HTML tags?

HTML character decoding is the opposite process of encoding. The encoded characters are converted back to their original form in the decoding process. It decodes a string that contains HTML numeric character references and returns the decoded string. You can also choose to convert HTML code into JavaScript string.

What is parseHTML in JavaScript?

parseHTML uses native methods to convert the string to a set of DOM nodes, which can then be inserted into the document. These methods do render all trailing or leading text (even if that's just whitespace).

How do you unescape in HTML?

One way to unescape HTML entities is to put our escaped text in a text area. This will unescape the text, so we can return the unescaped text afterward by getting the text from the text area. We have an htmlDecode function that takes an input string as a parameter.

Which method is used to decode the currently encoded HTML code?

The input string is encoded using the HtmlEncode method. The encoded string obtained is then decoded using the HtmlDecode method.


2 Answers

Security note: using this answer (preserved in its original form below) may introduce an XSS vulnerability into your application. You should not use this answer. Read lucascaro's answer for an explanation of the vulnerabilities in this answer, and use the approach from either that answer or Mark Amery's answer instead.

Actually, try

var encodedStr = "This is fun &amp; stuff"; var decoded = $("<div/>").html(encodedStr).text(); console.log(decoded);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div/>
like image 152
tom Avatar answered Sep 22 '22 23:09

tom


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.

like image 20
lucascaro Avatar answered Sep 22 '22 23:09

lucascaro