1

I have a javascript node in a variable, and if I log that variable to the console, I get this:

"​asekuhfas eo"

Just some random string in a javascript node. I want to get that literally to be a string. But the problem is, when I use textContent on it, I get this:

​asekuhfas eo

The special character is converted. I need to get the string to appear literally like this:

​asekuhfas eo

This way, I can deal with the special character (recognize when it exists in the string).

How can I get that node object to be a string LITERALLY as it appears?

Joel Worsham
  • 1,140
  • 1
  • 7
  • 19

2 Answers2

3

As VisionN has pointed out, it is not possible to reverse the UTF-8 encoding. However by using charCodeAt() you can probably still achieve your goal.

Say you have your textContent. By iterating through each character, retrieving its charCode and prepending "&#" as well as appending ";" you can get your desired result. The downside of this method obviously being that you will have each and every character in this annotation, even those do not require it. By introducing some kind of threshold you can restrict this to only the exotic characters.

A very naive approach would be something like this:

var a = div.textContent;
var result = "";
var treshold = 1000;
for (var i = 0; i < a.length; i++) {
  if (a.charCodeAt(i) > 1000)
    result += "&#" + a.charCodeAt(i) + ";";
 else 
    result += a[i];
}
Eugen Timm
  • 744
  • 6
  • 13
1

textContent returns everything correctly, as &#8203; is the Unicode Character 'ZERO WIDTH SPACE' (U+200B), which is:

commonly abbreviated ZWSP

this character is intended for invisible word separation and for line break control; it has no width, but its presence between two characters does not prevent increased letter spacing in justification

It can be easily proven with:

var div = document.createElement('div');
div.innerHTML = '&#8203;xXx';

console.log( div.textContent );                   // "​xXx"
console.log( div.textContent.length );            // 4
console.log( div.textContent[0].charCodeAt(0) );  // 8203

As Eugen Timm mentioned in his answer it is a bit tricky to convert UTF characters back to HTML entities, and his solution is completely valid for non standard characters with char code higher than 1000. As an alternative I may propose a shorter RegExp solution which will give the same result:

var result = div.textContent.replace(/./g, function(x) {
    var code = x.charCodeAt(0);
    return code > 1e3 ? '&#' + code + ';' : x;
});

console.log( result );  // "&#8203;xXx"

For a better solution you may have a look at this answer which can handle all HTML special characters.

Community
  • 1
  • 1
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • not sure this answers the OP's question regarding: `How can I get that node object to be a string LITERALLY as it appears?` OP wants to fetch the string as it appears in the html. (namely, the `​` bit – ddavison Nov 21 '14 at 15:19
  • Right, I get that. So I'm wondering if I can get that DOM node as it literally appears in some other way? Some sort of numeric or raw unicode encoding method that I'm not aware of perhaps? – Joel Worsham Nov 21 '14 at 15:19