0

I'm working in a system where there is no document and no jQuery, but I do have to present html entities in an understandable way. So the trick of putting the string in an element and then taking the .text() won't work.

I need a pure JavaScript solution. The system isn't reachable from the outside, there is no user-input so security is not really an issue.

Thanks for any help, I'm out of ideas (not that I had to many to begin with)...


Perhaps I should clarify, what I am looking for is a function (or pointers to get me pointing in the right direction) which is able to translate a string with substrings that should translate to characters. So it should be able to translate "blah &#60; blahblah" into "blah < blahblah". There are no additional frameworks I can use other than pure javascript.

UPDATE:

I've got the html4 part working, not extremely difficult, but I have been busy with other things. Here's the fiddle:html4 entities to characters. You could have done the same with a dictionary with just the characters already in there, but I didn't feel like making such a dictionary. The function is fairly simple but I guess it could do with some refactoring, can't really be bothered at the moment...

AndrewRM
  • 33
  • 4
  • can you show what you have so far – atmd Apr 07 '15 at 08:38
  • [This answer](http://stackoverflow.com/a/7394814/791010) has the start of a solution, though it mentions you'll need a hashmap of non-numeric entities to deal with those. – James Thorpe Apr 07 '15 at 08:38
  • Yes @JamesThorpe I found that soludion, but most entities I have to deal with are entities like `ë`, so then I would have to construct a dictionary with all the possible entities, which is an option but I was hoping/looking for an more elegant solution. – AndrewRM Apr 08 '15 at 10:26
  • It would seem that [node html entities github](https://github.com/mdevils/node-html-entities) has files ([html5-entities](https://github.com/mdevils/node-html-entities/blob/master/lib/html5-entities.js)) which make the creation of a suitable dictionary much easier. It will be a beast of a dictionary if I want to include all the html5 entities though. But I think I will be able to get a working function... – AndrewRM Apr 08 '15 at 12:00

2 Answers2

1

This function exists in PHP (htmlspecialchars_decode). As such, you'll find a javascript port from PHPJS. This is based on a very established codebase, and should be better than rolling something on your own.


Edit / Add: Flub on my part. I didn't read the entities part properly. You want the equiv of html_entity_decode:

http://phpjs.org/functions/html_entity_decode/

John Green
  • 13,241
  • 3
  • 29
  • 51
0

Assuming you are using nodejs, cheerio is exactly what you need. I have used it myself a couple of times with great success for off-browser testing of HTML structures returned from servers.

https://github.com/cheeriojs/cheerio

The most awesome part is that it uses jQuery API.

Octav Zlatior
  • 451
  • 4
  • 14
  • I see... Then your best chance is probably to have a look at this module and just use their approach. There are two arrays in lib/html-entities.js which are exactly what you need, you can build a dictionary from there and use the replace() method on strings. – Octav Zlatior Apr 08 '15 at 10:36