Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-05

fpWeb and html and uri escaping/unescaping elements

13 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt
to ik
ik wrote
Hello, Does fpWeb have any method/function that escape and un-escape html and URI elements ?

Yes.

in fphttpclient:

Function EncodeURLElement(S : String) : String;
Function DecodeURLElement(Const S : String) : String;

We should maybe move them to URIParser or maybe even strutils.

Michael.

ik
to Michael Van Canneyt
Michael Van Canneyt wrote 19 lines
On Tue, 24 May 2011, ik wrote: Hello, > > Does fpWeb have any method/function that escape and un-escape html and URI > elements ? > Yes. in fphttpclient: Function EncodeURLElement(S : String) : String; Function DecodeURLElement(Const S : String) : String; We should maybe move them to URIParser or maybe even strutils.

I can not find any HTML encode and decode, I'll make one and open a bug with the patches.
Where do you think it will be best to place it (I mean the unit) ?

Michael.

Thanks,

Ido

_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
ik
to ik: On Tue, May 24, 2011 at 00:23, Michael Van Canneyt I can not find any HTML encode and…
ik wrote 30 lines
On Tue, May 24, 2011 at 00:23, Michael Van Canneyt < > wrote: > > > On Tue, 24 May 2011, ik wrote: > > Hello, >> >> Does fpWeb have any method/function that escape and un-escape html and >> URI elements ? >> > > Yes. > > in fphttpclient: > > Function EncodeURLElement(S : String) : String; > Function DecodeURLElement(Const S : String) : String; > > We should maybe move them to URIParser or maybe even strutils. > I can not find any HTML encode and decode, I'll make one and open a bug with the patches. Where do you think it will be best to place it (I mean the unit) ?

I've created a patch with the Escape and unEscape functions, and place it here: http://bugs.freepascal.org/view.php?id=19407

Show quoted text 17 lines
> > Michael. > Thanks, Ido > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
José Mejuto
to ik

Hello FPC-Pascal,

Tuesday, May 24, 2011, 10:09:03 AM, you wrote:

i> I've created a patch with the Escape and unEscape functions, and place it i> here: http://bugs.freepascal.org/view.php?id=19407

Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test against:

"&amp;lt;"

Right unescape "&lt;", your code "<".

Best regards, José
ik
to José Mejuto
José Mejuto wrote 13 lines
Hello FPC-Pascal, Tuesday, May 24, 2011, 10:09:03 AM, you wrote: i> I've created a patch with the Escape and unEscape functions, and place it i> here: http://bugs.freepascal.org/view.php?id=19407 Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test against: "&amp;lt;"

I'm not sure what you mean here.

If you have already html entities you should not escape them. If you do not have html entities you should escape them.
The Escaping and unescaping works well, I already tested them before I sent them.

Show quoted text 7 lines
Right unescape "&lt;", your code "<". -- Best regards, José

Ido

Show quoted text 5 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ludo Brands
to ik

You should not unescape recursively.
Input to EscapeHTML: '&lt;' Output: '&amp;lt;' : Correct UnescapeHTML: input '&amp;lt;' Output '<' Wrong. This is because you replace '&amp;' with '&' resulting in '&lt;' which is translated to '< ' in the next line.

Ludo

Earlier message 52 lines
-----Message d'origine----- De : De la part de ik Envoyé : mercredi 25 mai 2011 08:34 À : FPC-Pascal users discussions Objet : Re: Re[2]: [fpc-pascal] fpWeb and html and uri escaping/unescapingelements On Tue, May 24, 2011 at 12:21, José Mejuto wrote: Hello FPC-Pascal, Tuesday, May 24, 2011, 10:09:03 AM, you wrote: i> I've created a patch with the Escape and unEscape functions, and place it i> here: http://bugs.freepascal.org/view.php?id=19407 Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test against: "&amp;lt;" I'm not sure what you mean here. If you have already html entities you should not escape them. If you do not have html entities you should escape them. The Escaping and unescaping works well, I already tested them before I sent them. Right unescape "&lt;", your code "<". -- Best regards, José Ido
ik
to Ludo Brands
Ludo Brands wrote 6 lines
You should not unescape recursively. Input to EscapeHTML: '&lt;' Output: '&amp;lt;' : Correct UnescapeHTML: input '&amp;lt;' Output '<' Wrong. This is because you replace '&amp;' with '&' resulting in '&lt;' which is translated to '< ' in the next line.

So you suggest to place the &amp; translation last.

Show quoted text 63 lines
Ludo -----Message d'origine----- *De :* [mailto: *De la part de* ik *Envoyé :* mercredi 25 mai 2011 08:34 *À :* FPC-Pascal users discussions *Objet :* Re: Re[2]: [fpc-pascal] fpWeb and html and uri escaping/unescapingelements On Tue, May 24, 2011 at 12:21, José Mejuto wrote: > Hello FPC-Pascal, > > Tuesday, May 24, 2011, 10:09:03 AM, you wrote: > > i> I've created a patch with the Escape and unEscape functions, and place > it > i> here: http://bugs.freepascal.org/view.php?id=19407 > > Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test > against: > > "&amp;lt;" > I'm not sure what you mean here. If you have already html entities you should not escape them. If you do not have html entities you should escape them. The Escaping and unescaping works well, I already tested them before I sent them. > > Right unescape "&lt;", your code "<". > > -- > Best regards, > José > Ido > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ludo Brands
to ik: I've created a patch with the Escape and unEscape functions, and place it here:…

This is a very minimalistic implementation covering only partial ascii character escaping. To cover ASCII only in unescapeHTML you should also add '&nbsp' which corresponds with the space character ' '. Also the format &#entity_number; is missing ('&lt;' or '&#60;' equals '<')

To support iso-8859-1 which is default in most browsers you'll need a lot more. See f.e. http://www.w3schools.com/tags/ref_entities.asp

Ludo

Earlier message 65 lines
-----Message d'origine----- De : De la part de ik Envoyé : mardi 24 mai 2011 10:09 À : FPC-Pascal users discussions Objet : Re: [fpc-pascal] fpWeb and html and uri escaping/unescaping elements On Tue, May 24, 2011 at 00:28, ik wrote: On Tue, May 24, 2011 at 00:23, Michael Van Canneyt wrote: On Tue, 24 May 2011, ik wrote: Hello, Does fpWeb have any method/function that escape and un-escape html and URI elements ? Yes. in fphttpclient: Function EncodeURLElement(S : String) : String; Function DecodeURLElement(Const S : String) : String; We should maybe move them to URIParser or maybe even strutils. I can not find any HTML encode and decode, I'll make one and open a bug with the patches. Where do you think it will be best to place it (I mean the unit) ? I've created a patch with the Escape and unEscape functions, and place it here: http://bugs.freepascal.org/view.php?id=19407 Michael. Thanks, Ido
José Mejuto
to ik: I'm not sure what you mean here. If you have already html entities you should not escape…

Hello FPC-Pascal,

you wrote
> "&amp;lt;"

i> I'm not sure what you mean here.

That's an already escaped html. Let it be a mini htmlpage:

<html><title>-&amp;lt;-</title></html>

i> If you have already html entities you should not escape them. If you do not i> have html entities you should escape them.
i> The Escaping and unescaping works well, I already tested them before I sent i> them.

The right content of <title> tag is "-&lt;-" when unescaped but the code unescapehtml produce "-<-". Explanation step by step:

1) The code looks for all '&amp;'
2) When found replace it by '&'
3) <title> content is now "-&lt;-"
4) Now the code looks for '&lt;'
5) When found replace it by '<'
6) Now title content is "-<-"

----------
 Result := StringReplace(s,      '&amp;',  '&', [rfReplaceAll]);
 Result := StringReplace(Result, '&lt;',   '<', [rfReplaceAll]);
----------

I hope my explanations are now understandable.

Best regards, José
Ludo Brands
to ik: So you suggest to place the &amp; translation last.
So you suggest to place the &amp; translation last.

That would be a solution for this problem. My previous comments on the "minimalistic" approach of the implementation suggest a different approach: scan the source string once and replace html entities as you find them (with a look up table for example). This would scale much better when implementing iso-8859-1 or '&#entity-number;' unescaping .

Ludo

Earlier message 79 lines
-----Message d'origine----- De : De la part de ik Envoyé : mercredi 25 mai 2011 09:22 À : FPC-Pascal users discussions Objet : Re: RE : Re[2]: [fpc-pascal] fpWeb and html and uriescaping/unescapingelements On Wed, May 25, 2011 at 10:09, Ludo Brands wrote: You should not unescape recursively. Input to EscapeHTML: '&lt;' Output: '&amp;lt;' : Correct UnescapeHTML: input '&amp;lt;' Output '<' Wrong. This is because you replace '&amp;' with '&' resulting in '&lt;' which is translated to '< ' in the next line. So you suggest to place the &amp; translation last. Ludo -----Message d'origine----- De : De la part de ik Envoyé : mercredi 25 mai 2011 08:34 À : FPC-Pascal users discussions Objet : Re: Re[2]: [fpc-pascal] fpWeb and html and uri escaping/unescapingelements On Tue, May 24, 2011 at 12:21, José Mejuto wrote: Hello FPC-Pascal, Tuesday, May 24, 2011, 10:09:03 AM, you wrote: i> I've created a patch with the Escape and unEscape functions, and place it i> here: http://bugs.freepascal.org/view.php?id=19407 Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test against: "&amp;lt;" I'm not sure what you mean here. If you have already html entities you should not escape them. If you do not have html entities you should escape them. The Escaping and unescaping works well, I already tested them before I sent them. Right unescape "&lt;", your code "<". -- Best regards, José Ido
ik
to Ludo Brands
Ludo Brands wrote 7 lines
> So you suggest to place the &amp; translation last. That would be a solution for this problem. My previous comments on the "minimalistic" approach of the implementation suggest a different approach: scan the source string once and replace html entities as you find them (with a look up table for example). This would scale much better when implementing iso-8859-1 or '&#entity-number;' unescaping .

For every html entity you need a dictionary, and that's makes your program with more fat.
converting number entity can be done, but you need to know what is the code page you wish to convert from/to.

Ludo

Ido

Show quoted text 90 lines
-----Message d'origine----- *De :* [mailto: *De la part de* ik *Envoyé :* mercredi 25 mai 2011 09:22 *À :* FPC-Pascal users discussions *Objet :* Re: RE : Re[2]: [fpc-pascal] fpWeb and html and uriescaping/unescapingelements On Wed, May 25, 2011 at 10:09, Ludo Brands wrote: > You should not unescape recursively. > Input to EscapeHTML: '&lt;' Output: '&amp;lt;' : Correct > UnescapeHTML: input '&amp;lt;' Output '<' Wrong. > This is because you replace '&amp;' with '&' resulting in '&lt;' which is > translated to '< ' in the next line. > So you suggest to place the &amp; translation last. > > Ludo > > > > -----Message d'origine----- > *De :* [mailto: > *De la part de* ik > *Envoyé :* mercredi 25 mai 2011 08:34 > *À :* FPC-Pascal users discussions > *Objet :* Re: Re[2]: [fpc-pascal] fpWeb and html and uri > escaping/unescapingelements > > > > > On Tue, May 24, 2011 at 12:21, José Mejuto wrote: > >> Hello FPC-Pascal, >> >> Tuesday, May 24, 2011, 10:09:03 AM, you wrote: >> >> i> I've created a patch with the Escape and unEscape functions, and place >> it >> i> here: http://bugs.freepascal.org/view.php?id=19407 >> >> Un/escapeHTML parsing must be in one go, specially the "&amp" one. Test >> against: >> >> "&amp;lt;" >> > > I'm not sure what you mean here. > > If you have already html entities you should not escape them. If you do > not have html entities you should escape them. > The Escaping and unescaping works well, I already tested them before I > sent them. > > > >> >> Right unescape "&lt;", your code "<". >> >> -- >> Best regards, >> José >> > > Ido > > >> >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal