Skip to content

Commit

Permalink
Allow for directives with all whitespace value
Browse files Browse the repository at this point in the history
A directive with an empty (all whitespace) value like
```
{subtitle: }
```
will now result in a subtitle with value "". This is done in order to
make the parsing more robust.
  • Loading branch information
Thyrum committed Jul 24, 2024
1 parent f49ff55 commit 0384e3f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/chordpro/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def title(title)
alias_method :t, :title

def subtitle(subtitle)
@html.h2(class: "subtitle") { |h2| h2.text! subtitle }
unless subtitle.match(/^\s*$/)
@html.h2(class: "subtitle") { |h2| h2.text! subtitle }
end
end
alias_method :st, :subtitle
alias_method :su, :subtitle
Expand Down
5 changes: 5 additions & 0 deletions lib/chordpro/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class Transform < Parslet::Transform
Chordpro::Directive.new(directive_name, value.to_s)
end

rule(directive: {name: simple(:name), value: []}) do
directive_name = Directive.find(name) || Directive::Name.new(name.to_s)
Chordpro::Directive.new(directive_name, "")
end

rule(directive: {name: simple(:name)}) do
directive_name = Directive.find(name) || Directive::Name.new(name.to_s)
Chordpro::Directive.new(directive_name)
Expand Down
3 changes: 3 additions & 0 deletions spec/chordpro/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def html(string)
it "renders h2 for #{name}" do
expect(html("{#{name}:The Beatles}").to_s).to eq('<h2 class="subtitle">The Beatles</h2>')
end
it "allows empty #{name}" do
expect(html("{#{name}: }").to_s).to eq('')
end
end

it "guards against xss in the subtitle" do
Expand Down

0 comments on commit 0384e3f

Please sign in to comment.