Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
377 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package XML::XSS::ProcessingInstruction; | ||
|
||
use 5.10.0; | ||
|
||
use Moose; | ||
use MooseX::SemiAffordanceAccessor; | ||
|
||
with 'XML::XSS::Role::Renderer'; | ||
|
||
no warnings qw/ uninitialized /; | ||
|
||
sub apply { | ||
my ( $self, $node, $args ) = @_; | ||
$args ||= {}; | ||
|
||
return $node->toString; | ||
} | ||
|
||
1; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package XML::XSS::Role::Template; | ||
|
||
use Moose::Role; | ||
|
||
Moose::Exporter->setup_import_methods( | ||
as_is => ['xsst'], | ||
); | ||
|
||
sub xsst($) { | ||
my $template = shift; | ||
|
||
my $code = _parse($template); | ||
|
||
my $sub = eval <<"END_SUB"; | ||
sub { | ||
my ( \$r, \$node, \$args ) = \@_; | ||
local *STDOUT; | ||
my \$output; | ||
open STDOUT, '>', \\\$output or die; | ||
$code; | ||
return \$output; | ||
} | ||
END_SUB | ||
|
||
die $@ if $@; | ||
|
||
return $sub; | ||
} | ||
|
||
sub _parse { | ||
my $template = shift; | ||
|
||
my @tokens = split /(<-?%\S*|%-?>)/, $template; | ||
|
||
my @pieces; | ||
|
||
while( @tokens ) { | ||
if ( $tokens[0] =~ /^<-?%/ ) { | ||
} | ||
else { | ||
push @pieces, [ 1, shift @tokens ]; | ||
} | ||
} | ||
|
||
$template =~ s/\|/\\\|/g; | ||
|
||
return "print q|$template|;" | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
|
||
<% $x = 3; %> | ||
<%= $i %> | ||
<%~ foo/bar %> | ||
<%@ @text %> | ||
<%# blah blah %> | ||
<%! foo | ||
yadah yadah | ||
%> | ||
|
||
sub extract { | ||
my ($self,$stylesheet,@includestack) = @_; | ||
|
||
my $filename = $self->{stylesheet_dependencies}[0] || "stylesheet"; | ||
|
||
my $contents = $self->read_stylesheet( $stylesheet ); | ||
|
||
my @tokens = split /(<%[-=~#@]*|-?%>)/, $contents; | ||
|
||
no warnings qw/ uninitialized /; | ||
|
||
my $script; | ||
my $line = 1; | ||
TOKEN: | ||
while ( @tokens ) { | ||
my $token = shift @tokens; | ||
|
||
if ( -1 == index $token, '<%' ) { | ||
$line += $token =~ tr/\n//; | ||
$token =~ s/\s+$// if -1 < index $tokens[0], '<%' | ||
and -1 < index $tokens[0], '-'; | ||
$token =~ s/\|/\\\|/g; | ||
# check for include | ||
$token =~ s{<!--#include.+file=(['"])(.*?)\1.*?-->} | ||
{ '|);' | ||
. $self->include_file( $2, @includestack) | ||
. 'print(q|'}seg; | ||
$script .= 'print(q|'.$token.'|);' if length $token; | ||
|
||
next TOKEN; | ||
} | ||
|
||
$script .= "\n#line $line $filename\n"; | ||
|
||
my $opening_tag = $token; | ||
my $code; | ||
my $closing_tag; | ||
my $level = 1; | ||
while( @tokens ) { | ||
my $t = shift @tokens; | ||
$level++ if -1 < index $t, '<%'; | ||
$level-- if -1 < index $t, '%>'; | ||
if ( $level == 0 ) { | ||
$closing_tag = $t; | ||
last; | ||
} | ||
$code .= $t; | ||
} | ||
|
||
die "stylesheet <% %>s are unbalanced: $opening_tag$code\n" | ||
unless $closing_tag; | ||
|
||
$line += $code =~ tr/\n//; | ||
|
||
if ( -1 < index $opening_tag, '=' ) { | ||
$script .= 'print( '.$code.' );'; | ||
} | ||
elsif ( -1 < index $opening_tag, '~' ) { | ||
$code =~ s/^\s+//; | ||
$code =~ s/\s+$//; | ||
$script .= 'print $processor->apply_templates( qq<'. $code .'> );'; | ||
} | ||
elsif( -1 < index $opening_tag, '#' ) { | ||
# do nothing | ||
} | ||
elsif( -1 < index $opening_tag, '@' ) { | ||
$code =~ s/^\s+(\S+).*?\n//; # strip first line | ||
my $tag = $1 | ||
or die "tag name missing in <%\@ %> at line $line\n"; | ||
|
||
my $here_delimiter = 'END_TAG'; | ||
while ( $code =~ /$here_delimiter/ ) { | ||
$here_delimiter .= 'x'; | ||
} | ||
$script .= <<END_SNIPPET; | ||
\$template->set( $tag => { content => <<'$here_delimiter' } ); | ||
$code | ||
$here_delimiter | ||
END_SNIPPET | ||
} | ||
else { | ||
# always add a ';', just in case | ||
$script .= $code . ';'; | ||
} | ||
|
||
if ( -1 < index $closing_tag, '-' ) { | ||
$tokens[0] =~ s/^\s*//; | ||
my $temp = $&; | ||
$line += $temp =~ tr/\n//; | ||
} | ||
} | ||
|
||
return $script; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.