| 1 | # -*- perl -*- | ||||||||||
| 2 | |||||||||||
| 3 | # | ||||||||||
| 4 | # $Id: JS.pm,v 1.8 2005/02/03 00:06:29 eserte Exp $ | ||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||
| 6 | # | ||||||||||
| 7 | # Copyright (C) 2002,2004 Slaven Rezic. All rights reserved. | ||||||||||
| 8 | # This package is free software; you can redistribute it and/or | ||||||||||
| 9 | # modify it under the same terms as Perl itself. | ||||||||||
| 10 | # | ||||||||||
| 11 | # Mail: slaven@rezic.de | ||||||||||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||||||||||
| 13 | # | ||||||||||
| 14 | |||||||||||
| 15 | package WE_Frontend::Plugin::JS; | ||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/); | |||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use base qw(Template::Plugin); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 24 | 0 | my($class, $context, @args) = @_; | |||||||||
| 25 | 0 | WE_Frontend::Plugin::JS::Quote->new($context, @args); | |||||||||
| 26 | 0 | WE_Frontend::Plugin::JS::QuoteUnicode->new($context, @args); | |||||||||
| 27 | 0 | WE_Frontend::Plugin::JS::Escape->new($context, @args); | |||||||||
| 28 | 0 | bless {}, $class; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | ###################################################################### | ||||||||||
| 32 | |||||||||||
| 33 | package WE_Frontend::Plugin::JS::Quote; | ||||||||||
| 34 | 1 | ▶ | use Template::Plugin::Filter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use base qw( Template::Plugin::Filter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | use vars qw(%repl $repl_keys $rx); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | %repl = ( | |||||||||
| 39 | "\\" => "\\\\", | ||||||||||
| 40 | "\"" => "\\\"", | ||||||||||
| 41 | "\'" => "\\\'", | ||||||||||
| 42 | "\n" => "\\n", | ||||||||||
| 43 | "\r" => "\\r", | ||||||||||
| 44 | "\b" => "\\b", | ||||||||||
| 45 | "\f" => "\\f", | ||||||||||
| 46 | "\t" => "\\t", | ||||||||||
| 47 | ); | ||||||||||
| 48 | 1 | for(0 .. 31) { | |||||||||
| 49 | 32 | ▶ | if (!exists $repl{chr($_)}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 27 | $repl{chr($_)} = "\\" . sprintf("%03o", $_); | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | 1 | $repl_keys = "(" . join("|", map { quotemeta } keys %repl) . ")"; | |||||||||
| 54 | 1 | $rx = qr/$repl_keys/; | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | 0 | my $self = shift; | |||||||||
| 58 | 0 | $self->install_filter("js_q"); | |||||||||
| 59 | 0 | $self; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub filter { | ||||||||
|
Subroutine
filter: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | my($self, $text) = @_; | |||||||||
| 64 | 0 | $text =~ s/$rx/$repl{$1}/g; | |||||||||
| 65 | 0 | $text; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | ###################################################################### | ||||||||||
| 69 | |||||||||||
| 70 | package WE_Frontend::Plugin::JS::QuoteUnicode; | ||||||||||
| 71 | 1 | ▶ | use Template::Plugin::Filter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | ▶ | use base qw( Template::Plugin::Filter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 1 | ▶ | use vars qw(%repl $repl_keys $rx); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 1 | %repl = ( | |||||||||
| 76 | "\\" => "\\\\", | ||||||||||
| 77 | "\"" => "\\\"", | ||||||||||
| 78 | "\'" => "\\\'", | ||||||||||
| 79 | "\n" => "\\n", | ||||||||||
| 80 | "\r" => "\\r", | ||||||||||
| 81 | "\b" => "\\b", | ||||||||||
| 82 | "\f" => "\\f", | ||||||||||
| 83 | "\t" => "\\t", | ||||||||||
| 84 | ); | ||||||||||
| 85 | 1 | for(0 .. 31) { | |||||||||
| 86 | 32 | ▶ | if (!exists $repl{chr($_)}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 27 | $repl{chr($_)} = "\\" . sprintf("%03o", $_); | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | 1 | $repl_keys = "(" . join("|", (map { quotemeta } keys %repl), "[\x7f-\x{fffd}]") . ")"; | |||||||||
| 91 | 1 | $rx = qr/$repl_keys/; | |||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
| |||||||||||
| 94 | 0 | my $self = shift; | |||||||||
| 95 | 0 | $self->install_filter("js_uni"); | |||||||||
| 96 | 0 | $self; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | sub filter { | ||||||||
|
Subroutine
filter: not called CC=2 SCAR=17.9
| |||||||||||
| 100 | 0 | my($self, $text) = @_; | |||||||||
| 101 | 0 | ▶ | $text =~ s/$rx/exists $repl{$1} ? $repl{$1} : sprintf "\\u%04x", ord($1)/ge; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | $text; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | ###################################################################### | ||||||||||
| 106 | |||||||||||
| 107 | package WE_Frontend::Plugin::JS::Escape; | ||||||||||
| 108 | 1 | ▶ | use Template::Plugin::Filter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 1 | ▶ | use base qw( Template::Plugin::Filter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 110 | #use URI::Escape qw(uri_escape); | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | 0 | $self->install_filter("js_escape"); | |||||||||
| 115 | 0 | $self; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub filter { | ||||||||
|
Subroutine
filter: not called CC=2 SCAR=17.9
| |||||||||||
| 119 | 0 | my($self, $text) = @_; | |||||||||
| 120 | #uri_escape($text, "^A-Za-z0-9"); | ||||||||||
| 121 | 0 | $text =~ s{ ([^A-Za-z0-9]) } | |||||||||
| 122 | 0 | ▶ | { ord($1) > 255 ? sprintf "%%u%04x", ord $1 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | : sprintf "%%%02x", ord $1 | ||||||||||
| 124 | }gex; | ||||||||||
| 125 | 0 | $text; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 1 | 1; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||