8000 The tokenizer emits supported entities literally · Issue #111 · digao-dalpiaz/DzHTMLText · GitHub
[go: up one dir, main page]

Skip to content

The tokenizer emits supported entities literally #111

Backlog

Description

@FranmoFR

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 &lt; B &amp; C &gt; D. The helper UnescapeHTMLToText(EscapeTextToHTML(...)) works by itself.

Cause and fix

The tokenizer treats ; as punctuation before decoding each word. It passes &amp 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 &amp;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, '&amp;') then
+   begin
+    Inc(I, 5);
+    Continue;
+   end;
+   if StartsEntity(A, I, '&lt;') or StartsEntity(A, I, '&gt;') 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0