0

I tried to implement the solution proposed in this thread to decode a string with html entities, e.g. "foo bar" to "foo bar".

Visually, it seems to work. But my quick Jest test fails:

Expected: "foobar"
Received: "foobar"

  3 | describe('encryption/decodeHtml', () => {
  4 |   it.each([['foo bar', 'foo bar'], ['foo­bar', 'foobar'], ['foo&bar', 'foo&bar']])('should decode html entities', (val, expected) => {
> 5 |     expect(decodeHtml(val)).toEqual(expected);
    |                             ^
  6 |   })
  7 | });
  8 |

A quick Object.is(decodeHtml(' '), ' ') also produces false.

Is there something to JS-Strings I'm not familiar with?

rsmidt
  • 69
  • 1
  • 7

1 Answers1

0

As pointed out by Andreas in the comments, I forgot about the byte representation of the string.

Look at this example:

toBytes('foo bar') -> Uint8Array(7) [102, 111, 111, 32, 98, 97, 114]
toBytes(decodeHtml('foo bar')) -> Uint8Array(8) [102, 111, 111, 194, 160, 98, 97, 114]

In hindsight it pretty obvious because the breaking space and the non breaking space are (of course) different characters.

rsmidt
  • 69
  • 1
  • 7