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...

4
  • can you show what you have so far Commented Apr 7, 2015 at 8:38
  • This answer has the start of a solution, though it mentions you'll need a hashmap of non-numeric entities to deal with those. Commented Apr 7, 2015 at 8:38
  • Yes @JamesThorpe I found that soludion, but most entities I have to deal with are entities like &euml;, 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. Commented Apr 8, 2015 at 10:26
  • It would seem that node html entities github has files (html5-entities) 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... Commented Apr 8, 2015 at 12:00

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.