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 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
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
28 changes: 23 additions & 5 deletions src/languages/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ export default function(hljs) {
}
]
};

const SINGLE_QUOTE_STRING = {
className: 'string',
relevance: 0,
begin: /'/,
end: /'/,
contains: [
{
match: /''/,
scope: 'char.escape',
relevance: 0
}
]
};

const STRING = {
className: 'string',
relevance: 0,
variants: [
{
begin: /'/,
end: /'/
},
{
begin: /"/,
end: /"/
Expand All @@ -67,7 +78,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 +193,7 @@ export default function(hljs) {
},
OBJECT,
ARRAY,
SINGLE_QUOTE_STRING,
STRING
];

Expand Down
8 changes: 8 additions & 0 deletions test/markup/yaml/string.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
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>
<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;<span class="hljs-char escape_">&#x27;&#x27;</span>&#x27;</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;some<span class="hljs-char escape_">&#x27;&#x27;</span>value&#x27;</span>
8 changes: 8 additions & 0 deletions test/markup/yaml/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ key: |
multi-string
value
key: true

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