fcl-js: fixed escaping invalid UTF-16 i string literals

git-svn-id: trunk@40191 -
This commit is contained in:
Mattias Gaertner 2018-11-02 22:52:22 +00:00
parent 9b0ff05ee8
commit f0e75cdbbb
2 changed files with 24 additions and 3 deletions

View File

@ -176,6 +176,7 @@ begin
else
exit;
end;
Result:=true;
end;
{$else}
var

View File

@ -534,8 +534,8 @@ Var
I,J,L : Integer;
R: TJSString;
c: WideChar;
begin
//system.writeln('TJSWriter.EscapeString "',S,'"');
I:=1;
J:=1;
R:='';
@ -543,7 +543,8 @@ begin
While I<=L do
begin
c:=S[I];
if (c in [#0..#31,'"','''','/','\']) or (c>=#$ff00) then
if (c in [#0..#31,'"','''','/','\'])
or (c>=#$ff00) or ((c>=#$D800) and (c<=#$DFFF)) then
begin
R:=R+Copy(S,J,I-J);
Case c of
@ -557,7 +558,25 @@ begin
#10 : R:=R+'\n';
#12 : R:=R+'\f';
#13 : R:=R+'\r';
#$ff00..#$ffff: R:=R+'\u'+TJSString(HexStr(ord(c),4));
#$D800..#$DFFF:
begin
if (I<L) then
begin
c:=S[I+1];
if (c>=#$D000) and (c<=#$DFFF) then
begin
inc(I,2); // surrogate, two char codepoint
continue;
end
else
// invalid UTF-16, cannot be encoded as UTF-8 -> encode as hex
R:=R+'\u'+TJSString(HexStr(ord(c),4));
end
else
// invalid UTF-16 at end of string, cannot be encoded as UTF-8 -> encode as hex
R:=R+'\u'+TJSString(HexStr(ord(c),4));
end;
#$FF00..#$FFFF: R:=R+'\u'+TJSString(HexStr(ord(c),4));
end;
J:=I+1;
end;
@ -565,6 +584,7 @@ begin
end;
R:=R+Copy(S,J,I-1);
Result:=R;
//system.writeln('TJSWriter.EscapeString Result="',Result,'"');
end;
procedure TJSWriter.WriteValue(V: TJSValue);