Skip to content

Commit b2c2a6c

Browse files
authored
fix: nested quotes (#40)
* fix: regex when json values includes quotes * fix: escape quotes regex
1 parent a6d1fd6 commit b2c2a6c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('unicode', () => {
3030
.toBe('[string]"Hello, world!"[clear]')
3131
})
3232

33+
it('strings (mixing quotes)', () => {
34+
expect(hlUni('\'"`\' "\'`" `"\'`'))
35+
.toBe('[string]\'"`\'[clear] [string]"\'`"[clear] [string]`"\'`[clear]')
36+
})
37+
38+
it('strings (scaping quotes)', () => {
39+
expect(hlUni('\'\\\'\' "\\"" `\\``'))
40+
.toBe('[string]\'\\\'\'[clear] [string]"\\""[clear] [string]`\\``[clear]')
41+
})
42+
3343
it('integers', () => {
3444
expect(hlUni('42'))
3545
.toBe('[number]42[clear]')
@@ -102,6 +112,16 @@ describe('html', () => {
102112
.toBe('<span class="sql-hl-string">"Hello, world!"</span>')
103113
})
104114

115+
it('strings (mixing quotes)', () => {
116+
expect(hlHtml('\'"`\' "\'`" `"\'`'))
117+
.toBe('<span class="sql-hl-string">\'"`\'</span> <span class="sql-hl-string">"\'`"</span> <span class="sql-hl-string">`"\'`</span>')
118+
})
119+
120+
it('strings (scaping quotes)', () => {
121+
expect(hlHtml('\'\\\'\' "\\"" `\\``'))
122+
.toBe('<span class="sql-hl-string">\'\\\'\'</span> <span class="sql-hl-string">"\\""</span> <span class="sql-hl-string">`\\``</span>')
123+
})
124+
105125
it('integers', () => {
106126
expect(hlHtml('42'))
107127
.toBe('<span class="sql-hl-number">42</span>')

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const highlighters = [
3939
},
4040
{
4141
name: 'string',
42-
regex: /(["'`].*?["'`])/g
42+
regex: /(['](?:\\'|.)*?[']|["](?:\\"|.)*?["]|[`](?:\\`|.)*?[`])/g
4343
},
4444
{
4545
name: 'bracket',

test/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ console.log(highlight('SELECT f1, f20b, f3a, -1, 1, "1", 1.00 FROM t1;'))
1818

1919
console.log(highlight('SELECT id FROM listings WHERE status = \'not available\''))
2020
console.log(highlight('SELECT id FROM listings WHERE status = "not available"'))
21+
22+
console.log(highlight('SELECT \'{"json_index":"json_value"}\' AS test;'))
23+
console.log(highlight('SELECT "This is a \\"text\\" test" AS text;'))

0 commit comments

Comments
 (0)