Elao/PhpEnums

Readable enum values should be escaped when generating javascript eums

Closed this issue · 2 comments

Hi there,

If a readable enum value contains characters that should be escaped in javascript strings, they need to be escaped in the JsDumper

for example:

class Foo extends ReadableEnum {
  static NICE = 'nice',

  static get readables() {
    return {
      [Foo.NICE]: 'it\'s nice'
    };
  }
}

but at the moment it will be dumped as invalid javascript:

class Foo extends ReadableEnum {
  static NICE = 'nice',

  static get readables() {
    return {
      [Foo.NICE]: 'it's nice'
    };
  }
}

How would you like to fix this?
we can use

  1. htmlspecialchars?
  2. something more complex like twig's js escaper?
  3. something else?

Thanks!

Hi! Thank you for nice and clear bug report. 🙏🏻

I guess using json_encode might be enough instead of

[{$shortName}.{$constant}]: '{$readable}',

with hardcoded single quotes.

thanks, i'll proceed on that basis