|
@@ -247,6 +247,7 @@ Type
|
|
{$ifdef FPC_HAS_CPSTRING}
|
|
{$ifdef FPC_HAS_CPSTRING}
|
|
Function UTF16ToUTF8(const S: UnicodeString): string;
|
|
Function UTF16ToUTF8(const S: UnicodeString): string;
|
|
{$endif}
|
|
{$endif}
|
|
|
|
+Function QuoteJSString(const S: TJSString; Quote: TJSChar = #0): TJSString;
|
|
|
|
|
|
implementation
|
|
implementation
|
|
|
|
|
|
@@ -271,6 +272,36 @@ begin
|
|
// conversion magic
|
|
// conversion magic
|
|
SetCodePage(RawByteString(Result), CP_ACP, False);
|
|
SetCodePage(RawByteString(Result), CP_ACP, False);
|
|
end;
|
|
end;
|
|
|
|
+
|
|
|
|
+function QuoteJSString(const S: TJSString; Quote: TJSChar): TJSString;
|
|
|
|
+var
|
|
|
|
+ i, j, Count: Integer;
|
|
|
|
+begin
|
|
|
|
+ if Quote=#0 then
|
|
|
|
+ begin
|
|
|
|
+ if Pos('"',S)>0 then
|
|
|
|
+ Quote:=''''
|
|
|
|
+ else
|
|
|
|
+ Quote:='"';
|
|
|
|
+ end;
|
|
|
|
+ Result := '' + Quote;
|
|
|
|
+ Count := length(S);
|
|
|
|
+ i := 0;
|
|
|
|
+ j := 0;
|
|
|
|
+ while i < Count do
|
|
|
|
+ begin
|
|
|
|
+ inc(i);
|
|
|
|
+ if S[i] = Quote then
|
|
|
|
+ begin
|
|
|
|
+ Result := Result + copy(S, 1 + j, i - j) + Quote;
|
|
|
|
+ j := i;
|
|
|
|
+ end;
|
|
|
|
+ end;
|
|
|
|
+ if i <> j then
|
|
|
|
+ Result := Result + copy(S, 1 + j, i - j);
|
|
|
|
+ Result := Result + Quote;
|
|
|
|
+end;
|
|
|
|
+
|
|
{$endif}
|
|
{$endif}
|
|
|
|
|
|
{ TBufferWriter }
|
|
{ TBufferWriter }
|
|
@@ -1023,14 +1054,11 @@ end;
|
|
|
|
|
|
|
|
|
|
procedure TJSWriter.WriteObjectLiteral(El: TJSObjectLiteral);
|
|
procedure TJSWriter.WriteObjectLiteral(El: TJSObjectLiteral);
|
|
-
|
|
|
|
-
|
|
|
|
Var
|
|
Var
|
|
i,C : Integer;
|
|
i,C : Integer;
|
|
QE,WC : Boolean;
|
|
QE,WC : Boolean;
|
|
S : TJSString;
|
|
S : TJSString;
|
|
Prop: TJSObjectLiteralElement;
|
|
Prop: TJSObjectLiteralElement;
|
|
-
|
|
|
|
begin
|
|
begin
|
|
C:=El.Elements.Count-1;
|
|
C:=El.Elements.Count-1;
|
|
QE:=(woQuoteElementNames in Options);
|
|
QE:=(woQuoteElementNames in Options);
|
|
@@ -1053,7 +1081,14 @@ begin
|
|
Writer.CurElement:=Prop.Expr;
|
|
Writer.CurElement:=Prop.Expr;
|
|
S:=Prop.Name;
|
|
S:=Prop.Name;
|
|
if QE or not IsValidJSIdentifier(S) then
|
|
if QE or not IsValidJSIdentifier(S) then
|
|
- S:='"'+S+'"';
|
|
|
|
|
|
+ begin
|
|
|
|
+ if (length(S)>1)
|
|
|
|
+ and (((S[1]='"') and (S[length(S)]='"'))
|
|
|
|
+ or ((S[1]='''') and (S[length(S)]=''''))) then
|
|
|
|
+ // already quoted
|
|
|
|
+ else
|
|
|
|
+ S:=QuoteJSString(s);
|
|
|
|
+ end;
|
|
Write(S+': ');
|
|
Write(S+': ');
|
|
Indent;
|
|
Indent;
|
|
FSkipRoundBrackets:=true;
|
|
FSkipRoundBrackets:=true;
|