mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
tools: Enforce '_html' suffix for unescaped hbs vars.
This adds a check to enforce the new convention of raw HTML variables having a `_html` suffix for better clarity. Discussion: https://chat.zulip.org/#narrow/channel/92-learning/topic/Marking.20commits.20to.20be.20squashed.20in.20PRs Signed-off-by: apoorvapendse <apoorvavpendse@gmail.com>
This commit is contained in:
committed by
Tim Abbott
parent
a935866601
commit
3188d9db31
@@ -81,6 +81,9 @@ def tokenize(text: str, template_format: str | None = None) -> list[Token]:
|
||||
def looking_at_handlebars_partial_block() -> bool:
|
||||
return template_format == "handlebars" and looking_at("{{#>")
|
||||
|
||||
def looking_at_handlebars_triple_stache() -> bool:
|
||||
return template_format == "handlebars" and (looking_at("{{{") or looking_at("{{~{"))
|
||||
|
||||
def looking_at_html_start() -> bool:
|
||||
return looking_at("<") and not looking_at("</")
|
||||
|
||||
@@ -184,6 +187,19 @@ def tokenize(text: str, template_format: str | None = None) -> list[Token]:
|
||||
s = get_handlebars_tag(text, state.i)
|
||||
tag = "else"
|
||||
kind = "handlebars_else"
|
||||
elif looking_at_handlebars_triple_stache():
|
||||
s = get_handlebars_triple_stache_tag(text, state.i)
|
||||
start_offset = end_offset = 3
|
||||
if s.startswith("{{~{"):
|
||||
start_offset += 1
|
||||
if s.endswith("}~}}"):
|
||||
end_offset += 1
|
||||
tag = s[start_offset:-end_offset].strip()
|
||||
if not tag.endswith("_html"):
|
||||
raise TemplateParserError(
|
||||
"Unescaped variables in triple staches {{{ }}} must be suffixed with `_html`"
|
||||
)
|
||||
kind = "handlebars_triple_stache"
|
||||
elif looking_at_handlebars_start():
|
||||
s = get_handlebars_tag(text, state.i)
|
||||
tag = s[3:-2].split()[0].strip("#").removeprefix("*")
|
||||
@@ -321,6 +337,7 @@ def tag_flavor(token: Token) -> str | None:
|
||||
"template_var",
|
||||
"text",
|
||||
"whitespace",
|
||||
"handlebars_triple_stache",
|
||||
):
|
||||
return None
|
||||
|
||||
@@ -632,6 +649,18 @@ def get_handlebars_tag(text: str, i: int) -> str:
|
||||
return s
|
||||
|
||||
|
||||
def get_handlebars_triple_stache_tag(text: str, i: int) -> str:
|
||||
end = i + 3
|
||||
while end < len(text) - 3 and text[end] != "}":
|
||||
end += 1
|
||||
if text[end : end + 3] == "}}}":
|
||||
return text[i : end + 3]
|
||||
elif end + 4 <= len(text) and text[end : end + 4] == "}~}}":
|
||||
return text[i : end + 4]
|
||||
else:
|
||||
raise TokenizationError('Tag missing "}}}"', text[i : end + 3])
|
||||
|
||||
|
||||
def get_spaces(text: str, i: int) -> str:
|
||||
s = ""
|
||||
while i < len(text) and text[i] in " ":
|
||||
|
||||
Reference in New Issue
Block a user