Browse Source

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

git-svn-id: trunk@40191 -
Mattias Gaertner 6 years ago
parent
commit
f0e75cdbbb
2 changed files with 24 additions and 3 deletions
  1. 1 0
      packages/fcl-js/src/jsbase.pp
  2. 23 3
      packages/fcl-js/src/jswriter.pp

+ 1 - 0
packages/fcl-js/src/jsbase.pp

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

+ 23 - 3
packages/fcl-js/src/jswriter.pp

@@ -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);