diff --git a/lib/escapeHtml.js b/lib/escapeHtml.js index feebb2b..2abc6c2 100644 --- a/lib/escapeHtml.js +++ b/lib/escapeHtml.js @@ -34,7 +34,11 @@ const charCodeMap = { 38: '&', // & 39: ''', // ' 60: '<', // < - 62: '>' // > + 62: '>', // > + 92: '\', // \\ + 9: '\t', // \t + 10: '\n', // \n + 13: '\r' // \r } function escapeHtml (str) { diff --git a/lib/index.js b/lib/index.js index ba2d539..77358ac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -97,6 +97,9 @@ function highlight (sqlString, options) { const escapedContent = options.htmlEscaper(content) return `${escapedContent}` } + if (name === 'string') { + content = JSON.stringify(content).slice(1, -1).replaceAll('\\"', '"') + } return options.colors[name] + content + options.colors.clear }) .join('') diff --git a/test/debug.js b/test/debug.js index 10610d8..071dbe2 100644 --- a/test/debug.js +++ b/test/debug.js @@ -21,6 +21,7 @@ console.log(highlight('SELECT id FROM listings WHERE status = "not available"')) console.log(highlight('SELECT \'{"json_index":"json_value"}\' AS test;')) console.log(highlight('SELECT "This is a \\"text\\" test" AS text;')) +console.log(highlight('SELECT \'\\\r\t\n\' AS text;')) console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) diff --git a/test/index.test.js b/test/index.test.js index a208a3a..33baf94 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -38,7 +38,12 @@ describe('unicode', () => { it('strings (scaping quotes)', () => { expect(hlUni('\'\\\'\' "\\"" `\\``')) - .toBe('[string]\'\\\'\'[clear] [string]"\\""[clear] [string]`\\``[clear]') + .toBe('[string]\'\\\\\'\'[clear] [string]"\\\\""[clear] [string]`\\\\``[clear]') + }) + + it('strings (line breaks)', () => { + expect(hlUni('\'\\\r\n\t\'')) + .toBe('[string]\'\\\\\\r\\n\\t\'[clear]') }) it('integers', () => { @@ -155,7 +160,12 @@ describe('html', () => { it('strings (scaping quotes)', () => { expect(hlHtml('\'\\\'\' "\\"" `\\``')) - .toBe(''\\'' "\\"" `\\``') + .toBe(''\'' "\"" `\``') + }) + + it('strings (line breaks)', () => { + expect(hlHtml('\'\\\r\n\t\'')) + .toBe(''\\r\n\t'') }) it('integers', () => {