From c274d3e42860e70806dc1f6de1532334d972ed89 Mon Sep 17 00:00:00 2001 From: Youssef Henna Date: Mon, 30 Sep 2024 14:05:01 +0300 Subject: [PATCH 1/4] Remove usage of lookbehind regex expression - Not supported on recent safari versions: https://caniuse.com/js-regexp-lookbehind --- src/VJsonParse.res | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/VJsonParse.res b/src/VJsonParse.res index 25bacc7..55e3daa 100644 --- a/src/VJsonParse.res +++ b/src/VJsonParse.res @@ -26,7 +26,6 @@ let parseVJsonWithVariable = parseVariableString => { \"<|>"(str("true") |> map(_ => true), str("false") |> map(_ => false)) |> lexeme let escapedQuoteRegex = %re(`/\\\\"/gm`) - let nonEsacapedQuoteRegex = %re(`/(? { regex(inQuotesRegex) |> map(match => { match - ->Js.String2.replaceByRe(nonEsacapedQuoteRegex, ``) - ->Js.String2.replaceByRe(escapedQuoteRegex, `"`) + // Get rid of all non-escaped quotes by temporarily replacing escaped quotes with a unique string + // then replacing that string with a quote after remaining non-escaped quotes have been removed. + // + // Detecting non-escaped quotes directly with a regex would require a lookbehind, which is not + // supported on recent versions of Safari. https://caniuse.com/js-regexp-lookbehind + // let nonEsacapedQuoteRegex = %re(`/(?Js.String2.replaceByRe(escapedQuoteRegex, `__ESCAPED__QUOTE__`) + ->Js.String2.replaceByRe(%re(`/"/gm`), ``) + ->Js.String2.replaceByRe(%re(`/__ESCAPED__QUOTE__/gm`), `"`) }) |> lexeme From bc527b752edc79dd116df30f4d3ef28d91f05fb1 Mon Sep 17 00:00:00 2001 From: Youssef Henna Date: Tue, 1 Oct 2024 11:13:17 +0300 Subject: [PATCH 2/4] Revert "Remove usage of lookbehind regex expression" This reverts commit c274d3e42860e70806dc1f6de1532334d972ed89. --- src/VJsonParse.res | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/VJsonParse.res b/src/VJsonParse.res index 55e3daa..25bacc7 100644 --- a/src/VJsonParse.res +++ b/src/VJsonParse.res @@ -26,6 +26,7 @@ let parseVJsonWithVariable = parseVariableString => { \"<|>"(str("true") |> map(_ => true), str("false") |> map(_ => false)) |> lexeme let escapedQuoteRegex = %re(`/\\\\"/gm`) + let nonEsacapedQuoteRegex = %re(`/(? { regex(inQuotesRegex) |> map(match => { match - // Get rid of all non-escaped quotes by temporarily replacing escaped quotes with a unique string - // then replacing that string with a quote after remaining non-escaped quotes have been removed. - // - // Detecting non-escaped quotes directly with a regex would require a lookbehind, which is not - // supported on recent versions of Safari. https://caniuse.com/js-regexp-lookbehind - // let nonEsacapedQuoteRegex = %re(`/(?Js.String2.replaceByRe(escapedQuoteRegex, `__ESCAPED__QUOTE__`) - ->Js.String2.replaceByRe(%re(`/"/gm`), ``) - ->Js.String2.replaceByRe(%re(`/__ESCAPED__QUOTE__/gm`), `"`) + ->Js.String2.replaceByRe(nonEsacapedQuoteRegex, ``) + ->Js.String2.replaceByRe(escapedQuoteRegex, `"`) }) |> lexeme From a50e2b5f100e3aee2f037a70c060ceee6c46f60a Mon Sep 17 00:00:00 2001 From: Youssef Henna Date: Tue, 1 Oct 2024 11:16:54 +0300 Subject: [PATCH 3/4] Update regex and re-add group when replacing --- src/VJsonParse.res | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/VJsonParse.res b/src/VJsonParse.res index 25bacc7..6fff89d 100644 --- a/src/VJsonParse.res +++ b/src/VJsonParse.res @@ -26,7 +26,7 @@ let parseVJsonWithVariable = parseVariableString => { \"<|>"(str("true") |> map(_ => true), str("false") |> map(_ => false)) |> lexeme let escapedQuoteRegex = %re(`/\\\\"/gm`) - let nonEsacapedQuoteRegex = %re(`/(? { regex(inQuotesRegex) |> map(match => { match - ->Js.String2.replaceByRe(nonEsacapedQuoteRegex, ``) + ->Js.String2.replaceByRe(nonEsacapedQuoteRegex, `$1`) // First group of the regex is characters before the quote that should be kept ->Js.String2.replaceByRe(escapedQuoteRegex, `"`) }) |> lexeme From 320833f2e679cb92f076c61f94487461560fdd37 Mon Sep 17 00:00:00 2001 From: Youssef Henna Date: Tue, 1 Oct 2024 11:17:21 +0300 Subject: [PATCH 4/4] typo --- src/VJsonParse.res | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/VJsonParse.res b/src/VJsonParse.res index 6fff89d..affcc63 100644 --- a/src/VJsonParse.res +++ b/src/VJsonParse.res @@ -26,7 +26,7 @@ let parseVJsonWithVariable = parseVariableString => { \"<|>"(str("true") |> map(_ => true), str("false") |> map(_ => false)) |> lexeme let escapedQuoteRegex = %re(`/\\\\"/gm`) - let nonEsacapedQuoteRegex = %re(`/((?:^|[^\\\\])(?:\\\\{2})*)"/gm`) + let nonEscapedQuoteRegex = %re(`/((?:^|[^\\\\])(?:\\\\{2})*)"/gm`) let inQuotesRegex = %re(`/"(?:[^"\\\\]|\\\\.)*"/`) // Parse a string. Allows for escaped quotes. // NOTE: not to be confused with `parse`, which takes a raw string and parses @@ -35,7 +35,7 @@ let parseVJsonWithVariable = parseVariableString => { regex(inQuotesRegex) |> map(match => { match - ->Js.String2.replaceByRe(nonEsacapedQuoteRegex, `$1`) // First group of the regex is characters before the quote that should be kept + ->Js.String2.replaceByRe(nonEscapedQuoteRegex, `$1`) // First group of the regex is characters before the quote that should be kept ->Js.String2.replaceByRe(escapedQuoteRegex, `"`) }) |> lexeme