Skip to content

Commit e11c88b

Browse files
authored
fix(yaml) - correct escaping behavior in single-quoted YAML strings (#4159)
* add 1char.escape1 scope to escape sequences in single quoted strings
1 parent e42dae5 commit e11c88b

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Core Grammars:
3939
- fix(nix) handle backslash string escapes [h7x4][]
4040
- fix(nix) don't mix escapes for `"` and `''` strings [h7x4][]
4141
- fix(swift) - Fixed syntax highlighting for class func/var declarations [guuido]
42+
- fix(yaml) - Fixed wrong escaping behavior in single quoted strings [guuido]
4243

4344
New Grammars:
4445

src/languages/yaml.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,25 @@ export default function(hljs) {
4242
}
4343
]
4444
};
45+
46+
const SINGLE_QUOTE_STRING = {
47+
className: 'string',
48+
relevance: 0,
49+
begin: /'/,
50+
end: /'/,
51+
contains: [
52+
{
53+
match: /''/,
54+
scope: 'char.escape',
55+
relevance: 0
56+
}
57+
]
58+
};
59+
4560
const STRING = {
4661
className: 'string',
4762
relevance: 0,
4863
variants: [
49-
{
50-
begin: /'/,
51-
end: /'/
52-
},
5364
{
5465
begin: /"/,
5566
end: /"/
@@ -67,7 +78,13 @@ export default function(hljs) {
6778
const CONTAINER_STRING = hljs.inherit(STRING, { variants: [
6879
{
6980
begin: /'/,
70-
end: /'/
81+
end: /'/,
82+
contains: [
83+
{
84+
begin: /''/,
85+
relevance: 0
86+
}
87+
]
7188
},
7289
{
7390
begin: /"/,
@@ -176,6 +193,7 @@ export default function(hljs) {
176193
},
177194
OBJECT,
178195
ARRAY,
196+
SINGLE_QUOTE_STRING,
179197
STRING
180198
];
181199

test/markup/yaml/string.expect.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
multi-string
66
value
77
</span><span class="hljs-attr">key:</span> <span class="hljs-literal">true</span>
8+
9+
<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;\&#x27;</span>
10+
<span class="hljs-attr">key:</span> <span class="hljs-string">&quot;\\&quot;</span>
11+
<span class="hljs-attr">key:</span> <span class="hljs-string">&quot;\&quot;
12+
key: value&quot;</span>
13+
<span class="hljs-attr">key:</span> <span class="hljs-string">value</span>
14+
<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;<span class="hljs-char escape_">&#x27;&#x27;</span>&#x27;</span>
15+
<span class="hljs-attr">key:</span> <span class="hljs-string">&#x27;some<span class="hljs-char escape_">&#x27;&#x27;</span>value&#x27;</span>

test/markup/yaml/string.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ key: |
55
multi-string
66
value
77
key: true
8+
9+
key: '\'
10+
key: "\\"
11+
key: "\"
12+
key: value"
13+
key: value
14+
key: ''''
15+
key: 'some''value'

0 commit comments

Comments
 (0)