From 6df970ae94b6d09df4075419429ea7a134fce601 Mon Sep 17 00:00:00 2001 From: Sharon Rosner Date: Wed, 8 May 2024 08:26:58 +0200 Subject: [PATCH] Compiler: add support for #text (#17) --- lib/papercraft/compiler.rb | 13 +++++++++++++ lib/papercraft/tags.rb | 5 +++-- test/fixtures/compiler/text.html | 1 + test/fixtures/compiler/text_compiled.rb | 3 +++ test/fixtures/compiler/text_source.rb | 11 +++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/compiler/text.html create mode 100644 test/fixtures/compiler/text_compiled.rb create mode 100644 test/fixtures/compiler/text_source.rb diff --git a/lib/papercraft/compiler.rb b/lib/papercraft/compiler.rb index 5de896a..788786b 100644 --- a/lib/papercraft/compiler.rb +++ b/lib/papercraft/compiler.rb @@ -82,6 +82,12 @@ def visit_call_node(node) return super if node.receiver @html_location_start ||= node.location + + case node.name + when :text + return emit_html_text(node) + end + inner_text, attrs = tag_args(node) block = node.block @@ -166,4 +172,11 @@ def emit_tag_attribute_node(node, key = false) tag_attr_embed_visit(node, key) end end + + def emit_html_text(node) + args = node.arguments&.arguments + return nil if !args + + emit_tag_inner_text(args[0]) + end end diff --git a/lib/papercraft/tags.rb b/lib/papercraft/tags.rb index 5f53d28..3bfb787 100644 --- a/lib/papercraft/tags.rb +++ b/lib/papercraft/tags.rb @@ -218,9 +218,10 @@ def method_missing(sym, *args, **opts, &block) # Emits text into the rendering buffer, escaping any special characters to # the respective XML entities. # - # @param data [String] text + # @param data [String, nil] text # @return [void] - def text(data) + def text(data = nil) + return if !data return if @render_fragment && @fragment != @render_fragment @buffer << escape_text(data) diff --git a/test/fixtures/compiler/text.html b/test/fixtures/compiler/text.html new file mode 100644 index 0000000..fc072f8 --- /dev/null +++ b/test/fixtures/compiler/text.html @@ -0,0 +1 @@ +

foo&bar

bar&baz

\ No newline at end of file diff --git a/test/fixtures/compiler/text_compiled.rb b/test/fixtures/compiler/text_compiled.rb new file mode 100644 index 0000000..3c55053 --- /dev/null +++ b/test/fixtures/compiler/text_compiled.rb @@ -0,0 +1,3 @@ +->(__buffer__) { + __buffer__ << "

foo&bar

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

" +} diff --git a/test/fixtures/compiler/text_source.rb b/test/fixtures/compiler/text_source.rb new file mode 100644 index 0000000..dc44232 --- /dev/null +++ b/test/fixtures/compiler/text_source.rb @@ -0,0 +1,11 @@ +x = 'bar&baz' + +->() { + h1 { + text 'foo&bar' + } + text # should emit nothing + h2 { + text x + } +}