Skip to content

Commit

Permalink
Compiler: Fix HTML escaping for non-string values (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Apr 21, 2024
1 parent 5548f8a commit 746dd3d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/papercraft/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def embed_visit(node, pre = '', post = '')
end

def html_embed_visit(node)
embed_visit(node, '#{CGI.escapeHTML(', ')}')
embed_visit(node, '#{CGI.escapeHTML((', ').to_s)}')
end

def tag_attr_embed_visit(node, key)
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/expr_text_compiled.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
->(__buffer__) {
__buffer__ << "<h1>#{CGI.escapeHTML("#{a.zoo} - zoo")}</h1>"
__buffer__ << "<h1>#{CGI.escapeHTML(("#{a.zoo} - zoo").to_s)}</h1>"
}
1 change: 1 addition & 0 deletions test/fixtures/iteration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>1</p><p>2</p><p>3</p><p>4</p>
5 changes: 5 additions & 0 deletions test/fixtures/iteration_compiled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
->(__buffer__) {
items.each { |i|
__buffer__ << "<p>#{CGI.escapeHTML((i).to_s)}</p>"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/iteration_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
items = [1, 2, 3, 4]

->() {
items.each { |i|
p i
}
}
4 changes: 3 additions & 1 deletion test/test_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class CompilerTest < Minitest::Test
assert_equal compiled_src, compiled_code

compiled_proc = eval(compiled_code, proc.binding)
assert_equal html, compiled_proc.call(+'')
compiled_html = +''
compiled_proc.call(compiled_html)
assert_equal html, compiled_html
end
end
end

0 comments on commit 746dd3d

Please sign in to comment.