Menu

[r3194]: / trunk / Src / UJavaScriptUtils.pas  Maximize  Restore  History

Download this file

194 lines (173 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Utilities that assist when working with JavaScript.
}
unit UJavaScriptUtils;
interface
type
/// <summary>Container for methods that assist in generating JavaScript code.
/// </summary>
TJavaScript = record
strict private
/// <summary>Converts the given integer into a literal numeric parameter
/// suitable for passing to a JavaScript function.</summary>
class function LiteralParam(const I: Integer): string; overload; static;
/// <summary>Converts the given string into a literal string parameter
/// suitable for passing to a JavaScript function.</summary>
/// <remarks>The string is quoted and characters are escaped as necessary
/// to make the result a valid JavaScript string.</remarks>
class function LiteralParam(const S: string): string; overload; static;
/// <summary>Converts the given floating point value into a literal numeric
/// parameter suitable for passing to a JavaScript function.</summary>
class function LiteralParam(const F: Extended): string; overload; static;
/// <summary>Converts the given boolean value into either "true" or "false"
/// keyword suitable for passing to a JavaScript function.</summary>
class function LiteralParam(const B: Boolean): string; overload; static;
public
/// <summary>Creates and returns a JavaScript function call with a
/// parameter list comprising of literal values.</summary>
/// <param name="FnName">string [in] Name of function.</param>
/// <param name="Params">array of const [in] Dynamic array of literal
/// parameter values to be passed to the function. [] indicates a
/// parameterless function.</param>
/// <returns>string. Required JavaScript function.</returns>
/// <exception>EBug is raised if type of any value in Params has an
/// unsupported type. Valid types are Integer, Boolean, Extended,
/// UnicodeString and WideChar.</exception>
class function LiteralFunc(const FnName: string;
const Params: array of const): string; static;
end;
implementation
uses
// Delphi
SysUtils, Classes,
// Project
UConsts, UExceptions, UStrUtils;
/// <summary>Replaces specified characters in a string with escape characters
/// in C format.</summary>
/// <param name="S">string [in] String to be escaped.</param>
/// <param name="EscapeChars">string [in] Escape characters to replace
/// characters from EscapableChars.</param>
/// <param name="EscapableChars">string [in] Characters to be escaped.</param>
/// <returns>string. String with all relevant characters escaped.</returns>
function CEscapeStr(const S: string; const EscapeChars,
EscapableChars: string): string;
const
cEscChar = '\'; // the C escape character
var
EscCount: Integer; // count of escaped characters in string
Idx: Integer; // loops thru string
PRes: PChar; // points to chars in result string
EscCharPos: Integer; // position of esc chars in EscapeChars & EscapableChars
begin
// Check for empty string and treat specially (empty string crashes main code)
if S = '' then
begin
Result := '';
Exit;
end;
// Count escapable characters in string
EscCount := 0;
for Idx := 1 to Length(S) do
begin
if StrContainsStr(S[Idx], EscapableChars) then
Inc(EscCount);
end;
// Set size of result string and get pointer to it
SetLength(Result, Length(S) + EscCount);
PRes := PChar(Result);
// Replace escapable chars with the escaped version
for Idx := 1 to Length(S) do
begin
EscCharPos := StrPos(S[Idx], EscapableChars);
if EscCharPos > 0 then
begin
PRes^ := cEscChar;
Inc(PRes);
PRes^ := EscapeChars[EscCharPos];
end
else
PRes^ := S[Idx];
Inc(PRes);
end;
// copy last character (not processed in loop)
PRes^ := S[Length(S)];
end;
{ TJavaScript }
class function TJavaScript.LiteralFunc(const FnName: string;
const Params: array of const): string;
var
Idx: Integer; // loops thru all provided parameters
ParamVar: TVarRec; // value of a parameter from array
ParamList: TStringList; // list of literal parameters for passing to function
Param: string; // a parameter in suitable form to pass to function
begin
// We store literal parameters in a string
ParamList := TStringList.Create;
try
for Idx := Low(Params) to High(Params) do
begin
// Handle each parameter according to type
ParamVar := TVarRec(Params[Idx]);
case ParamVar.VType of
vtBoolean:
Param := LiteralParam(ParamVar.VBoolean);
vtInteger:
Param := LiteralParam(ParamVar.VInteger);
vtExtended:
Param := LiteralParam(ParamVar.VExtended^);
vtUnicodeString:
Param := LiteralParam(PWideChar(ParamVar.VUnicodeString));
vtWideChar:
Param := LiteralParam(ParamVar.VWideChar);
else
raise EBug.Create('JSLiteralFunc: Unsupported parameter type');
end;
// Store param in list
ParamList.Add(Param);
end;
// Build function name and parameter list
Result := FnName + '(' + StrJoin(ParamList, ', ') + ')';
finally
FreeAndNil(ParamList);
end;
end;
class function TJavaScript.LiteralParam(const I: Integer): string;
begin
Result := IntToStr(I);
end;
class function TJavaScript.LiteralParam(const S: string): string;
const
cQuote = '"'; // quote to delimit string literal
cEscapableChars = cQuote + '\' + LF + CR + TAB; // characters to be escaped
cEscapeChars = cQuote + '\nrt'; // escape characters
begin
Result :=
cQuote +
CEscapeStr(
StrUnixLineBreaks(S), // convert CRLF to LF
cEscapeChars,
cEscapableChars
) +
cQuote;
end;
class function TJavaScript.LiteralParam(const B: Boolean): string;
begin
if B then
Result := 'true'
else
Result := 'false';
end;
class function TJavaScript.LiteralParam(const F: Extended): string;
begin
Result := FloatToStr(F);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.