Skip to content

Commit 81154ab

Browse files
authored
Revise __WD_EscapeString to further encode strings (#469)
1 parent 51934ec commit 81154ab

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
Go to [legend](#legend---types-of-changes) for further information about the types of changes.
1111

12+
## [Unreleased]
13+
14+
### Project
15+
16+
- Improve string encoding by using existing function from JSON UDF
17+
1218
## [1.0.0] - 2023-04-28
1319

1420
### Added

wd_core.au3

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Global Const $_WD_JSON_Shadow = "[value][" & $_WD_SHADOW_ID & "]"
8888
Global Const $_WD_JSON_Error = "[value][error]"
8989
Global Const $_WD_JSON_Message = "[value][message]"
9090

91+
Global Const $JSON_MLREFORMAT = 1048576 ; Addition to constants from json.au3
92+
9193
Global Enum _
9294
$_WD_DEBUG_None = 0, _ ; No logging
9395
$_WD_DEBUG_Error, _ ; logging in case of Error
@@ -197,7 +199,6 @@ Global $_WD_HTTPRESULT = 0 ; Result of last WinHTTP request
197199
Global $_WD_HTTPRESPONSE = '' ; Response of last WinHTTP request
198200
Global $_WD_SESSION_DETAILS = "" ; Response from _WD_CreateSession
199201
Global $_WD_BFORMAT = $SB_UTF8 ; Binary format
200-
Global $_WD_ESCAPE_CHARS = '\\"' ; Characters to escape
201202
Global $_WD_DRIVER_CLOSE = True ; Close prior driver instances before launching new one
202203
Global $_WD_DRIVER_DETECT = True ; Don't launch new driver instance if one already exists
203204
Global $_WD_RESPONSE_TRIM = -1 ; Trim response string to given value for debug output
@@ -838,7 +839,7 @@ Func _WD_ExecuteScript($sSession, $sScript, $sArguments = Default, $bAsync = Def
838839
If IsBool($vSubNode) Then $vSubNode = ($vSubNode) ? $_WD_JSON_Value : "" ; True = the JSON value node is returned , False = entire JSON response is returned
839840

840841
If IsString($vSubNode) Then
841-
$sScript = __WD_EscapeString($sScript)
842+
$sScript = __WD_EscapeString($sScript, $JSON_MLREFORMAT)
842843

843844
$sData = '{"script":"' & $sScript & '", "args":[' & $sArguments & ']}'
844845
$sCmd = ($bAsync) ? 'async' : 'sync'
@@ -1675,20 +1676,26 @@ EndFunc ;==>__WD_CloseDriver
16751676
; #INTERNAL_USE_ONLY# ===========================================================================================================
16761677
; Name ..........: __WD_EscapeString
16771678
; Description ...: Escapes designated characters in string.
1678-
; Syntax ........: __WD_EscapeString($sData)
1679-
; Parameters ....: $sData - the string to be escaped
1680-
; Return values..: Escaped string.
1679+
; Syntax ........: __WD_EscapeString($sData[, $iOption = 0])
1680+
; Parameters ....: $sData - the string to be escaped
1681+
; $iOption - [optional] Any combination of $JSON_* constants. Default is 0.
16811682
; Author ........: Danp2
16821683
; Modified ......:
1683-
; Remarks .......:
1684+
; Remarks .......: $JSON_MLREFORMAT will strip tabs and CR/LFs from a multiline string.
1685+
; See $JSON_* constants in json.au3 for other $iOption possibilities.
16841686
; Related .......:
16851687
; Link ..........:
16861688
; Example .......: No
16871689
; ===============================================================================================================================
1688-
Func __WD_EscapeString($sData)
1689-
Local $sRegEx = "([" & $_WD_ESCAPE_CHARS & "])"
1690-
Local $sEscaped = StringRegExpReplace($sData, $sRegEx, "\\$1")
1691-
Return SetError($_WD_ERROR_Success, 0, $sEscaped)
1690+
Func __WD_EscapeString($sData, $iOption = 0)
1691+
If BitAND($iOption, $JSON_MLREFORMAT) Then
1692+
$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs
1693+
$iOption = BitXOR($iOption, $JSON_MLREFORMAT) ; Flip bit off
1694+
Endif
1695+
1696+
$sData = Json_StringEncode($sData, $iOption) ; Escape JSON Strings
1697+
1698+
Return SetError($_WD_ERROR_Success, 0, $sData)
16921699
EndFunc ;==>__WD_EscapeString
16931700

16941701
; #INTERNAL_USE_ONLY# ===========================================================================================================

wd_helper.au3

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,15 +1956,8 @@ Func _WD_SelectFiles($sSession, $sStrategy, $sSelector, $sFilename)
19561956

19571957
If $iErr = $_WD_ERROR_Success Then
19581958
If $sFilename <> "" Then
1959-
$sSavedEscape = $_WD_ESCAPE_CHARS
1960-
; Convert file string into proper format
1961-
$sFilename = StringReplace(__WD_EscapeString($sFilename), @LF, "\n")
1962-
; Prevent further string escaping
1963-
$_WD_ESCAPE_CHARS = ""
19641959
_WD_ElementAction($sSession, $sElement, 'value', $sFilename)
19651960
$iErr = @error
1966-
; Restore setting
1967-
$_WD_ESCAPE_CHARS = $sSavedEscape
19681961
Else
19691962
_WD_ElementAction($sSession, $sElement, 'clear')
19701963
$iErr = @error

0 commit comments

Comments
 (0)