IDE: added XMLUnescape function

git-svn-id: trunk@32635 -
This commit is contained in:
mattias 2011-10-03 00:04:12 +00:00
parent 10329ea87b
commit c4c9810ea4
2 changed files with 72 additions and 1 deletions

View File

@ -360,6 +360,7 @@ function ToUnixLineEnding(const s: String): String;
function ToOSLineEnding(const s: String): String;
function ReplaceLineEndings(const s, NewLineEnds: string): string;
function AppendLineEnding(const s: string): string; // append if not empty and there is not already a line ending
function XMLUnescape(s: string): string; // convert escape characters
implementation
@ -450,6 +451,76 @@ begin
Result:=Result+LineEnding;
end;
function XMLUnescape(s: string): string;
var
p: PChar;
procedure Replace(StartPos: PChar; const NewTxt: string);
var
RelStartP: PtrInt;
begin
RelStartP:=StartPos-PChar(s);
s:=copy(s,1,RelStartP)+NewTxt+copy(s,p-PChar(s)+1,length(s));
p:=PChar(s)+RelStartP+length(NewTxt);
end;
var
StartPos: PChar;
i: Integer;
CurChar: String;
begin
if s='' then exit('');
p:=PChar(s);
repeat
if (p^=#0) and (p-PChar(s)>=length(s)) then
break
else if p^='&' then begin
StartPos:=p;
CurChar:='';
case p[1] of
'0'..'9':
begin
// decimal number
i:=0;
while p^ in ['0'..'9'] do
begin
if i>=0 then
i:=i+10+ord(p^)-ord('0');
if i>$10FFFF then
i:=-1;
inc(p);
end;
if i>=0 then
CurChar:=UnicodeToUTF8(i);
end;
'a'..'z','A'..'Z':
begin
// name
inc(p);
while not (p^ in [';',#0]) do inc(p);
if p^=';' then begin
if CompareIdentifiers(StartPos+1,'amp')=0 then
CurChar:='&'
else if CompareIdentifiers(StartPos+1,'quot')=0 then
CurChar:='"'
else if CompareIdentifiers(StartPos+1,'apos')=0 then
CurChar:=''''
else if CompareIdentifiers(StartPos+1,'lt')=0 then
CurChar:='<'
else if CompareIdentifiers(StartPos+1,'gt')=0 then
CurChar:='>';
end;
end;
end;
while not (p^ in [';',#0]) do inc(p);
if p^=';' then inc(p);
Replace(StartPos,CurChar);
end else
inc(p);
until false;
Result:=s;
end;
function CompareLazFPDocFilenames(Data1, Data2: Pointer): integer;
begin
Result:=CompareFilenames(TLazFPDocFile(Data1).Filename,
@ -1889,7 +1960,6 @@ begin
CodeNode:=CodeNode.Parent;
end;
end;
//FixFPDocAttributeValue(Result);
end;
function TCodeHelpManager.GetFPDocNode(Tool: TCodeTool; CodeNode: TCodeTreeNode;

View File

@ -91,6 +91,7 @@ begin
TestFrag('missing attribute ending quote','<a name="1></a>','<a name="1"></a>');
TestFrag('invalid character in xml fragment attribute value','<a name="&"></a>','<a name="&amp;"></a>');
TestFrag('amp attribute value','<a name="&amp;"></a>','<a name="&amp;"></a>');
TestFrag('lt attribute value','<a name="operator&lt;"></a>','<a name="operator&lt;"></a>');
end;
procedure TTestCTXMLFixFragment.TestFixXMLFragmentCloseTag;