2006-01-04

Unescaping HTML in javascript without function invocation

Here is a (rarely) useful tip on how you can effortlessly unescape HTML in a document, without even calling a function. It is probably mostly useful when debugging some live page where everything below some node has ended up one level of HTML quoting too much, and you want to see what it should actually look like. Or, in other words: do not use this in production code; first, because you should always go for fixing the problem at its source, second, because real string dabbling is still the more portable route, across new and aging browsers alike.

Anyway, if you have a node whose contents need unescaping a level, here is what you do: target.innerHTML = target.textContent; Yes, that would be all. Run it again to unescape another level of HTML escapes. To do this part point-and-click:y with the Firefox Document Inspector, invoke that first by Ctrl+Shift+i (or Command+Shift+i on the Mac, I presume), click the "select element by click" arrow, click your node in the document, switch to the "Javascript Object" view in the right panel, right-click Subject, picking "Evaluate Javascript...", where you type or paste the magic statement from above. Done!

If you want a test page to try it, have a look at the last excerpt on this page on Yukuan's Blogmarks blog, generated by the Blogmarks sync feature (which seems to either escape some entries once too much, or, perhaps more likely, not unescape them once too seldom. (I would venture guessing that this is the effect of not properly taking notice of attributes in the imported dataset stating whether it is quoted or not, but I may be a bit off the mark as I have been reading up on Atom and RSS and lost sense of Blogmarks context somewhere in the middle of it. It's also five AM here; when did that happen?)

When done marvelling at the beauty of this nifty hack, do read the linked article on the Google Reader API; it's good for some interesting future prospects. When will we see a mashup of feeds and translation services, for instance? I find I would want to pick up Yukuan's feed, tell FeedBurner, or better still my feed reader, to translate it to English for me (adding some standard header bringing reader attention to the fact that the poor translation was the result of machine translation, rather than insanity on part of the author) and otherwise show it as any other posts in my reading list.

I tried in vain to get Google Language Tools to translate Atom and RSS feeds alike; it only seems to chew text/html at present. I suppose we will see good things happen on this front too, eventually.
blog comments powered by Disqus