Skip to content

Commit

Permalink
Ignore special characters embedded in Javascript strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapJoris committed May 16, 2021
1 parent 1bbd290 commit 30f2e51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ class DjJS(Mode):
"""

STRING = r"[\"'`].*?[\"'`]"
BRACES = r"[\{\[\(\)\]\}]"
COMMENT = DjCSS.COMMENT
TOKEN = re.compile(Mode.TOKEN.pattern + f"|({BRACES})|({COMMENT})")
TOKEN = re.compile(Mode.TOKEN.pattern + f"|({STRING})|({BRACES})|({COMMENT})")

def indent(self, tabwidth, level=0):
lines = self.tokenize(tabwidth, level)
Expand All @@ -301,6 +302,12 @@ def get_token_type(self, raw_token):
return Token.Open
if raw_token in "}])":
return Token.Close
if raw_token.startswith(("'", '"')):
return Token.Text
if raw_token.startswith("`"):
if "\n" in raw_token:
return Comment
return Token.Text
if raw_token.startswith("/*"):
if "\n" in raw_token:
return Comment
Expand Down
13 changes: 13 additions & 0 deletions tests/suite/js.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ doStuff();
*
*/
</script>


<!-- Nasty characters embedded in strings -->
<script>
console.log("$(function() {");
console.log('$(function() {');
console.log(`$(function() {`);
console.log(`
function f() {
return 42;
}
`);
</script>
13 changes: 13 additions & 0 deletions tests/suite/js.out
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@
*
*/
</script>


<!-- Nasty characters embedded in strings -->
<script>
console.log("$(function() {");
console.log('$(function() {');
console.log(`$(function() {`);
console.log(`
function f() {
return 42;
}
`);
</script>

0 comments on commit 30f2e51

Please sign in to comment.