02 February 2016

Translating a named HTML entity to a Unicode value using Javascript

Translating a named HTML entity to a Unicode value


Google Docs


We use Google for Work and Google Docs at Stand Sure almost exclusively.

One of our biggest headaches is inserting special characters.

For reasons unknown, the Google Docs "Special Characters" tool under "Insert" is sometimes flaky and results in a message that the page needs reloaded (this started happening to us when we switched to the "new" version of Google Docs).

Chrome OS


We use Chrome OS devices.


Unicode is really easy on Chrome OS


Ctrl+Shift+U followed by the hexadecimal number.

This is a really awesome feature.


The Headache


In most cases, we know from HTML experience that © results in a copyright symbol, ©, that § results in a section symbol, §, etc.

But remembering that the Unicode for © is

&#a9;

so that we can type Ctrl+Shift+Ua9 to get the appropriate symbol in Google Docs is nigh impossible.


The Solution


Javascript to the rescue


"use strict";

var translateNamedEntity = function translateNamedEntity(text, undefined) {
  var div = document.createElement("div");

  div.innerHTML = text;
  text = div.childNodes[0].nodeValue;
  return text.charCodeAt(0).toString(16);
}

Usage

Paste the code into a REPL console.

Type something like the following:

translateNamedEntity("©")

In this example, you will get back the following:

"a9"

No comments: