Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ errors:
- name: opening_tag
type: token

- name: StrayERBClosingTagError
message:
template: "Stray `%%>` found at (%u:%u). This closing delimiter is not part of an ERB tag and will be treated as plain text. If you want a literal `%%>`, use the HTML entities `%>` instead."
arguments:
- start.line
- start.column

fields: []

- name: NestedERBTagError
message:
template: "ERB tag `%s` at (%u:%u) was terminated by nested `<%%` tag at (%zu:%zu). Nesting `<%%` tags is not supported."
Expand Down
10 changes: 10 additions & 0 deletions sig/herb/errors.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ static AST_HTML_TEXT_NODE_T* parser_parse_text_content(parser_T* parser, hb_arra
return NULL;
}

if (parser->options.strict && parser->current_token->type == TOKEN_PERCENT) {
lexer_T lexer_copy = *parser->lexer;
token_T* peek_token = lexer_next_token(&lexer_copy);

if (peek_token && peek_token->type == TOKEN_HTML_TAG_END) {
position_T stray_start = parser->current_token->location.start;
position_T stray_end = peek_token->location.end;
token_free(peek_token);

append_strayerb_closing_tag_error(stray_start, stray_end, document_errors);

token_T* percent = parser_advance(parser);
hb_buffer_append(&content, percent->value);
token_free(percent);

token_T* gt = parser_advance(parser);
hb_buffer_append(&content, gt->value);
token_free(gt);

continue;
}

token_free(peek_token);
}

token_T* token = parser_advance(parser);
hb_buffer_append(&content, token->value);
token_free(token);
Expand Down Expand Up @@ -1004,6 +1029,32 @@ static AST_HTML_OPEN_TAG_NODE_T* parser_parse_html_open_tag(parser_T* parser) {
token_free(next_token);
}

if (parser->current_token->type == TOKEN_PERCENT) {
lexer_T lexer_copy = *parser->lexer;
token_T* peek_token = lexer_next_token(&lexer_copy);

if (peek_token && peek_token->type == TOKEN_HTML_TAG_END) {
position_T stray_start = parser->current_token->location.start;
position_T stray_end = peek_token->location.end;
token_free(peek_token);

append_strayerb_closing_tag_error(stray_start, stray_end, errors);

token_T* percent = parser_advance(parser);
token_T* gt = parser_advance(parser);

AST_LITERAL_NODE_T* literal = ast_literal_node_init("%>", stray_start, stray_end, NULL);
hb_array_append(children, literal);

token_free(percent);
token_free(gt);

continue;
}

token_free(peek_token);
}

parser_append_unexpected_error(
parser,
"Unexpected Token",
Expand Down
81 changes: 81 additions & 0 deletions test/parser/stray_erb_closing_tag_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Parser
class StrayERBClosingTagTest < Minitest::Spec
include SnapshotUtils

test "stray %> in open tag after unquoted ERB attribute value" do
template = <<~HTML
<div id=<%= dom_id(@slot) %> %>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "stray %> in open tag after quoted ERB attribute value" do
template = <<~HTML
<div id="<%= dom_id(@slot) %>" %>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "stray %> in open tag with no preceding ERB tag" do
template = <<~HTML
<div %>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "stray %> at document top level" do
template = <<~HTML
hello %> world
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "stray %> at document top level after ERB tag" do
template = <<~HTML
<% content %> %>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "%> inside quoted attribute value is not stray" do
template = <<~HTML
<div class="foo %> bar">hello</div>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "multiple stray %> in open tag" do
template = <<~HTML
<div %> %>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end

test "stray %> between attributes in open tag" do
template = <<~HTML
<div class="foo" %> id="bar"></div>
HTML

assert_parsed_snapshot(template, strict: true)
assert_parsed_snapshot(template, strict: false)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading