Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yaml) - correct escaping behavior in single-quoted YAML strings #4159

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Core Grammars:
- fix(nix) handle backslash string escapes [h7x4][]
- fix(nix) don't mix escapes for `"` and `''` strings [h7x4][]
- fix(swift) - Fixed syntax highlighting for class func/var declarations [guuido]
- fix(yaml) - Fixed wrong escaping behavior in single quoted strings [guuido]

New Grammars:

Expand Down
27 changes: 24 additions & 3 deletions src/languages/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,28 @@ export default function(hljs) {
}
]
};
const STRING = {

const SINGLE_QUOTE_STRING = {
className: 'string',
relevance: 0,
variants: [
guuido marked this conversation as resolved.
Show resolved Hide resolved
{
begin: /'/,
end: /'/
},
}
],
contains: [
{
begin: /''/,
guuido marked this conversation as resolved.
Show resolved Hide resolved
relevance: 0
}
]
};

const STRING = {
className: 'string',
relevance: 0,
variants: [
{
begin: /"/,
end: /"/
Expand All @@ -67,7 +81,13 @@ export default function(hljs) {
const CONTAINER_STRING = hljs.inherit(STRING, { variants: [
{
begin: /'/,
end: /'/
end: /'/,
contains: [
{
begin: /''/,
relevance: 0
}
]
},
{
begin: /"/,
Expand Down Expand Up @@ -176,6 +196,7 @@ export default function(hljs) {
},
OBJECT,
ARRAY,
SINGLE_QUOTE_STRING,
STRING
];

Expand Down
6 changes: 6 additions & 0 deletions test/markup/yaml/string.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
multi-string
value
</span><span class="hljs-attr">key:</span> <span class="hljs-literal">true</span>

<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;\&#x27;</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">&quot;\\&quot;</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">&quot;\&quot;
key: value&quot;</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">value</span>
7 changes: 7 additions & 0 deletions test/markup/yaml/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ key: |
multi-string
value
key: true

key: '\'
key: "\\"
key: "\"
key: value"
key: value