mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-09 07:16:49 +02:00
laz2_dom: added XmlValueToStr
git-svn-id: trunk@36348 -
This commit is contained in:
parent
861c143774
commit
d5b401577b
@ -854,7 +854,8 @@ const
|
|||||||
stduri_xml: DOMString = 'http://www.w3.org/XML/1998/namespace';
|
stduri_xml: DOMString = 'http://www.w3.org/XML/1998/namespace';
|
||||||
stduri_xmlns: DOMString = 'http://www.w3.org/2000/xmlns/';
|
stduri_xmlns: DOMString = 'http://www.w3.org/2000/xmlns/';
|
||||||
|
|
||||||
function StrToXMLValue(const s: string): string;
|
function StrToXMLValue(const s: string): string; // removes #0
|
||||||
|
function XMLValueToStr(const s: string): string; // reverse of StrToXMLValue (except for invalid #0)
|
||||||
function EncodeLesserAndGreaterThan(const s: string): string;
|
function EncodeLesserAndGreaterThan(const s: string): string;
|
||||||
|
|
||||||
// =======================================================
|
// =======================================================
|
||||||
@ -924,6 +925,78 @@ begin
|
|||||||
Convert(PChar(Result),NewLen);
|
Convert(PChar(Result),NewLen);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function XMLValueToStr(const s: string): string;
|
||||||
|
// convert & " &apos < >
|
||||||
|
var
|
||||||
|
Src: PChar;
|
||||||
|
Dst: PChar;
|
||||||
|
begin
|
||||||
|
if Pos('&',s)<1 then exit(s);
|
||||||
|
SetLength(Result,length(s));
|
||||||
|
Src:=PChar(s);
|
||||||
|
Dst:=PChar(Result);
|
||||||
|
repeat
|
||||||
|
case Src^ of
|
||||||
|
#0:
|
||||||
|
if Src-PChar(s)=length(s) then
|
||||||
|
break
|
||||||
|
else
|
||||||
|
inc(Src);
|
||||||
|
'&':
|
||||||
|
begin
|
||||||
|
inc(Src);
|
||||||
|
case Src^ of
|
||||||
|
'a':
|
||||||
|
if (Src[1]='m') and (Src[2]='p') then begin
|
||||||
|
inc(Src,3);
|
||||||
|
if Src^=';' then inc(Src);
|
||||||
|
Dst^:='&';
|
||||||
|
inc(Dst);
|
||||||
|
continue;
|
||||||
|
end else if (Src[1]='p') and (Src[2]='o') and (Src[3]='s') then begin
|
||||||
|
inc(Src,4);
|
||||||
|
if Src^=';' then inc(Src);
|
||||||
|
Dst^:='''';
|
||||||
|
inc(Dst);
|
||||||
|
continue;
|
||||||
|
end;
|
||||||
|
'q':
|
||||||
|
if (Src[1]='u') and (Src[2]='o') and (Src[3]='t') then begin
|
||||||
|
inc(Src,4);
|
||||||
|
if Src^=';' then inc(Src);
|
||||||
|
Dst^:='"';
|
||||||
|
inc(Dst);
|
||||||
|
continue;
|
||||||
|
end;
|
||||||
|
'l':
|
||||||
|
if (Src[1]='t') then begin
|
||||||
|
inc(Src,2);
|
||||||
|
if Src^=';' then inc(Src);
|
||||||
|
Dst^:='<';
|
||||||
|
inc(Dst);
|
||||||
|
continue;
|
||||||
|
end;
|
||||||
|
'g':
|
||||||
|
if (Src[1]='t') then begin
|
||||||
|
inc(Src,2);
|
||||||
|
if Src^=';' then inc(Src);
|
||||||
|
Dst^:='>';
|
||||||
|
inc(Dst);
|
||||||
|
continue;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
Dst^:='&';
|
||||||
|
inc(Dst);
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
Dst^:=Src^;
|
||||||
|
inc(Src);
|
||||||
|
inc(Dst);
|
||||||
|
end;
|
||||||
|
until false;
|
||||||
|
SetLength(Result,Dst-PChar(Result));
|
||||||
|
end;
|
||||||
|
|
||||||
function EncodeLesserAndGreaterThan(const s: string): string;
|
function EncodeLesserAndGreaterThan(const s: string): string;
|
||||||
|
|
||||||
function Convert(Dst: PChar; out NewLen: PtrUInt): boolean;
|
function Convert(Dst: PChar; out NewLen: PtrUInt): boolean;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
Test specific with:
|
Test specific with:
|
||||||
./runtests --format=plain --suite=TestStrToXMLValue
|
./runtests --format=plain --suite=TestStrToXMLValue
|
||||||
|
./runtests --format=plain --suite=TestXMLValueToStr
|
||||||
}
|
}
|
||||||
unit TestLazXML;
|
unit TestLazXML;
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ type
|
|||||||
public
|
public
|
||||||
published
|
published
|
||||||
procedure TestStrToXMLValue;
|
procedure TestStrToXMLValue;
|
||||||
|
procedure TestXMLValueToStr;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@ -39,7 +41,21 @@ begin
|
|||||||
AssertEquals('String with ''',''',StrToXMLValue(''''));
|
AssertEquals('String with ''',''',StrToXMLValue(''''));
|
||||||
AssertEquals('String with "','"',StrToXMLValue('"'));
|
AssertEquals('String with "','"',StrToXMLValue('"'));
|
||||||
AssertEquals('String mix 1','<a>"',StrToXMLValue('<a>'#0'"'));
|
AssertEquals('String mix 1','<a>"',StrToXMLValue('<a>'#0'"'));
|
||||||
AssertEquals('String mix 1','abc',StrToXMLValue('abc'));
|
AssertEquals('String mix 2','abc',StrToXMLValue('abc'));
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TTestLazXML.TestXMLValueToStr;
|
||||||
|
begin
|
||||||
|
AssertEquals('Empty string','',XMLValueToStr(''));
|
||||||
|
AssertEquals('Short string','a',XMLValueToStr('a'));
|
||||||
|
AssertEquals('Short string',#0,XMLValueToStr(#0));
|
||||||
|
AssertEquals('String with &','&',XMLValueToStr('&'));
|
||||||
|
AssertEquals('String with <','<',XMLValueToStr('<'));
|
||||||
|
AssertEquals('String with >','>',XMLValueToStr('>'));
|
||||||
|
AssertEquals('String with ''','''',XMLValueToStr('''));
|
||||||
|
AssertEquals('String with "','"',XMLValueToStr('"'));
|
||||||
|
AssertEquals('String mix 1','<a>"',XMLValueToStr('<a>"'));
|
||||||
|
AssertEquals('String mix 2','abc',XMLValueToStr('abc'));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
|
Loading…
Reference in New Issue
Block a user