Skip to content

Commit

Permalink
Merge pull request #207 from Shopify/raw-tag-with-extra-str
Browse files Browse the repository at this point in the history
allow extra string after raw tag delimiter
  • Loading branch information
ggmichaelgo authored Sep 7, 2023
2 parents 8f11a88 + 20beb30 commit 4c87f8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ext/liquid_c/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static bool match_full_token_possibly_invalid(token_t *token, struct full_token_
const char *curr_delimiter_start;
long curr_delimiter_len = 0;

bool is_last_char_whitespace = true;

// Search from the end of the string.
// The token could have a part of the body like this:
// {% endraw {% endraw %}
// In this case, we need to return body_len to 10 to preserve the body content.
for (long i = len - 3; i > 1; i--) {
char c = str[i];

Expand All @@ -35,13 +41,22 @@ static bool match_full_token_possibly_invalid(token_t *token, struct full_token_

if (is_word_char(c)) {
curr_delimiter_start = str + i;
curr_delimiter_len++;

if (is_last_char_whitespace) {
// start a new delimiter match
curr_delimiter_len = 1;
} else {
curr_delimiter_len++;
}
} else if (!is_word_char(c) && !is_whitespace) {
curr_delimiter_start = NULL;
curr_delimiter_len = 0;
}

is_last_char_whitespace = is_whitespace;

if (curr_delimiter_len > 0) {
// match start of a tag which is {% or {%-
if (
(str[i - 1] == '%' && str[i - 2] == '{') ||
(i - 3 >= 0 && str[i - 1] == '-' && str[i - 2] == '%' && str[i - 3] == '{')
Expand Down
10 changes: 10 additions & 0 deletions test/unit/raw_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def test_derived_class
end
end

def test_allows_extra_string_after_tag_delimiter
output = Liquid::Template.parse("{% raw %}message{% endraw this_is_allowed %}").render
assert_equal("message", output)

output = Liquid::Template.parse("{% raw %}message{% endraw r%}").render
assert_equal("message", output)
end

def test_ignores_incomplete_tag_delimter
output = Liquid::Template.parse("{% raw %}{% endraw {% endraw %}").render
assert_equal("{% endraw ", output)
Expand All @@ -55,6 +63,7 @@ def test_does_not_allow_nbsp_in_tag_delimiter
Liquid::Template.parse("{% raw %}body{% endraw\u00A0 %}")
Liquid::Template.parse("{% raw %}body{% endraw \u00A0 %}")
Liquid::Template.parse("{% raw %}body{% endraw \u00A0 endraw %}")
Liquid::Template.parse("{% raw %}body{% endraw\u00A0endraw %}")

[
"{%\u00A0endraw%}",
Expand All @@ -63,6 +72,7 @@ def test_does_not_allow_nbsp_in_tag_delimiter
"{% \u00A0 endraw%}",
"{%\u00A0endraw\u00A0%}",
"{% - endraw %}",
"{% endnot endraw %}",
].each do |bad_delimiter|
exception = assert_raises(
Liquid::SyntaxError,
Expand Down

0 comments on commit 4c87f8c

Please sign in to comment.