Parse me a numeric html entity

github logo ・1 min read

I saw the following question:

I am using wordpress rest api and am getting encoded title strings from the server. I want to decode the string before I use it to replace the document.title.

Wordpress api

{
 "id": 698,
 "title": {
  "rendered": "Ludovico Einaudi – “Divenire”"
 },
}

actions.js

export default {
  updateDocTitle ({ state,
…

It's been closed as a dupe, but that didn't discourage me from wanting to figure it out for myself.

So, the challenge is: for any given string input, replace any numerically represented html entities with the correct character.

Remember, the largest codepoint is 0x10ffff.

Some test cases:

'ö_ö' // expected 'ΓΆ_ΓΆ'
'Hello &&&#x;'  // expected 'Hello &&&#x;'
'&#123 {'  // expected '&#123 Δ£'
'�'    // expected '�'

I'll post my attempt below!

twitter logo DISCUSS (2)
markdown guide
 

This is how I solved it:

There must be a less verbose way :D

 

Created a little Grease Monkey script to use it:

// ==UserScript==
// @name     HTML Entity Replacer
// @version  1
// @include  http*
// @grant    none
// @require  https://gist.githubusercontent.com/Mellen/ce5d247587dcb7b49e5145a6f49328be/raw/16871d379ea7afe4063ac3646851ae35cbc010df/replaceNumericEntities.js
// @run-at   document-end
// ==/UserScript==


inputs = Array.from(document.querySelectorAll('input[type=text], textarea'));
inputs.forEach(input => 
               {
                 input.addEventListener('keyup', e =>      
                                                 {
                                                   e.target.value = replaceNumericEntities(e.target.value);
                                                 });
               });
Classic DEV Post from Aug 14

What Does Your IDE/Code Editor Look Like?

Matt Ellen profile image
Ultra-fullstack software developer. Python, JavaScript, C#, C