Repository navigation
Description
Environment
DzHTMLText 6.13, Delphi 12 Athens, Windows. VCL runtime regression tests passed on Win32 and Win64. The FMX Win32 package compiled.
Reproduction
Set Text to the result of TDzHTMLText.EscapeTextToHTML('A < B & C > D'). The visible text and generated PlainText should read A < B & C > D.
Observed behavior
The tokenizer emits A < B & C > D. The helper UnescapeHTMLToText(EscapeTextToHTML(...)) works by itself.
Cause and fix
The tokenizer treats ; as punctuation before decoding each word. It passes & and ; to the decoder separately. The patch keeps the three supported entities together during word scanning. Decoding still runs once. Unknown entities and ordinary semicolons retain their existing treatment.
Verification
The regression covers the escaped round trip, unknown entities, ordinary punctuation and double-escaped input such as &lt;.
Base revision bfdda0f. The implementation uses Delphi 12 inline variables. Older Delphi versions and Lazarus require compatibility adjustments and were outside this verification.
Patch
diff --git a/Source/Vcl.DHCommon.pas b/Source/Vcl.DHCommon.pas
index ea4a1f4..4c8f383 100644
--- a/Source/Vcl.DHCommon.pas
+++ b/Source/Vcl.DHCommon.pas
@@ -41,7 +41,7 @@ type
class function IsCJKChar(const C: Char): Boolean; inline;
public
class function IsPunctuationChar(const C: Char): Boolean;
- class function FindNextWordBreakChar(const A: string; From: Integer): Integer; inline;
+ class function FindNextWordBreakChar(const A: string; From: Integer): Integer;
end;
function SplitStr(const Str, Separator: string; var Left: string; var Right: string): Boolean;
@@ -122,20 +122,37 @@ end;
{$ENDREGION}
{$REGION 'TDHCharUtils'}
+
+// Rozpoznaje pełną encję bez tworzenia kopii badanego fragmentu.
+function StartsEntity(const A: string; Position: Integer; const Entity: string): Boolean;
+begin
+ Result := (Length(A) - Position + 1 >= Length(Entity)) and CompareMem(PChar(A) + Position - 1, PChar(Entity), Length(Entity) * SizeOf(Char));
+end;
+
class function TDHCharUtils.FindNextWordBreakChar(const A: string; From: Integer): Integer;
-var
- I: Integer;
- C: Char;
begin
- for I := From to A.Length do
+ // Średnik zamykający rozpoznaną encję pozostaje częścią tokenu tekstowego.
+ var I := From;
+ while I <= Length(A) do
+ begin
+ var C := A[I];
+ if C = '&' then
begin
- C := A[I];
-
- if CharInSet(C, [STR_SPACE,'<','>','/','\',#13,#10]) or IsPunctuationChar(C) or IsCJKChar(C) then
- Exit(I);
+ if StartsEntity(A, I, '&') then
+ begin
+ Inc(I, 5);
+ Continue;
+ end;
+ if StartsEntity(A, I, '<') or StartsEntity(A, I, '>') then
+ begin
+ Inc(I, 4);
+ Continue;
+ end;
end;
-
- Result := 0;
+ if CharInSet(C, [STR_SPACE,'<','>','/','\',#13,#10]) or IsPunctuationChar(C) or IsCJKChar(C) then Exit(I);
+ Inc(I);
+ end;
+ Result := 0;
end;
class function TDHCharUtils.IsPunctuationChar(const C: Char): Boolean;
Activity