Diff for /loncom/localize/lonlocal.pm between versions 1.65 and 1.66

version 1.65, 2014/12/11 01:47:25 version 1.66, 2015/06/09 21:23:15
Line 171  use DateTime::Locale; Line 171  use DateTime::Locale;
 require Exporter;  require Exporter;
   
 our @ISA = qw (Exporter);  our @ISA = qw (Exporter);
 our @EXPORT = qw(mt mtn ns mt_user);  our @EXPORT = qw(mt mtn ns mt_user js_escape html_escape);
   
 my %mtcache=();  my %mtcache=();
   
Line 575  sub mt_escape { Line 575  sub mt_escape {
     $$str_ref =~s/([\[\]])/~$1/g;      $$str_ref =~s/([\[\]])/~$1/g;
 }  }
   
   =pod 
   
   =item * js_escape
   
   js_escape takes a string, string reference or hash reference,
   and escapes the values so that they can be used within a <script> element.
   It replaces all instances of \ by \\, ' by \', " by \" and \n by \\n.
   It is typically used with localized strings, which might contain quotes.
   
   =cut
   
   sub js_escape {
       my ($v) = @_;
       my $ref = ref($v);
       if ($ref eq 'SCALAR') {
           $$v =~ s/\\/\\\\/g;
           $$v =~ s/'/\\'/g;
           $$v =~ s/"/\\"/g;
           $$v =~ s/\n/\\n/g;
       } elsif ($ref eq 'HASH') {
           foreach my $key (keys %$v) {
               $v->{$key} =~ s/\\/\\\\/g;
               $v->{$key} =~ s/'/\\'/g;
               $v->{$key} =~ s/"/\\"/g;
               $v->{$key} =~ s/\n/\\n/g;
           }
       } else {
           $v =~ s/\\/\\\\/g;
           $v =~ s/'/\\'/g;
           $v =~ s/"/\\"/g;
           $v =~ s/\n/\\n/g;
           return $v;
       }
   }
   
   =pod 
   
   =item * html_escape
   
   js_escape takes a string, string reference or hash reference,
   and escapes the values so that they can be used as HTML.
   It encodes <, >, &, ' and ".
   
   =cut
   
   sub html_escape {
       my ($v) = @_;
       my $ref = ref($v);
       if ($ref eq 'SCALAR') {
           $$v =~ s/&/&amp;/g;
           $$v =~ s/</&lt;/g;
           $$v =~ s/>/&gt;/g;
           $$v =~ s/'/&apos;/g;
           $$v =~ s/"/&quot;/g;
       } elsif ($ref eq 'HASH') {
           foreach my $key (keys %$v) {
               $v->{$key} =~ s/&/&amp;/g;
               $v->{$key} =~ s/</&lt;/g;
               $v->{$key} =~ s/>/&gt;/g;
               $v->{$key} =~ s/'/&apos;/g;
               $v->{$key} =~ s/"/&quot;/g;
           }
       } else {
           $v =~ s/&/&amp;/g;
           $v =~ s/</&lt;/g;
           $v =~ s/>/&gt;/g;
           $v =~ s/'/&apos;/g;
           $v =~ s/"/&quot;/g;
           return $v;
       }
       # NOTE: we could also turn \n into <br> if needed
   }
   
 =pod  =pod
   
 =item * choose_language  =item * choose_language

Removed from v.1.65  
changed lines
  Added in v.1.66


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>