iPro: Refactoring. Remove duplicate code. Split TIpHtmlNodeText.BuildWordList.

git-svn-id: trunk@45712 -
This commit is contained in:
juha 2014-06-28 18:41:45 +00:00
parent 9e22dec710
commit c1a4f8f48e

View File

@ -1487,6 +1487,10 @@ type
function GetAnsiText: string; function GetAnsiText: string;
procedure SetAnsiText(const Value: string); procedure SetAnsiText(const Value: string);
procedure SetEscapedText(const Value: string); procedure SetEscapedText(const Value: string);
procedure AddAWord(StartP: PAnsiChar);
function CutAndAddWord(StartP, EndP: PAnsiChar): PAnsiChar;
procedure DoPreformattedWords(N: PAnsiChar);
procedure DoNormalWords(N: PAnsiChar);
procedure BuildWordList; procedure BuildWordList;
protected protected
PropsR : TIpHtmlProps; {reference} PropsR : TIpHtmlProps; {reference}
@ -5542,84 +5546,43 @@ begin
Result := True; Result := True;
end; end;
procedure TrimFormattingPre(const S: string; Target: PAnsiChar); procedure TrimFormatting(const S: string; Target: PAnsiChar; PreFormatted: Boolean = False);
var var
R, W : Integer; R, W : Integer;
procedure CopyChar(ch: AnsiChar);
begin
Target[w] := ch;
Inc(w);
end;
begin begin
r := 1; r := 1;
w := 0; w := 0;
while r <= length(S) do begin while r <= length(S) do begin
case S[r] of case S[r] of
#13 :
begin
Target[w] := LF;
Inc(w);
end;
#10 :
if (w = 0) or (Target[w - 1] <> LF) then begin
Target[w] := LF;
Inc(w);
end;
#0..#8, #11..#12, #14..#31 : #0..#8, #11..#12, #14..#31 :
; ;
#9, #32 : #9 :
begin if PreFormatted then
Target[w] := ' '; CopyChar(' ');
Inc(w); #13 :
end; if PreFormatted then
else CopyChar(LF);
begin
Target[w] := S[r];
Inc(w);
end;
end;
Inc(r);
end;
Target[w] := #0;
end;
procedure TrimFormattingNormal(const S: string; Target: PAnsiChar);
var
R, W : Integer;
begin
r := 1;
w := 0;
while r <= length(S) do begin
case S[r] of
#0..#9, #11..#13, #14..#31 :
;
#10 : #10 :
if w > 1 then begin if PreFormatted then begin
Target[w] := ' '; if (w = 0) or (Target[w-1] <> LF) then
Inc(w); CopyChar(LF);
end
else begin
if w > 1 then
CopyChar(' ');
end; end;
#32 :
begin
Target[w] := ' ';
Inc(w);
end;
else
begin
Target[w] := S[r];
Inc(w);
end;
end;
Inc(r);
end;
Target[w] := #0;
r := 0;
w := 0;
while Target[r] <> #0 do begin
case Target[r] of
' ' : ' ' :
if (w = 0) or (Target[w - 1] <> ' ') then begin if PreFormatted or (w = 0) or (Target[w-1] <> ' ') then
Target[w] := ' '; CopyChar(' ');
Inc(w);
end;
else else
if w <> r then CopyChar(S[r]);
Target[w] := Target[r];
Inc(w);
end; end;
Inc(r); Inc(r);
end; end;
@ -6065,7 +6028,7 @@ begin
if CurToken = IpHtmlTagText then begin if CurToken = IpHtmlTagText then begin
Getmem(B, length(GetTokenString) + 1); Getmem(B, length(GetTokenString) + 1);
try try
TrimFormattingNormal(EscapeToAnsi(GetTokenString), B); TrimFormatting(EscapeToAnsi(GetTokenString), B);
FTitleNode.Title := B; FTitleNode.Title := B;
finally finally
Freemem(B); Freemem(B);
@ -9681,136 +9644,126 @@ begin
BuildWordList; BuildWordList;
end; end;
procedure TIpHtmlNodeText.BuildWordList; procedure TIpHtmlNodeText.AddAWord(StartP: PAnsiChar);
begin
if FFirstW then
Owner.AddWord(StartP, PropsR, Self)
else
Owner.AddWord(StartP, nil, Self);
FFirstW := False;
end;
function TIpHtmlNodeText.CutAndAddWord(StartP, EndP: PAnsiChar): PAnsiChar;
var var
NewEntry : PIpHtmlElement; EndCh: AnsiChar;
l : Integer; begin
B, N, N2 : PAnsiChar; EndCh := EndP^;
First : Boolean; EndP^ := #0;
Ch : AnsiChar; AddAWord(StartP);
EndP^ := EndCh;
Result := EndP;
end;
procedure TIpHtmlNodeText.DoPreformattedWords(N: PAnsiChar);
var
N2: PAnsiChar;
ImplicitLF: Boolean; ImplicitLF: Boolean;
begin begin
First := True;
ImplicitLF := False; ImplicitLF := False;
if PropsR.Preformatted then begin while N^ <> #0 do begin
l := length(EscapedText); case N^ of
if l > 0 then begin CR :
Getmem(B, l + 1); ImplicitLF := True;
try LF :
TrimFormattingPre(EscapedText, B); begin
N := B; EnqueueElement(Owner.HardLF);
while N^ <> #0 do begin Inc(N);
case N^ of ImplicitLF := False;
CR : end;
ImplicitLF := True; else
LF : begin
begin if ImplicitLF then begin
EnqueueElement(Owner.HardLF); EnqueueElement(Owner.HardLF);
Inc(N); Inc(N);
ImplicitLF := False; ImplicitLF := False;
end; end;
else N2 := StrScan(N, CR);
begin if N2 <> nil then
if ImplicitLF then begin N := CutAndAddWord(N, N2)
EnqueueElement(Owner.HardLF); else begin
Inc(N); N2 := StrScan(N, LF);
ImplicitLF := False; if N2 <> nil then
end; N := CutAndAddWord(N, N2)
N2 := StrScan(N, CR); else begin
if N2 <> nil then begin AddAWord(N);
N2^ := #0; N^ := #0;
if First then
Owner.AddWord(N, PropsR, Self)
else
Owner.AddWord(N, nil, Self);
N2^ := CR;
First := False;
N := N2;
end else begin
N2 := StrScan(N, LF);
if N2 <> nil then begin
N2^ := #0;
if First then
Owner.AddWord(N, PropsR, Self)
else
Owner.AddWord(N, nil, Self);
N2^ := LF;
First := False;
N := N2;
end else begin
if First then
Owner.AddWord(N, PropsR, Self)
else
Owner.AddWord(N, nil, Self);
First := False;
N^ := #0;
end;
end;
end;
end; end;
end; end;
finally
FreeMem(B);
end; end;
end; end;
end else begin end;
l := length(EscapedText); end;
if l > 0 then begin
Getmem(B, l + 1); procedure TIpHtmlNodeText.DoNormalWords(N: PAnsiChar);
try var
TrimFormattingNormal(EscapedText, B); NewEntry : PIpHtmlElement;
N := B; N2: PAnsiChar;
while N^ <> #0 do begin begin
case N^ of while N^ <> #0 do begin
LF : case N^ of
begin LF :
EnqueueElement(Owner.HardLF); begin
Inc(N); EnqueueElement(Owner.HardLF);
end; Inc(N);
' ' :
begin
if not ElementQueueIsEmpty then begin
NewEntry := Owner.NewElement(etWord, Self);
NewEntry.AnsiWord := ' ';
NewEntry.IsBlank := 1;
if First then
NewEntry.Props := PropsR
else
NewEntry.Props := nil;
EnqueueElement(NewEntry);
First := False;
end;
Inc(N);
end;
else
begin
N2 := N;
while not (N2^ in [#0, ' ', LF]) do
Inc(N2);
if N2^ <> #0 then begin
Ch := N2^;
N2^ := #0;
if First then
Owner.AddWord(N, PropsR, Self)
else
Owner.AddWord(N, nil, Self);
N2^ := Ch;
First := False;
N := N2;
end else begin
if First then
Owner.AddWord(N, PropsR, Self)
else
Owner.AddWord(N, nil, Self);
First := False;
N^ := #0;
end;
end;
end;
end;
finally
FreeMem(B);
end; end;
' ' :
begin
if not ElementQueueIsEmpty then begin
NewEntry := Owner.NewElement(etWord, Self);
NewEntry.AnsiWord := ' ';
NewEntry.IsBlank := 1;
if FFirstW then
NewEntry.Props := PropsR
else
NewEntry.Props := nil;
EnqueueElement(NewEntry);
FFirstW := False;
end;
Inc(N);
end;
else
begin
N2 := N;
while not (N2^ in [#0, ' ', LF]) do
Inc(N2);
if N2^ <> #0 then
N := CutAndAddWord(N, N2)
else begin
AddAWord(N);
N^ := #0;
end;
end;
end;
end;
end;
procedure TIpHtmlNodeText.BuildWordList;
var
l : Integer;
B : PAnsiChar;
begin
FFirstW := True;
l := length(EscapedText);
if l > 0 then begin
Getmem(B, l + 1);
try
TrimFormatting(EscapedText, B, PropsR.Preformatted);
if PropsR.Preformatted then
DoPreformattedWords(B)
else
DoNormalWords(B);
finally
FreeMem(B);
end; end;
end; end;
end; end;
@ -14836,7 +14789,7 @@ begin
S := TIpHtmlNodeText(FChildren[0]).EscapedText; S := TIpHtmlNodeText(FChildren[0]).EscapedText;
Getmem(B, length(S) + 1); Getmem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
S := Trim(B); S := Trim(B);
if Self.Multiple then begin if Self.Multiple then begin
j := TListBox(FControl).Items.Add(S); j := TListBox(FControl).Items.Add(S);
@ -14864,7 +14817,7 @@ begin
S := TIpHtmlNodeText(FChildren[0]).EscapedText; S := TIpHtmlNodeText(FChildren[0]).EscapedText;
GetMem(B, length(S) + 1); GetMem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
S := Trim(B); S := Trim(B);
if Self.Multiple then begin if Self.Multiple then begin
k := TListBox(FControl).Items.Add(S); k := TListBox(FControl).Items.Add(S);
@ -14911,7 +14864,7 @@ begin
S := TIpHtmlNodeText(FChildren[0]).EscapedText; S := TIpHtmlNodeText(FChildren[0]).EscapedText;
GetMem(B, length(S) + 1); GetMem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
if Self.Multiple then begin if Self.Multiple then begin
j := TListBox(FControl).Items.Add(Trim(B)); j := TListBox(FControl).Items.Add(Trim(B));
TListBox(FControl).Selected[j] := Selected; TListBox(FControl).Selected[j] := Selected;
@ -14936,7 +14889,7 @@ begin
S := TIpHtmlNodeText(FChildren[0]).EscapedText; S := TIpHtmlNodeText(FChildren[0]).EscapedText;
GetMem(B, length(S) + 1); GetMem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
if Self.Multiple then begin if Self.Multiple then begin
k := TListBox(FControl).Items.Add(Trim(B)); k := TListBox(FControl).Items.Add(Trim(B));
TListBox(FControl).Selected[k] := Selected; TListBox(FControl).Selected[k] := Selected;
@ -15046,7 +14999,7 @@ begin
S := TIpHtmlNodeText(FChildren[i]).EscapedText; S := TIpHtmlNodeText(FChildren[i]).EscapedText;
Getmem(B, length(S) + 1); Getmem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
TMemo(FControl).Lines.Add(B); TMemo(FControl).Lines.Add(B);
finally finally
FreeMem(B); FreeMem(B);
@ -15067,7 +15020,7 @@ begin
S := TIpHtmlNodeText(FChildren[i]).EscapedText; S := TIpHtmlNodeText(FChildren[i]).EscapedText;
GetMem(B, length(S) + 1); GetMem(B, length(S) + 1);
try try
TrimFormattingNormal(S, B); TrimFormatting(S, B);
TMemo(FControl).Lines.Add(B); TMemo(FControl).Lines.Add(B);
finally finally
Freemem(B); Freemem(B);