1

Topic: Converting HTML of the codes of type "&" in characters

If in Delphi something ready or most to invent a wheel?
Can at InDy is?

2

Re: Converting HTML of the codes of type "&" in characters

X11;
Such bicycles the sea .

function MakeSafeHTMLText (TheText: string): string;
var
Idx: Integer;//loops thru the given text
begin
Result: = ";
for Idx: = 1 to Length (TheText) do
case TheText [Idx] of
' <'://opens tags: replace with special char reference
Result: = Result + ' < ';
'> '://closes tags: replace with special char reference
Result: = Result + ' > ';
' AND '://begins char references: replace with special char reference
Result: = Result + ' & ';
' "'://quotes (can be a problem in quoted attributes)
Result: = Result + ' " ';
#0..#31, #127..#255://control and special chars: replace with encoding
Result: = Result + ' &# ' + SysUtils. IntToStr (Ord (TheText [Idx])) + '; ';
else//compatible text: pass thru
Result: = Result + TheText [Idx];
end;
end;

3

Re: Converting HTML of the codes of type "&" in characters

DarkMaster wrote:

X11;
Such bicycles the sea .

function MakeSafeHTMLText (TheText: string): string;
var
Idx: Integer;//loops thru the given text
begin
Result: = ";
for Idx: = 1 to Length (TheText) do
case TheText [Idx] of
' <'://opens tags: replace with special char reference
Result: = Result + ' < ';
'> '://closes tags: replace with special char reference
Result: = Result + ' > ';
' AND '://begins char references: replace with special char reference
Result: = Result + ' & ';
' "'://quotes (can be a problem in quoted attributes)
Result: = Result + ' " ';
#0..#31, #127..#255://control and special chars: replace with encoding
Result: = Result + ' &# ' + SysUtils. IntToStr (Ord (TheText [Idx])) + '; ';
else//compatible text: pass thru
Result: = Result + TheText [Idx];
end;
end;

At the HARDWARE the question was - how to make on the contrary)))

4

Re: Converting HTML of the codes of type "&" in characters

5

Re: Converting HTML of the codes of type "&" in characters

defecator wrote:

it is passed...
At the HARDWARE the question was - how to make on the contrary)))

Means it is necessary to turn or read from below upwards smile))

6

Re: Converting HTML of the codes of type "&" in characters

HTTPApp. HTMLEncode

7

Re: Converting HTML of the codes of type "&" in characters

wadman wrote:

HTMLEncode

Then Decode

8

Re: Converting HTML of the codes of type "&" in characters

Not, it is not necessary.
It swears on a mnemocode of a double left quote:
[img=https://image.prntscr.com/image/vNR906uaSS_5mBAvfY7qKA.png]

Web. HTTPApp. HTMLDecode wrote:

Project raised exception class EConvertError with message ' Invalid HTML encoded character (&l) at position 123 '.

9

Re: Converting HTML of the codes of type "&" in characters

Here still found. But too produces exception in case of the wrong code. Too it is inconvenient.
[spoiler function HTMLDecode (const AStr: String): String;]

function HTMLDecode (const AStr: String): String;
resourcestring
sInvalidHTMLEncodedChar = ' Invalid HTML encoded character (%s) at position %d ';
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength (Result, Length (AStr));
Sp: = PChar (AStr);
Rp: = PChar (Result);
Cp: = Sp;
try
while Sp ^ <> #0 do
begin
case Sp ^ of
' AND ': begin
Cp: = Sp;
Inc (Sp);
case Sp ^ of
' a ': if AnsiStrPos (Sp, ' amp; ') = Sp then {do not localize}
begin
Inc (Sp, 3);
Rp ^: = ' AND ';
end;
' r ';
' l ';
' g ': if (AnsiStrPos (Sp, ' lt; ') = Sp) or (AnsiStrPos (Sp, ' gt; ') = Sp) then {do not localize}
begin
Cp: = Sp;
Inc (Sp, 2);
while (Sp ^ <> '; ') and (Sp ^ <> #0) do
Inc (Sp);
if Cp ^ = ' l ' then
Rp ^: = ' <'
else
Rp ^: = '> ';
end;
' q ': if AnsiStrPos (Sp, ' quot; ') = Sp then {do not localize}
begin
Inc (Sp, 4);
Rp ^: = ' "';
end;
' # ': begin
Tp: = Sp;
Inc (Tp);
while (Sp ^ <> '; ') and (Sp ^ <> #0) do
Inc (Sp);
SetString (S, Tp, Sp - Tp);
Val (S, I, Code);
if I> = $10000 then
begin
//Decode surrogate pair
Rp ^: = Char (((I - $10000) div $400) + $d800);
Inc (Rp);
Rp ^: = Char (((I - $10000) and $3ff) + $dc00);
end
else
Rp ^: = Chr ((I));
end;
else
raise EConvertError. CreateFmt (sInvalidHTMLEncodedChar;
[Cp ^ + Sp ^, Cp - PChar (AStr)])
end;
end
else
Rp ^: = Sp ^;
end;
Inc (Rp);
Inc (Sp);
end;
except
on E:EConvertError do
raise EConvertError. CreateFmt (sInvalidHTMLEncodedChar;
[Cp ^ + Sp ^, Cp - PChar (AStr)])
end;
SetLength (Result, Rp - PChar (Result));
end;

[/spoiler]

10

Re: Converting HTML of the codes of type "&" in characters

XML-functions in Jedi Code Library

11

Re: Converting HTML of the codes of type "&" in characters

Arioch wrote:

Jedi Code Library

It is not installed and will not be

12

Re: Converting HTML of the codes of type "&" in characters

X11;
Well you can still THtmlView download :-)
But that , SGML entities - a piece slippery where to take  and  I do not know their list.
At HTML the version was much....
And still is XHTML (in XML it is possible own entities to register)
In general to solve the task "on " it is simple - StringReplace and rushed.
And here to solve correctly... He-he-he....

13

Re: Converting HTML of the codes of type "&" in characters

While left here it