Skip to content

Commit

Permalink
Bash: Added support for escaped quotes (PrismJS#2256)
Browse files Browse the repository at this point in the history
Single and double quotes can be escaped via backslashes. Prism now supports these escapes.
  • Loading branch information
RunDevelopment authored and quentinvernot committed Sep 11, 2020
1 parent 5c490b8 commit c7b4766
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 31 deletions.
3 changes: 2 additions & 1 deletion components/prism-bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
},
// “Normal” string
{
pattern: /(["'])(?:\\[\s\S]|\$\([^)]+\)|`[^`]+`|(?!\1)[^\\])*\1/,
pattern: /(^|[^\\](?:\\\\)*)(["'])(?:\\[\s\S]|\$\([^)]+\)|`[^`]+`|(?!\2)[^\\])*\2/,
lookbehind: true,
greedy: true,
inside: insideString
}
Expand Down
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

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

4 changes: 2 additions & 2 deletions components/prism-shell-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'info': {
// foo@bar:~/files$ exit
// foo@bar$ exit
pattern: /^(?:[^\r\n$#*!]+)(?=[$#])/m,
pattern: /^[^\r\n$#*!]+(?=[$#])/m,
alias: 'punctuation',
inside: {
'path': {
Expand All @@ -33,7 +33,7 @@
}
},
'command': {
pattern: RegExp(/[$#](?:[^\r\n'"<]|<<str>>)+/.source.replace(/<<str>>/g, strings)),
pattern: RegExp(/[$#](?:[^\\\r\n'"<]|\\.|<<str>>)+/.source.replace(/<<str>>/g, strings)),
greedy: true,
inside: {
'bash': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-shell-session.min.js

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

113 changes: 88 additions & 25 deletions tests/languages/bash/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,124 @@ bar'
'"bar"'
"$@"
"${foo}"
\\"foo"
\'a # ' not a string

<< STRING_END
foo
bar
STRING_END

<<- STRING_END
foo
bar
STRING_END

<< EOF
foo $@
bar
EOF

<< 'EOF'
'single quoted string'
"double quoted string"
EOF

<< "EOF"
foo
$bar
EOF

<< STRING_END
# comment
STRING_END

" # comment "

----------------------------------------------------

[
["string", ["\"\""]],
["string", ["''"]],
["string", ["\"foo\""]],
["string", ["'foo'"]],
["string", ["\"foo\r\nbar\""]],
["string", ["'foo\r\nbar'"]],
["string", ["\"'foo'\""]],
["string", ["'\"bar\"'"]],
["string", [
"\"", ["variable", "$@"], "\""
]],
["string", [
"\"", ["variable", ["${foo}"]], "\""
]],
["operator", ["<<"]],
["string", ["STRING_END\r\nfoo\r\nbar\r\nSTRING_END"]],
["operator", ["<<-"]],
["string", ["STRING_END\r\nfoo\r\nbar\r\nSTRING_END"]],
["operator", ["<<"]],
["string", ["EOF\r\nfoo ", ["variable", "$@"], "\r\nbar\r\nEOF"]],
["operator", ["<<"]],
["string", [
"\"\""
]],
["string", [
"''"
]],
["string", [
"\"foo\""
]],
["string", [
"'foo'"
]],
["string", [
"\"foo\r\nbar\""
]],
["string", [
"'foo\r\nbar'"
]],
["string", [
"\"'foo'\""
]],
["string", [
"'\"bar\"'"
]],
["string", [
"\"",
["variable", "$@"],
"\""
]],
["string", [
"\"",
["variable", [
"${foo}"
]],
"\""
]],
["punctuation", "\\"],
["punctuation", "\\"],
["string", [
"\"foo\""
]],
["punctuation", "\\"],
"'a ",
["comment", "# ' not a string"],
["operator", [
"<<"
]],
["string", [
"STRING_END\r\nfoo\r\nbar\r\nSTRING_END"
]],
["operator", [
"<<-"
]],
["string", [
"STRING_END\r\nfoo\r\nbar\r\nSTRING_END"
]],
["operator", [
"<<"
]],
["string", [
"EOF\r\nfoo ",
["variable", "$@"],
"\r\nbar\r\nEOF"
]],
["operator", [
"<<"
]],
["string", "'EOF'\r\n'single quoted string'\r\n\"double quoted string\"\r\nEOF"],
["operator", ["<<"]],
["operator", [
"<<"
]],
["string", "\"EOF\"\r\nfoo\r\n$bar\r\nEOF"],
["operator", ["<<"]],
["string", ["STRING_END\r\n# comment\r\nSTRING_END"]],
["string", ["\" # comment \""]]
["operator", [
"<<"
]],
["string", [
"STRING_END\r\n# comment\r\nSTRING_END"
]],
["string", [
"\" # comment \""
]]
]

----------------------------------------------------
Expand Down
15 changes: 14 additions & 1 deletion tests/languages/shell-session/command_string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ foo
bar
STRING_END

$ echo \'a # '

----------------------------------------------------

[
Expand Down Expand Up @@ -58,7 +60,18 @@ STRING_END
]],
["string", "\"STRING_END\"\r\nfoo\r\nbar\r\nSTRING_END"]
]]
]]
]],

["command", [
["shell-symbol", "$"],
["bash", [
["builtin", "echo"],
["punctuation", "\\"],
"'a ",
["comment", "# "]
]]
]],
["output", "'"]
]

----------------------------------------------------
Expand Down

0 comments on commit c7b4766

Please sign in to comment.