Skip to content

Commit 65fdec0

Browse files
authored
Parse ERB inside HTML Comments (#44)
This pull request improves the parsing of ERB tags inside HTML comment nodes. Additionally, a new test case has been added to ensure the correct parsing of interpolated content within HTML comments. We also changed the structure of the existing HTML comment nodes. The content of the HTML comment itself is now stored in the `childrens` array as literals or ERB Nodes.
1 parent 2c233b1 commit 65fdec0

7 files changed

+64
-15
lines changed

β€Žsrc/parser.cβ€Ž

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,26 @@ static AST_NODE_T* parser_parse_html_comment(parser_T* parser, AST_NODE_T* eleme
9292
buffer_T comment = buffer_new();
9393

9494
while (parser->current_token->type != TOKEN_EOF && parser->current_token->type != TOKEN_HTML_COMMENT_END) {
95-
token_T* token = parser_consume(parser, parser->current_token->type, comment_node);
96-
buffer_append(&comment, token->value);
97-
}
95+
switch (parser->current_token->type) {
96+
case TOKEN_ERB_START: {
97+
parser_build_node(parser, AST_LITERAL_NODE, buffer_value(&comment), comment_node);
98+
comment = buffer_new();
99+
parser_parse_erb_tag(parser, comment_node);
100+
break;
101+
}
98102

99-
parser_consume(parser, TOKEN_HTML_COMMENT_END, comment_node);
103+
default: {
104+
token_T* token = parser_consume(parser, parser->current_token->type, comment_node);
105+
buffer_append(&comment, token->value);
106+
}
107+
}
108+
}
100109

101-
comment_node->name = buffer_value(&comment);
110+
if (buffer_length(&comment) >= 0) {
111+
parser_build_node(parser, AST_LITERAL_NODE, buffer_value(&comment), comment_node);
112+
}
102113

114+
parser_consume(parser, TOKEN_HTML_COMMENT_END, comment_node);
103115
array_append(element->children, comment_node);
104116

105117
return comment_node;

β€Žtest/parser/erb_test.rbβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ class ERBTest < Minitest::Spec
4949
test "interpolate inside attribute value with static content around" do
5050
assert_parsed_snapshot(%(<h1 class="text-white <%= "bg-black" %> title"></h1>))
5151
end
52+
53+
test "interpolate inside comment" do
54+
assert_parsed_snapshot(%(<!-- <%= "Comment" %> -->))
55+
end
5256
end
5357
end

β€Žtest/snapshots/parser/comments_test/test_0001_HTML_comment_with_padding_whitespace_9cb56068ef4732067422a670d943c08e.txtβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
β”œβ”€β”€ name: βˆ…
33
└── children: (1)
44
@ Comment (location: (0,0)-(0,0))
5-
β”œβ”€β”€ name: " Hello World "
6-
└── children: []
5+
β”œβ”€β”€ name: βˆ…
6+
└── children: (1)
7+
@ Literal (location: (0,0)-(0,0))
8+
β”œβ”€β”€ name: " Hello World "
9+
└── children: []
710

β€Žtest/snapshots/parser/comments_test/test_0002_HTML_comment_with_no_whitespace_4644299a23e9ee76cdc970a26168b559.txtβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
β”œβ”€β”€ name: βˆ…
33
└── children: (1)
44
@ Comment (location: (0,0)-(0,0))
5-
β”œβ”€β”€ name: "Hello World"
6-
└── children: []
5+
β”œβ”€β”€ name: βˆ…
6+
└── children: (1)
7+
@ Literal (location: (0,0)-(0,0))
8+
β”œβ”€β”€ name: "Hello World"
9+
└── children: []
710

β€Žtest/snapshots/parser/comments_test/test_0003_HTML_comment_followed_by_html_tag_573b93df7df7d12ef12722008abfc7ee.txtβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
β”œβ”€β”€ name: βˆ…
33
└── children: (2)
44
@ Comment (location: (0,0)-(0,0))
5-
β”œβ”€β”€ name: "Hello World"
6-
└── children: []
5+
β”œβ”€β”€ name: βˆ…
6+
└── children: (1)
7+
@ Literal (location: (0,0)-(0,0))
8+
β”œβ”€β”€ name: "Hello World"
9+
└── children: []
710

811
@ Element (location: (0,0)-(0,0))
912
β”œβ”€β”€ name: βˆ…

β€Žtest/snapshots/parser/comments_test/test_0004_HTML_comment_followed_by_html_tag_with_nested_comment_f4042eb3e6a5d62fcc86e4162fe10cf2.txtβ€Ž

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
└── children: []
77

88
@ Comment (location: (0,0)-(0,0))
9-
β”œβ”€β”€ name: "Hello World"
10-
└── children: []
9+
β”œβ”€β”€ name: βˆ…
10+
└── children: (1)
11+
@ Literal (location: (0,0)-(0,0))
12+
β”œβ”€β”€ name: "Hello World"
13+
└── children: []
1114

1215
@ TextContent (location: (0,0)-(0,0))
1316
β”œβ”€β”€ name: "\n "
@@ -27,8 +30,11 @@
2730
β”œβ”€β”€ name: βˆ…
2831
└── children: (1)
2932
@ Comment (location: (0,0)-(0,0))
30-
β”œβ”€β”€ name: " Hello World "
31-
└── children: []
33+
β”œβ”€β”€ name: βˆ…
34+
└── children: (1)
35+
@ Literal (location: (0,0)-(0,0))
36+
β”œβ”€β”€ name: " Hello World "
37+
└── children: []
3238

3339
@ CloseTag (location: (0,0)-(0,0))
3440
β”œβ”€β”€ name: "h1"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@ DocumentNode (location: (0,0)-(0,0))
2+
β”œβ”€β”€ name: βˆ…
3+
└── children: (1)
4+
@ Comment (location: (0,0)-(0,0))
5+
β”œβ”€β”€ name: βˆ…
6+
└── children: (3)
7+
@ Literal (location: (0,0)-(0,0))
8+
β”œβ”€β”€ name: " "
9+
└── children: []
10+
11+
@ ERBContent (location: (0,0)-(0,0))
12+
β”œβ”€β”€ name: " "Comment" "
13+
└── children: []
14+
15+
@ Literal (location: (0,0)-(0,0))
16+
β”œβ”€β”€ name: " "
17+
└── children: []
18+

0 commit comments

Comments
Β (0)