Skip to content

Commit f678cda

Browse files
authored
Add comment and raw to compiler (#936)
I didn't think there was really any optimization to be done to `raw` but it needs to split the buffer when it's encountered.
1 parent c405c1d commit f678cda

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

lib/phlex/compiler/method_compiler.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def visit_call_node(node)
4646
return compile_plain_helper(node)
4747
elsif fragment_helper?(node)
4848
return compile_fragment_helper(node)
49+
elsif comment_helper?(node)
50+
return compile_comment_helper(node)
51+
elsif raw_helper?(node)
52+
return compile_raw_helper(node)
4953
end
5054
end
5155

@@ -194,6 +198,19 @@ def compile_fragment_helper_block(node)
194198
)
195199
end
196200

201+
def compile_comment_helper(node)
202+
[
203+
buffer("<!-- "),
204+
visit_phlex_block(node.block),
205+
buffer(" -->"),
206+
]
207+
end
208+
209+
def compile_raw_helper(node)
210+
@current_buffer = nil
211+
node
212+
end
213+
197214
private def ensure_new_line
198215
proc(&:ensure_new_line)
199216
end
@@ -293,6 +310,14 @@ def compile_fragment_helper_block(node)
293310
node.name == :fragment && own_method_without_scope?(node)
294311
end
295312

313+
private def comment_helper?(node)
314+
node.name == :comment && own_method_without_scope?(node)
315+
end
316+
317+
private def raw_helper?(node)
318+
node.name == :raw && own_method_without_scope?(node)
319+
end
320+
296321
ALLOWED_OWNERS = [Phlex::SGML, Phlex::HTML, Phlex::SVG]
297322
private def own_method_without_scope?(node)
298323
ALLOWED_OWNERS.include?(@component.instance_method(node.name).owner)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class Comment < Phlex::HTML
4+
def view_template
5+
comment { "hello world" }
6+
comment { "Begin rendering #{self.class.name}" }
7+
end
8+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class Raw < Phlex::HTML
4+
def view_template
5+
p { "output before" }
6+
raw(safe("raw output in the middle"))
7+
p { "output after" }
8+
end
9+
end

0 commit comments

Comments
 (0)