Skip to content

Commit

Permalink
fix(yaml) - correct escaping behavior in single-quoted YAML strings (#…
Browse files Browse the repository at this point in the history
…4159)

* add 1char.escape1 scope to escape sequences in single quoted strings
  • Loading branch information
guuido authored Nov 6, 2024
1 parent e42dae5 commit e11c88b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
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'

0 comments on commit e11c88b

Please sign in to comment.