summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Text/Xslate/Syntax/Handlebars.pm15
-rw-r--r--t/expressions.t24
2 files changed, 37 insertions, 2 deletions
diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm
index dd543dc..fe2f083 100644
--- a/lib/Text/Xslate/Syntax/Handlebars.pm
+++ b/lib/Text/Xslate/Syntax/Handlebars.pm
@@ -553,10 +553,25 @@ sub define_function {
return;
}
+sub parse_literal {
+ my $self = shift;
+ my ($literal) = @_;
+
+ if ($literal =~ /\A\[(.*)\]\z/ms) {
+ $literal = $1;
+ $literal =~ s/(["\\])/\\$1/g;
+ $literal = '"' . $literal . '"';
+ }
+
+ return $self->SUPER::parse_literal($literal);
+}
+
sub is_valid_field {
my $self = shift;
my ($field) = @_;
+ # allow foo.[10]
+ return 1 if $field->arity eq 'literal';
# undefined symbols are all treated as variables - see undefined_name
return 1 if $field->arity eq 'variable';
# allow ../../foo
diff --git a/t/expressions.t b/t/expressions.t
index f12022c..0026dbe 100644
--- a/t/expressions.t
+++ b/t/expressions.t
@@ -73,7 +73,28 @@ render_ok(
"backtracking into other hash variables with ../ and ."
);
-{ local $TODO = "unimplemented";
+render_ok(
+ '{{articles.[10].comments}}',
+ {
+ articles => {
+ 10 => { comments => "First post!" },
+ },
+ },
+ 'First post!',
+ "field access with non-identifiers"
+);
+
+render_ok(
+ '{{articles.[.foo\som#th"ing].comments}}',
+ {
+ articles => {
+ '.foo\som#th"ing' => { comments => "First post!" },
+ },
+ },
+ 'First post!',
+ "field access with non-identifiers"
+);
+
render_ok(
'{{articles.[10].comments}}',
{
@@ -85,7 +106,6 @@ render_ok(
'First post!',
"array dereferencing"
);
-}
render_ok(
'{{.}} {{this}}',