Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 2a8df53f

Von Sven Schöling vor etwa 11 Jahren hinzugefügt

  • ID 2a8df53fac09e95c9199021399e2343ff766535d
  • Vorgänger 0767bd03
  • Nachfolger 6f3f13dd

Javascript escape nach Ecmascript Spec.

Es gab einen Eckfall mit CR wo das kaputt gegangen ist, Spec kennt
noch als weitere Randfälle TAB, VT, ' und BS

Unterschiede anzeigen:

SL/Template/Plugin/JavaScript.pm
17 17
# public interface
18 18
#
19 19

  
20
# see ecmascript spec section 7.8.4
21
my @escape_chars = ('\\', '\'', '"');
22
my %control_chars = (
23
  "\n"   => 'n',
24
  "\t"   => 't',
25
  "\r"   => 'r',
26
  "\f"   => 'f',
27
  "\x08" => 'b',
28
  "\x0B" => 'v', # noone uses vertical tab anyway...
29
);
30
my $re = join '', map { qr/($_)/s } join '|', keys(%control_chars), map { "\Q$_\E" } @escape_chars;
31

  
20 32
sub escape {
21 33
  my $self = shift;
22 34
  my $text = shift;
23 35

  
24
  $text =~ s|\\|\\\\|g;
25
  $text =~ s|\"|\\\"|g;
26
  $text =~ s|\n|\\n|g;
27
  $text =~ s|\r|\\r|g;
36
  $text =~ s/$re/'\\' . ($control_chars{$1} || $1)/egs;
28 37

  
29 38
  return $text;
30 39
}

Auch abrufbar als: Unified diff