是否有用于转义HTML的Delphi标准函数?

时间:2010-06-03 16:55:27

标签: html delphi escaping

我有一个报告应该采用网格控件并生成HTML输出。网格中的一列可以显示多个值中的任何一个,或<Any>。当这个输出到HTML时,它当然是空白的。

我可能会编写一些例程来使用StringReplace将其转换为&lt;Any&gt;,因此它会正确显示这个特殊情况,但我认为RTL中可能有一个已经过测试并且正确的情况。谁知道我能在哪里找到它?

9 个答案:

答案 0 :(得分:18)

我99%确定RTL中不存在这样的功能(从Delphi 2009开始)。当然 - 然而 - 编写这样的函数是微不足道的。

<强> 更新

您正在寻找HTTPUtil.HTMLEscape:

function HTMLEscape(const Str: string): string;

我不敢在这里发布代码(可能是版权侵犯),但例程非常简单。它将“&lt;”,“&gt;”,“&amp;”和“”“编码为&lt;&gt;&amp;&quot;。它还取代了字符#92,#160 ..#255到十进制代码,例如&#92;

如果文件是UTF-8,则后一步是不必要的,也是不合逻辑的,因为较高的特殊字符(如∮)保持不变,而较低的特殊字符(如×)则被编码。

更新2

为了回应Stijn Sanders的回答,我做了一个简单的性能测试。

program Project1;

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils;

var
  t1, t2, t3, t4: Int64;
  i: Integer;
  str: string;
const
  N = 100000;


function HTMLEncode(const Data: string): string;
var
  i: Integer;
begin

  result := '';
  for i := 1 to length(Data) do
    case Data[i] of
      '<': result := result + '&lt;';
      '>': result := result + '&gt;';
      '&': result := result + '&amp;';
      '"': result := result + '&quot;';
    else
      result := result + Data[i];
    end;

end;

function HTMLEncode2(Data: string):string;
begin
  Result:=
    StringReplace(
    StringReplace(
    StringReplace(
    StringReplace(
      Data,
      '&','&amp;',[rfReplaceAll]),
      '<','&lt;',[rfReplaceAll]),
      '>','&gt;',[rfReplaceAll]),
      '"','&quot;',[rfReplaceAll]);
end;

begin

  QueryPerformanceCounter(t1);
  for i := 0 to N - 1 do
    str := HTMLEncode('Testing. Is 3*4<3+4? Do you like "A & B"');
  QueryPerformanceCounter(t2);

  QueryPerformanceCounter(t3);
  for i := 0 to N - 1 do
    str := HTMLEncode2('Testing. Is 3*4<3+4? Do you like "A & B"');
  QueryPerformanceCounter(t4);

  Writeln(IntToStr(t2-t1));
  Writeln(IntToStr(t4-t3));

  Readln;


end.

输出

532031
801969

答案 1 :(得分:13)

这似乎是一场小型比赛:)这是另外一个实现:

function HTMLEncode3(const Data: string): string;
var
  iPos, i: Integer;

  procedure Encode(const AStr: String);
  begin
    Move(AStr[1], result[iPos], Length(AStr) * SizeOf(Char));
    Inc(iPos, Length(AStr));
  end;

begin
  SetLength(result, Length(Data) * 6);
  iPos := 1;
  for i := 1 to length(Data) do
    case Data[i] of
      '<': Encode('&lt;');
      '>': Encode('&gt;');
      '&': Encode('&amp;');
      '"': Encode('&quot;');
    else
      result[iPos] := Data[i];
      Inc(iPos);
    end;
  SetLength(result, iPos - 1);
end;

更新1:最初更新提供了错误的代码。

更新2:时间:

HTMLEncode :   2286508597
HTMLEncode2:   3577001647
HTMLEncode3:    361039770

答案 2 :(得分:3)

我通常只使用这段代码:

function HTMLEncode(Data:string):string;
begin
  Result:=
    StringReplace(
    StringReplace(
    StringReplace(
    StringReplace(
    StringReplace(
      Data,
      '&','&amp;',[rfReplaceAll]),
      '<','&lt;',[rfReplaceAll]),
      '>','&gt;',[rfReplaceAll]),
      '"','&quot;',[rfReplaceAll]),
      #13#10,'<br />'#13#10,[rfReplaceAll]);
end;

(版权所有?open source

答案 3 :(得分:2)

单元HTTPApp有一个名为HTMLEncode的函数。它还有其他与HTML / HTTP相关的功能。

答案 4 :(得分:1)

我不知道它引入了哪个delphi版本,但是System.NetEncoding单元有:

TNetEncoding.HTML.Encode
TNetEncoding.HTML.Decode

功能。阅读Why do I get error: RPC failed; result=52, HTTP code = 0 fatal: The remote end hung up unexpectedly when pushing to github?。你不再需要外部库了。

答案 5 :(得分:1)

对于较旧的delphi版本,从Soap.HTTPUtil单元或仅HTTPUtil单元中,您可以使用

function HTMLEscape(const Str: string): string;
var
  i: Integer;
begin
  Result := '';
  for i := Low(Str) to High(Str) do
  begin
    case Str[i]  of
    '<' : Result := Result + '&lt;';    { Do not localize }
    '>' : Result := Result + '&gt;';    { Do not localize }
    '&' : Result := Result + '&amp;';   { Do not localize }
    '"' : Result := Result + '&quot;';  { Do not localize }
{$IFNDEF UNICODE}
    #92, Char(160) .. #255 : Result := Result + '&#' + IntToStr(Ord(Str[ i ])) +';';  { Do not localize }
{$ELSE}
    // NOTE: Not very efficient
    #$0080..#$FFFF : Result := Result + '&#' + IntToStr(Ord(Str[ i ])) +';'; { Do not localize }
{$ENDIF}
    else
      Result := Result + Str[i];
    end;
  end;
end;

答案 6 :(得分:0)

如何替换特殊字符:

    function HtmlWeg(sS: String): String;
var
  ix,cc: Integer;
  sC, sR: String;
begin
  result := sS;
  ix := pos('\u00',sS);

  while ix >0 do
  begin
    sc := copy(sS,ix+4,2) ;
    cc := StrtoIntdef('$' +sC,32);
    sR := '' + chr(cc);
    sS := Stringreplace(sS, '\u00'+sC,sR,[rfreplaceall]) ;
    ix := pos('\u00',sS);
  end;
  result := sS;
end;

答案 7 :(得分:0)

我的函数将for循环与字符串的最小重新分配结合起来:

function HtmlEncode(const Value: string): string;
var
  i: Integer;

begin
  Result := Value;
  i := 1;

  while i <= Length(Result) do
  begin
    if Result[i] = '<' then
    begin
      Result[i] := '&';
      Insert('lt;', Result, i + 1);
      Inc(i, 4);
    end
    else if Result[i] = '>' then
    begin
      Result[i] := '&';
      Insert('gt;', Result, i + 1);
      Inc(i, 4);
    end
    else if Result[i] = '"' then
    begin
      Result[i] := '&';
      Insert('quot;', Result, i + 1);
      Inc(i, 6);
    end
    else if Result[i] = '&' then
    begin
      Insert('amp;', Result, i + 1);
      Inc(i, 5);
    end
    else
      Inc(i);
  end;
end;

答案 8 :(得分:0)

在delphi中,您具有功能

THTMLEncoding.HTML.Encode