Skip to content

Commit

Permalink
Compiler: add support for #text (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed May 8, 2024
1 parent fb808be commit 6df970a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/papercraft/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions lib/papercraft/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/compiler/text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>foo&amp;bar</h1><h2>bar&amp;baz</h2>
3 changes: 3 additions & 0 deletions test/fixtures/compiler/text_compiled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
->(__buffer__) {
__buffer__ << "<h1>foo&amp;bar</h1><h2>#{CGI.escapeHTML((x).to_s)}</h2>"
}
11 changes: 11 additions & 0 deletions test/fixtures/compiler/text_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x = 'bar&baz'

->() {
h1 {
text 'foo&bar'
}
text # should emit nothing
h2 {
text x
}
}

0 comments on commit 6df970a

Please sign in to comment.