Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/ruby-handlebars/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Parser < Parslet::Parser
rule(:at) { str('@') }
rule(:tilde) { str('~') }
rule(:caret) { str('^') }
rule(:dash) { str('-') }
rule(:else_kw) { str('else') | caret }
rule(:as_kw) { str('as') }

Expand All @@ -24,6 +25,7 @@ class Parser < Parslet::Parser
rule(:tccurly) { space? >> tilde.maybe.as(:collapse_after) >> ccurly >> ccurly >> ccurly }
rule(:qocurly) { ocurly >> ocurly >> ocurly >> ocurly }
rule(:qccurly) { ccurly >> ccurly >> ccurly >> ccurly }
rule(:ddash) { dash >> dash }

rule(:else_absent?) { (else_kw >> space? >> dccurly).absent? }
rule(:as_absent?) { (as_kw >> space? >> pipe).absent? }
Expand All @@ -40,8 +42,9 @@ class Parser < Parslet::Parser
rule(:path) { identifier >> (dot >> (identifier | else_kw)).repeat }

rule(:nocurly) { match('[^{}]') }
rule(:noocurly) { match('[^{]') }
rule(:noccurly) { match('[^}]') }
rule(:noocurly) { match('[^{]') }
rule(:noccurly) { match('[^}]') }
rule(:nodash) { match('[^-]') }
rule(:eof) { any.absent? }

rule(:sq_string) { str("'") >> match("[^']").repeat.maybe.as(:str_content) >> str("'") }
Expand Down Expand Up @@ -109,9 +112,29 @@ class Parser < Parslet::Parser

rule(:comment) do
docurly >>
bang >>
space? >>
nocurly.repeat.maybe.as(:comment) >>
bang >> (
(
nodash >> (
nocurly.repeat(1) |
ocurly >> nocurly |
ocurly >> ocurly >> nocurly |
ccurly >> nocurly
).repeat(1)
).as(:comment) |
(
ddash >> (
match('[^{}-]').repeat(1) |
dash >> nodash |
dash >> dash >> noccurly |
ocurly >> noocurly |
ocurly >> ocurly >> noocurly |
ocurly >> ocurly >> ocurly >> noocurly |
ccurly |
ocurly >> eof
).repeat(1).as(:comment) >>
ddash
)
) >>
dccurly
end

Expand Down
10 changes: 10 additions & 0 deletions spec/handlebars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def evaluate(template, args = {})
it 'can remove comments with whitespace' do
expect(evaluate('Hello {{~! comment content~}} world')).to eq('Hello world')
end

it "removes multiline comments" do
expect(evaluate(<<~TEMPLATE.strip)).to eq("Hello\n\nworld")
Hello
{{!--
this is a multiline comment, with {{handlebars}} and -- included
--}}
world
TEMPLATE
end
end

context 'partials' do
Expand Down
29 changes: 23 additions & 6 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,29 @@
)
end

it 'comments' do
expect(parser.parse('{{! this is a comment }}')).to eq(
block_items: [
collapse_options.merge(comment: 'this is a comment ')
]
)
context "with comments" do
it 'parses single-line comments' do
expect(parser.parse('foo{{! this is a {{comment} }}bar')).to eq(
block_items: [
{template_content: 'foo'},
collapse_options.merge(comment: ' this is a {{comment} '),
{template_content: 'bar'},
]
)
end

it 'parses multi-line comments' do
template = <<~TEMPLATE.strip
foo{{!--\nthis is a multiline {{{comment}}},\nand it contains -- and {}.\n--}}bar
TEMPLATE
expect(parser.parse(template)).to eq(
block_items: [
{template_content: 'foo'},
collapse_options.merge(comment: "\nthis is a multiline {{{comment}}},\nand it contains -- and {}.\n"),
{template_content: 'bar'},
]
)
end
end

context 'helpers' do
Expand Down