From 746dd3dc5b74eb04d9ccc8bfb7e16f9ee1116624 Mon Sep 17 00:00:00 2001 From: Sharon Rosner Date: Sun, 21 Apr 2024 14:52:18 +0200 Subject: [PATCH] Compiler: Fix HTML escaping for non-string values (#17) --- lib/papercraft/compiler.rb | 2 +- test/fixtures/expr_text_compiled.rb | 2 +- test/fixtures/iteration.html | 1 + test/fixtures/iteration_compiled.rb | 5 +++++ test/fixtures/iteration_source.rb | 7 +++++++ test/test_compiler.rb | 4 +++- 6 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/iteration.html create mode 100644 test/fixtures/iteration_compiled.rb create mode 100644 test/fixtures/iteration_source.rb diff --git a/lib/papercraft/compiler.rb b/lib/papercraft/compiler.rb index fe7057d..d3934b1 100644 --- a/lib/papercraft/compiler.rb +++ b/lib/papercraft/compiler.rb @@ -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) diff --git a/test/fixtures/expr_text_compiled.rb b/test/fixtures/expr_text_compiled.rb index b3aa5ad..6034ab0 100644 --- a/test/fixtures/expr_text_compiled.rb +++ b/test/fixtures/expr_text_compiled.rb @@ -1,3 +1,3 @@ ->(__buffer__) { - __buffer__ << "

#{CGI.escapeHTML("#{a.zoo} - zoo")}

" + __buffer__ << "

#{CGI.escapeHTML(("#{a.zoo} - zoo").to_s)}

" } diff --git a/test/fixtures/iteration.html b/test/fixtures/iteration.html new file mode 100644 index 0000000..f091cd8 --- /dev/null +++ b/test/fixtures/iteration.html @@ -0,0 +1 @@ +

1

2

3

4

\ No newline at end of file diff --git a/test/fixtures/iteration_compiled.rb b/test/fixtures/iteration_compiled.rb new file mode 100644 index 0000000..7afb1ee --- /dev/null +++ b/test/fixtures/iteration_compiled.rb @@ -0,0 +1,5 @@ +->(__buffer__) { + items.each { |i| + __buffer__ << "

#{CGI.escapeHTML((i).to_s)}

" + } +} diff --git a/test/fixtures/iteration_source.rb b/test/fixtures/iteration_source.rb new file mode 100644 index 0000000..f48b714 --- /dev/null +++ b/test/fixtures/iteration_source.rb @@ -0,0 +1,7 @@ +items = [1, 2, 3, 4] + +->() { + items.each { |i| + p i + } +} diff --git a/test/test_compiler.rb b/test/test_compiler.rb index f730c18..bf8e807 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -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