Skip to content
Open
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
49 changes: 33 additions & 16 deletions ext/liquid_c/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,47 @@ void tokenizer_next(tokenizer_t *tokenizer, token_t *token)
token->type = TOKEN_INVALID;
if (c == '%') {
while (cursor < last) {
if (*cursor++ != '%')
continue;
c = *cursor++;
while (c == '%' && cursor <= last)
c = *cursor++;
if (c != '}')
continue;
token->type = TOKEN_TAG;
goto found;
switch (c) {
case '"': case '\'': {
const char *end_quote = memchr(cursor, c, last + 1 - cursor);
if (end_quote) {
cursor = end_quote + 1;
}
break;
}
case '%': {
if (*cursor == '}') {
cursor++;
token->type = TOKEN_TAG;
goto found;
}
break;
}
}
}
// unterminated tag
cursor = tokenizer->cursor + 2;
goto found;
} else {
while (cursor < last) {
if (*cursor++ != '}')
continue;
if (*cursor++ != '}') {
// variable incomplete end, used to end raw tags
cursor--;
goto found;
c = *cursor++;
switch (c) {
case '"': case '\'': {
const char *end_quote = memchr(cursor, c, last + 1 - cursor);
if (end_quote) {
cursor = end_quote + 1;
}
break;
}
case '}': {
if (*cursor == '}') {
cursor++;
token->type = TOKEN_VARIABLE;
} // else variable incomplete end, used to end raw tags
goto found;
}
}
token->type = TOKEN_VARIABLE;
goto found;
}
// unterminated variable
cursor = tokenizer->cursor + 2;
Expand Down
13 changes: 13 additions & 0 deletions test/unit/tokenizer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def test_utf8_encoded_template
assert_equal [source], output
end

def test_quoted_strings
assert_equal ['{% assign foo = "%}" %}'], tokenize('{% assign foo = "%}" %}')
assert_equal ["{%assign foo = '%}'%}"], tokenize("{%assign foo = '%}'%}")
assert_equal ['{{ "}}" }}'], tokenize('{{ "}}" }}')
assert_equal ["{{'}}'}}"], tokenize("{{'}}'}}")
end

def test_unterminated_quotes
assert_equal ['{% assign foo = " %}'], tokenize('{% assign foo = " %}')
assert_equal ['{{ " }}'], tokenize('{{ " }}')
assert_equal ["{{'}}"], tokenize("{{'}}")
end

private

def tokenize(source)
Expand Down